https://rentry.org/PPP2_p148

// Code derived from Stroustrup's PPP2 book
// § 5.6.2 Range errors
//  -and beginning on p 148

#include <iostream>
#include <vector>

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

int main()
{
  cout << "Please enter integers: (enter '|' to stop):\n";
  vector<int> v;          // a vector of ints
  for (int i; cin >> i;)  // get values
    v.push_back(i);

  for (unsigned int i = 0; i <= v.size(); ++i)  // print values
    cout << "v[" << i << "] == " << v[i] << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 22 Feb 2023 16:08 UTC
Edit: 29 Apr 2023 09:26 UTC
Views: 366