/* * Read some integers into an array, then echo them, using pointers. */ #include using namespace std; const int ARRMAX = 100; int main() { // Read them in. int arr[ARRMAX]; /* Array of numbers. */ int *scan = arr; cout << "Enter some integers: " << endl; while(cin >> *scan) ++scan; int *end = scan; // Print them back out again. cout << "===============" << endl; for(scan = arr; scan < end; ++scan) { cout << *scan << " "; } cout << 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; while(scan < end) { std::cout << *scan; scan += 2; if(scan < end) { std::cout << " "; } } std::string sep = "|"; for(scan = arr + 1; scan < end; scan += 2) { std::cout << sep << *scan; sep = " "; } std::cout << "\n===============" << std::endl; }