Array With Explicit Bounds Check

Programmer uses explicit checks to keep the array in bounds.

/* * Read some integers into an array, then echo them. */ #include <iostream> #include <array> int main() { // Read them in. We use a check to end the reading loop if the // array fills up. std::array<int, 10> arr; int sub = 0; std::cout << "Enter some integers: " << std::endl; int val; while(std::cin >> val) { if(sub >= arr.size()) { std::cout << "Array full; reading stopped." << std::endl; break; } arr[sub++] = val; } int num = sub; // Print them back out again. std::cout << "===============" << std::endl; for(int sub = 0; sub < num; ++sub) std::cout << arr[sub] << " "; std::cout << std::endl; }