从字符串到字符串流再到vector< int>。

问题描述:

我已经有了要在应用程序中实现的步骤的示例程序。我想将字符串上的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


    }


int num;
while (lineStream >> num) numbers.push_back(num);