Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going from string to stringstream to vector<int>

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


    }
like image 528
andandandand Avatar asked Jan 18 '09 17:01

andandandand


1 Answers

int num;
while (lineStream >> num) numbers.push_back(num);
like image 162
mmx Avatar answered Oct 17 '22 18:10

mmx