简体   繁体   English

填充矢量 <int> 来自char *中的整数

[英]Populate a vector<int> from integers in a char *

char *values = "   3   1   4 15";

vector<int> array;

I want to populate the array with the values, 我想用数值填充数组,

3,1,4,15 3,1,4,15

Is there a slick way to do it with the stl copy algorithm? 使用stl复制算法有一种灵巧的方法吗?

Indeed there is: 确实有:

std::istringstream iss(values);
std::copy(std::istream_iterator<int>(iss), 
          std::istream_iterator<int>(), 
          std::back_inserter(array));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM