Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 拆分:如何拆分带有字符的字符串?_C++ - Fatal编程技术网

C++ 拆分:如何拆分带有字符的字符串?

C++ 拆分:如何拆分带有字符的字符串?,c++,C++,我被这个简单的问题困住了。 假设我有一个由字符[0-9]组成的字符串。我想做的是使用boost::split将字符串按单个字符分割 std::string str = "0102725"; std::vector<std::string> str2; boost::split(str2, str, boost::is_any_of(SOMETHING)); std::string str=“0102725”; std::载体str2; boost::split(str2,str,b

我被这个简单的问题困住了。 假设我有一个由字符[0-9]组成的字符串。我想做的是使用boost::split将字符串按单个字符分割

std::string str = "0102725";
std::vector<std::string> str2;
boost::split(str2, str, boost::is_any_of(SOMETHING));
std::string str=“0102725”;
std::载体str2;
boost::split(str2,str,boost::是(某物)的任意部分);

我在寻找这样的东西:str2[0]包含“0”,str2[1]包含“1”,str2[2]包含“0”,依此类推。到目前为止,我已经尝试了“,”和“:”但是没有运气…

boost::split
在这方面做得太过分了

for (size_t i=0; i < str.length(); i++)
  str2.push_back(std::string(1, str.at(i)));
(大小i=0;i str2.push_back(std::string(1,str.at(i));
除了@Mat的方法,下面是我对它的看法。由于您正在分隔可能不需要的字符

vector<string> str2; // acquires character + extra space for '\0'
向量str2;//为“\0”获取字符+额外空间
而是

vector<char> str2; // only single character
向量str2;//只有一个字符
以下是您可以选择的方法:

for(unsigned int i = 0; i < str.size(); i++)
  str2.push_back(str[i]);
for(无符号整数i=0;i

。您可以在需要时访问
str2[i]

您认为
std::string
的构造函数中的哪一个会触发…?;-)好极了。和+1表示简单实用的方法。@Mat谢谢!看起来我太沉迷于使用boost::split了。谢谢你的替代方法。