/* * Read some integers into an array, then echo them, using iterators. */ #include #include int main() { // Read them in. std::array arr; std::array::iterator scan = arr.begin(); std::cout << "Enter some integers: " << std::endl; while(std::cin >> *scan) { ++scan; } auto end = scan; // Print them back out again. std::cout << "===============" << std::endl; for(scan = arr.begin(); scan < end; ++scan) { std::cout << *scan << " "; } std::cout << std::endl; // Array iterators can be compared, the result reflecting their // position, and they can incremented by numbers other than 1. // Here, we go up the even-numbered ones, then the odd ones. scan = arr.begin(); while(scan < end) { std::cout << *scan; scan += 2; if(scan < end) { std::cout << " "; } } std::string sep = "|"; for(scan = arr.begin() + 1; scan < end; scan += 2) { std::cout << sep << *scan; sep = " "; } std::cout << "\n===============" << std::endl; }