C ++:将字符串和内容分割成std :: vector的优雅方式 [英] C++: Elegant way to split string and stuff contents into std::vector

查看:252
本文介绍了C ++:将字符串和内容分割成std :: vector的优雅方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想沿着空格分割一个字符串,我知道标记
表示有效的整数。我想将令牌转换为整数
并填充一个向量与它们。

I would like to split a string along whitespaces, and I know that the tokens represent valid integers. I would like to transform the tokens into integers and populate a vector with them.

我可以使用boost :: split,使一个令牌字符串的向量,然后使用std :: transform。

I could use boost::split, make a vector of token strings, then use std::transform.

你的解决方案是什么?使用boost是可以接受的。

What is your solution? Using boost is acceptable.

推荐答案

类似这样:

std::istringstream iss("42 4711 ");
std::vector<int> results( std::istream_iterator<int>(iss)
                        , std::istream_iterator<int>() );

这篇关于C ++:将字符串和内容分割成std :: vector的优雅方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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