7.4.3. Specifying an array’s Size with a Constant Variable and Setting array Elements with Calculations

Figure 7.5 sets the elements of a 5-element array s to the even integers 2, 4, 6, 8 and 10 (lines 15–16) and prints the array in tabular format (lines 18–22). These numbers are generated (line 16) by multiplying each successive value of the loop counter by 2 and adding 2.

 1   // Fig. 7.5: fig07_05.cpp 2   // Set array s to the even integers from 2 to 10. 3   #include <iostream> 4   #include <iomanip> 5   #include <array> 6   using namespace std; 7  8   int main() 9   {10      // constant variable can be used to specify array size11      const size_t arraySize = 5; // must initialize in declaration 12 13       ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.