Array Container With Iterator
/* * Read some integers into an array, then echo them, using iterators. */ #include <iostream> #include <array> int main() { // Read them in. std::array<int, 100> arr; std::array<int, 100>::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; // Comparison of array iterators give the same result as compairing // the denoted subscripts, 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; }