问题描述
尝试使用istringstream读取字符串中元素时报错
#include <iostream>
#include <vector>
using namespace std;
int main() {
string input = "2-2-11-3";
std::istringstream ss(input);
int num;
char c = ' ';
while (ss >> num && ss >> c) {
cout << num << endl;
cout << c << endl;
}
return 0;
}
解决方法
少include了必要的头文件,添加 #include <sstream>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string input = "2-2-11-3";
std::istringstream ss(input);
int num;
char c = ' ';
while (ss >> num && ss >> c) {
cout << num << endl;
cout << c << endl;
}
return 0;
}