你在程序设置是在CMD值线的末端需要;
。
告诉你的源代码有没有问题(如果你添加;
)。我们可以编译它没有警告,它工作得很好,如果我们增加萨姆线,使其成为一个独立的程序:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main(int ac, char **av) {
std::string cmd = "par1=1&par2=ciao&par3=1.2e4";
std::stringstream ss(cmd);
std::string argdef;
std::vector<std::string> pairs(0);
while (std::getline(ss, argdef, '&')) {
std::cout << argdef << std::endl;
pairs.push_back(argdef);
}
return 0;
}
例如,汇编作品在Linux上的OpenSuSE LEAP 42.1无预警与G ++/GCC 4.8。 5(源文件被命名为x.cpp),输出是正确的(你可以看到,我已经添加了一行记录argdef的内容,它看起来是正确的):
% g++ -Wall -o x x.cpp
% ./x
par1=1
par2=ciao
par3=1.2e4
%
所以这里一切都很好。
说明你的段错误错误的根本原因:
- 要么
;
你已经在第一行遗忘使程序不能编译,而你正在运行的二进制是一个旧的,有错误;
- 或者您的程序中有其他任何内容会导致分段错误。
无论如何,这里显示的源代码是正确的。
[Can not reproduce。](http://coliru.stacked-crooked.com/a/35cfe24f53a60b49) – aschepler
很确定getline会通过null结束符发挥作用。尝试添加一个'&'到你的字符串的末尾?没关系... stringstream应该为你处理eof。 – zzxyz