记录一个根据空格分隔字符串的函数,自己写的split
代码:
#include <iostream>
#include <vector>
#include <string>
#include<sstream>
using namespace std;
vector<string> split(string temp)//按空格分隔字符串
{
vector<string> str;
string word;
stringstream input;//利用字符串流输入
input << temp;
while (input >> word)//循环把缓冲区数据读出
{
str.push_back(word);
}
return str;
}
int main()
{
string str = "hello world !";//测试
vector<string> vec = split(str);
for (size_t i=0;i<vec.size();i++)//输出
{
cout << vec[i] << endl;
}
system("pause");
return 0;
}