I've this sample program of a step that I want to implement on my application. I want to push_back the int elements on the string separately, into a vector. How can I?
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main(){
string line = "1 2 3 4 5"; //includes spaces
stringstream lineStream(line);
vector<int> numbers; // how do I push_back the numbers (separately) here?
// in this example I know the size of my string but in my application I won't
}
This is a classic example of
std::back_inserter
.You can create the vector right from the start on, if you wish
Remember to put parentheses around the first argument. The compiler thinks it's a function declaration otherwise. If you use the vector for just getting iterators for the numbers, you can use the istream iterators directly: