http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html
C++標准庫里面沒有字符分割函數split ,這可太不方便了,我已經遇到>3次如何對字符串快速分割這個問題了。列幾個常用方法以備不時之需。
方法一: 利用STL自己實現split 函數(常用,簡單,直觀)
原型: vector<string> split(const string &s, const string &seperator);
輸入一個字符串,一個分隔符字符串(可包含多個分隔符),返回一個字符串向量。這是我最喜歡的方法,因為它最直觀,在平常也最常用。實現及測試代碼如下
#include <vector> #include <string> #include <iostream> using namespace std; vector<string> split(const string &s, const string &seperator){ vector<string> result; typedef string::size_type string_size; string_size i = 0; while(i != s.size()){ //找到字符串中首個不等於分隔符的字母; int flag = 0; while(i != s.size() && flag == 0){ flag = 1; for(string_size x = 0; x < seperator.size(); ++x) if(s[i] == seperator[x]){ ++i; flag = 0; break; } } //找到又一個分隔符,將兩個分隔符之間的字符串取出; flag = 0; string_size j = i; while(j != s.size() && flag == 0){ for(string_size x = 0; x < seperator.size(); ++x) if(s[j] == seperator[x]){ flag = 1; break; } if(flag == 0) ++j; } if(i != j){ result.push_back(s.substr(i, j-i)); i = j; } } return result; } int main(){ string s = "a,b*c*d,e"; vector<string> v = split(s, ",*"); //可按多個字符來分隔; for(vector<string>::size_type i = 0; i != v.size(); ++i) cout << v[i] << " "; cout << endl; //輸出: a b c d }
提供了一段更簡潔高效的代碼,實現如下:
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c) { std::string::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while(std::string::npos != pos2) { v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if(pos1 != s.length()) v.push_back(s.substr(pos1)); }
方法二: 用C語言中的strtok 函數來進行分割
原型: char *strtok(char *str, const char *delim);
strtok函數包含在頭文件<string.h>中,對於字符數組可以采用這種方法處理。當然也可以將字符數組轉換成字符串之后再使用法一。測試代碼如下
#include <string.h> #include <stdio.h> int main(){ char s[] = "a,b*c,d"; const char *sep = ",*"; //可按多個字符來分割 char *p; p = strtok(s, sep); while(p){ printf("%s ", p); p = strtok(NULL, sep); } printf("\n"); return 0; } //輸出: a b c d
map:數據的插入
在構造map容器后,我們就可以往里面插入數據了。這里講三種插入數據的方法:
第一種:用insert函數插入pair數據
第一種:用insert函數插入pair數據
map<int, string> mapStudent; mapStudent.insert(pair<int, string>(1,“student_one”));
第二種:用insert函數插入value_type數據
map<int, string> mapStudent; mapStudent.insert(map<int, string>::value_type (1,"student_one"));
mapStudent.insert(make_pair(1, "student_one"));
第三種:用數組方式插入數據
map<int, string> mapStudent; mapStudent[1] = “student_one”; mapStudent[2] = “student_two”;
以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數插入數據,在數據的插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是不能再插入這個數據的,但是用數組方式就不同了,它可以覆蓋以前該關鍵字對應的值,即:如果當前存在該關鍵字,則覆蓋改關鍵字的值,否則,以改關鍵字新建一個key—value;
筆試題代碼:

#include<iostream> #include<string> #include<vector> #include<map> using namespace std; #define HTTP_HEADER \ "GET /index.html HTTP/1.1.\r\n" \ "Content-Type :text\r\n" \ "Host:web\r\n" \ "Accept:text/thml\r\n" \ "Accept-Encoding:identifty\r\n" \ "User-Agent:Morzilla\r\n" \ "\r\n" vector<string> split(const string &s, const string &seperator){ vector<string> result; typedef string::size_type string_size; string_size i = 0; while (i != s.size()){ //找到字符串中首個不等於分隔符的字母; int flag = 0; while (i != s.size() && flag == 0){ flag = 1; for (string_size x = 0; x < seperator.size(); ++x) if (s[i] == seperator[x]){ ++i; flag = 0; break; } } //找到又一個分隔符,將兩個分隔符之間的字符串取出; flag = 0; string_size j = i; while (j != s.size() && flag == 0){ for (string_size x = 0; x < seperator.size(); ++x) if (s[j] == seperator[x]){ flag = 1; break; } if (flag == 0) ++j; } if (i != j){ result.push_back(s.substr(i, j - i)); i = j; } } return result; } void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c) { std::string::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while (std::string::npos != pos2) { v.push_back(s.substr(pos1, pos2 - pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if (pos1 != s.length()) v.push_back(s.substr(pos1)); } int main(){ map<string, string> headers; string str = HTTP_HEADER; cout << str << endl; vector<string> v = split(str, "\r\n"); //可按多個字符來分隔; string str0Line = v[0]; string str1Line = v[1]; string str2Line = v[2]; string str3Line = v[3]; string str4Line = v[4]; string str5Line = v[5]; cout << str0Line << endl; cout << str1Line << endl; cout << str2Line << endl; cout << str3Line << endl; cout << str4Line << endl; cout << str5Line << endl; vector<string> v1 = split(str0Line, " "); //可按多個字符來分隔; string str00Line = v1[0]; string str01Line = v1[1]; string str02Line = v1[2]; cout << str00Line << endl; cout << str01Line << endl; cout << str02Line << endl; for (vector<string>::size_type i = 1; i != v.size(); ++i) { vector<string> v2 = split(v[i], ":"); //可按:字符來分隔; string str10Line = v2[0]; string str11Line = v2[1]; headers.insert(pair<string, string>(str10Line, str11Line)); cout << str10Line << " " << str11Line << endl; } getchar(); getchar(); return 0; }