https://rentry.org/PPP2_p120

// Code derived from Stroustrup's PPP2 book
// § 4.6.2 Growing a vector
//  -and beginning on p 120

#include <iostream>
#include <vector>

using std::cout;
using std::vector;

int main()
{
  vector<double> v;  // start off empty; that is, v has no elements

  v.push_back(2.7);  // add an element with the value 2.7 at end (“the back”)
                     //  of v
                     // v now has one element and v[0]==2.7

  v.push_back(5.6);  // add an element with the value 5.6 at end of v
                     // v now has two elements and v[1]==5.6

  v.push_back(7.9);  // add an element with the value 7.9 at end of v
                     // v now has three elements and v[2]==7.9

  // output above variable values to console...

  for (double e : v)
    cout << e << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p120.cpp && ./a.out

PrevUpNext

Edit
Pub: 08 Feb 2023 01:30 UTC
Edit: 29 Apr 2023 08:40 UTC
Views: 483