Populate a vector<int> from integers in a char *

[亡魂溺海] 提交于 2020-01-02 08:38:22

问题


char *values = "   3   1   4 15";

vector<int> array;

I want to populate the array with the values,

3,1,4,15

Is there a slick way to do it with the stl copy algorithm?


回答1:


Indeed there is:

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


来源:https://stackoverflow.com/questions/372453/populate-a-vectorint-from-integers-in-a-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!