- 输入1,2,3,4,以字符,来分割后数组是[1,2,3,4]
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void split(const string& s, vector<int>& sv, const char flag = ' ') {
sv.clear();
istringstream iss(s);
string temp;
while (getline(iss, temp, flag)) {
sv.push_back(stoi(temp));
}
return;
}
int main() {
string s;
cin >> s;
vector<int> sv;
split(s, sv, ',');
cout << "验证切分效果\n";
for (const auto& s : sv) {
cout << s << ' ';
}
return 0;
}