C++ array loop via for each statement
#include <iostream> #include <array> #include <vector> #include <ios> using namespace std; int main(int argc, char *argv []) { // ww w . j a va 2s .com const unsigned int ARRAY_SIZE = 5; using MyArray = std::array<int, ARRAY_SIZE>; MyArray = { 0, 1, 2, 3, 4 }; for (unsigned int i = 0; i < ARRAY_SIZE; ++i) { if (myArray[i] == 2) { cout << "Found: " << myArray[i] << " at position: " << i << endl; } } for (auto iter=myArray.begin(); iter!=myArray.end(); ++iter) { if (*iter == 2) { cout << "Found: " << *iter << endl; } } for (auto& number : myArray) { if (number == 2) { cout << "Found : " << number << endl; } } MyArray::iterator found = find(myArray.begin(), myArray.end(), 2); if (found != myArray.end()) { cout << "Found : " << *found << endl; } return 0; }