目录
单个字符作为分割
函数原型
vector<string> Split(const string &s, const char &seperator);
方法一:
istringstream类用于执行C++风格的串流的输入操作
istringstream的构造函数原形如下:
istringstream::istringstream(string str), 它的作用是从string对象str中读取字符
vector<string> Split(const string &s, const char &seperator) {
vector<string> ans;
istringstream stream(s);
string word;
while (getline(stream, word, seperator)) {
ans.push_back(word);
}
return ans;
}
方法二:
vector<string> Split(const string &s, const char &seperator) {
vector<string> ans;
string tmp = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] != seperator) {
tmp += s[i];
} else {
ans.push_back(tmp);
tmp = "";
}
}
ans.push_back(tmp);
return ans;
}
字符串作为分割
函数原型
vector<string> Split(const string &s, const string &seperator)
方法一:
vector<string> Split(const string &s, const string &seperator) {
vector<string> ans;
string token, str = s;
size_t pos = 0;
while((pos = str.find(seperator)) != string::npos) {
token = str.substr(0, pos);
ans.push_back(token);
str.erase(0, pos + seperator.length());
}
ans.push_back(str);
return ans;
}
方法二:
vector<string> Split(const string &s, const string &seperator) {
vector<string> ans;
if(s == "") return ans;
//s -> char *
char * strs = new char[s.length() + 1] ; //不要忘了
strcpy(strs, s.c_str());
//seperator -> char *
char * d = new char[seperator.length() + 1];
strcpy(d, seperator.c_str());
char *p = strtok(strs, d);
while(p) {
string token = p; //分割得到的字符串转换为string类型
ans.push_back(token); //存入结果数组
p = strtok(NULL, d);
}
delete[] strs;
delete[] d;
return ans;
}
代码测试
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
vector<string> Split1(const string &s, const char &seperator) {
vector<string> ans;
istringstream stream(s);
string word;
while (getline(stream, word, seperator)) {
ans.push_back(word);
}
return ans;
}
vector<string> Split2(const string &s, const char &seperator) {
vector<string> ans;
string tmp = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] != seperator) {
tmp += s[i];
} else {
ans.push_back(tmp);
tmp = "";
}
}
ans.push_back(tmp);
return ans;
}
vector<string> Split3(const string& s, const string &seperator) {
vector<string> ans;
if(s == "") return ans;
//s -> char *
char * strs = new char[s.length() + 1] ; //不要忘了
strcpy(strs, s.c_str());
//seperator -> char *
char * d = new char[seperator.length() + 1];
strcpy(d, seperator.c_str());
char *p = strtok(strs, d);
while(p) {
string token = p; //分割得到的字符串转换为string类型
ans.push_back(token); //存入结果数组
p = strtok(NULL, d);
}
delete[] strs;
delete[] d;
return ans;
}
vector<string> Split4(const string &s, const string &seperator) {
vector<string> ans;
string token, str = s;
size_t pos = 0;
while((pos = str.find(seperator)) != string::npos) {
token = str.substr(0, pos);
ans.push_back(token);
str.erase(0, pos + seperator.length());
}
ans.push_back(str);
return ans;
}
int main() {
string str1 = "hello c++ c java python php Go";
string str2 = "192.168.10.254";
string str3 = "88<=99<=100<=166";
string str4 = "scott>=tiger>=mushroom";
vector<string> v1 = Split1(str1, ' ');
vector<string> v2 = Split2(str2, '.');
vector<string> v3 = Split3(str3, "<=");
vector<string> v4 = Split4(str4, ">=");
for (auto &x: v1) cout << x << ' ';
cout << endl;
for (auto &x: v2) cout << x << ' ';
cout << endl;
for (auto &x: v3) cout << x << ' ';
cout << endl;
for (auto &x: v4) cout << x << ' ';
return 0;
}