C++ string分割字符串, char数组分割字符串

tech2022-12-17  112

C++ string字符串分割函数

单个分割字符

#include <iostreeam> #include <string> #include <vector> using namespace std; vector<string> Split(const string &str, const string &deln) { vector<string> strvec; string::size_type pos1, pos2; string c_str = str + deln; pos2 = c_str.find(deln); pos1 = 0; while(pos2 != string::npos) { string c_sing = c_str.substr(pos1, pos2 - pos1); if(c_sing.size() != 0) strvec.pushback(c_sing); pos1 = pos2 + deln.size(); pos2 = c_str.find(deln, pos1); } return strvec; }

C语言 strtok函数分割字符串(单/多字符)

#include <iostream> #include <stdio.h> #include <string> #include <vector> using namespace std; vector<string> Split(char chr[], char *deln) { vector<string> chevec; char *p; p = strtok(chr, deln); while(p) { chevec.pushback(p); p = strtok(Null, deln); } return chevec; }
最新回复(0)