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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.