Originally Posted by
CPlus
Would the C++ equivalent look something like this?
No, since you cannot read into a vector like that. By the way, I suggest that you reserve fully capitalised names for macro names. I note that PEP-8 recommends that constants be fully capitalised, thus your naming convention is also unusual in Python.
This is a roughly equivalent C++ program:
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
using namespace std;
// Read in a list of numbers on a line as a string, e.g., 0 1 2 3 4 5
cout << "Input your list here: ";
string line;
getline(cin, line);
// Extract the numbers from the string into a vector of integers.
stringstream ss(line);
vector<int> numbers;
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(numbers));
// Append 1
numbers.push_back(1);
// Print the vector of integers
copy(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
This part may be confusing:
Code:
stringstream ss(line);
vector<int> numbers;
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(numbers));
The idea is to treat the line of numbers as an input stream by using a string stream. You can then read from this string stream as if the user entered input from standard input, except that now the line of input has already been read.
The use of istream_iterator<int> allows one to iterate over the ints of the input stream, using operator>>. back_inserter means that for each int x read, numbers.push_back(x) is performed (i.e., the number read is inserted at the back).