C ++,从字符串到stringstream到向量< int> [英] C++, Going from string to stringstream to vector<int>

查看:124
本文介绍了C ++,从字符串到stringstream到向量< int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个步骤的示例程序,我想在我的应用程序上实现。我想push_back分别在字符串上的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);

这篇关于C ++,从字符串到stringstream到向量&lt; int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆