Comparing Arrays, Vector Objects, and Array Objects

Perhaps the simplest way to understand the similarities and differences between arrays, vector objects, and array objects is to look at a brief example (Listing 4.24) that uses all three approaches.

Listing 4.24. choices.cpp

// choices.cpp -- array variations#include <iostream>#include <vector>   // STL C++98#include <array>    // C++11int main(){    using namespace std;// C, original C++    double a1[4] = {1.2, 2.4, 3.6, 4.8};// C++98 STL    vector<double> a2(4);   // create vector with 4 elements// no simple way to initialize in C98    a2[0] = 1.0/3.0;    a2[1] = 1.0/5.0;    a2[2] = 1.0/7.0;    a2[3] = 1.0/9.0;// C++11 -- create and initialize array object    array<double, 4> a3 = {3.14, 2.72, ...

Get C++ Primer Plus now with O’Reilly online learning.

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