作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C++ 的新手,我想我在这段代码的某处犯了一个小错误。到目前为止我没能发现它。我希望你能帮助我,并告诉我它是如何/在哪里/为什么是错误的?非常感谢。
代码:
std::vector<std::string> spliter(const std::string& s, char delimiter)
{
std::vector<std::string> result;
size_t start = 0;
for(std::size_t i = 0; i != std::string::npos; i = s.find(delimiter,start))
{
result.push_back( s.substr(start,i-start) );
start = i+1;
}
iprintf("\x1b[2J");
printf("\x1b[4;0HDone Splitting Text.");
swiWaitForVBlank();
return result;
}
给出的参数:s = "$ 00-000 SS ''Prologue'' CF N00-001 V 1 MP 20"
delimiter = ' '
(一个空格)
预期结果:
result[0] = $
result[1] = 00-000
result[2] = SS
etc.
当前错误结果:
result[0] =
result[1] =
result[2] = 00-000
etc.
非常感谢任何帮助!
最佳答案
我相信问题出在循环中。你从 0 开始,你推送的第一件事是从 0 到 0。
size_t start = 0;
for(std::size_t i = 0; i != std::string::npos; i = s.find(delimiter,start))
{
result.push_back( s.substr(start,i-start) );
start = i+1;
}
相反,如果您从 s.find(delimiter, start)
开始 i
,它应该可以工作。 Example here..
关于C++ 在 char 上正确拆分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15735360/
我是一名优秀的程序员,十分优秀!