實現c++的string的split功能


今天寫程序,遇到了一個要實現string.split()這個的一個函數。python里面有,qt里面有,c++里面沒有。照着網上抄了一個,放在這里。有需要的時候直接拽過去用,否則老是寫了小例子就扔,用的時候沒有,也是個麻煩事

例如 “aa*bb*cc” 會存儲成vector<string> "aa" "bb" "cc"

 1 // temp1.cpp : 定義控制台應用程序的入口點。
 2 
 3 #include "stdafx.h"
 4 #include <iostream>
 5 #include <stdio.h>
 6 #include <string>
 7 #include <vector>
 8 using namespace std;
 9 
10  
11 int _tmain(int argc, _TCHAR* argv[])
12 {
13     char a[] = "abc*123*xyz";  //目標是解析成 abc  123 xyz 然后存儲在下面的變量 vector<string>中
14     string strArry = a;
15     vector<string> strArryList;
16 
17 
18     size_t last = 0;
19     size_t index = strArry.find_first_of("*",last);  //找到last坐標后面的第一個*
20     while( index != std::string::npos )//找到一個推進vector一個,一直到找到了最后
21     {
22         strArryList.push_back( strArry.substr(last, index-last));
23         last = index +1;
24         index = strArry.find_first_of("*",last);
25     }
26     if(index - last > 0) //記得把最后一個推進去.這里是"xyz"
27     {
28         strArryList.push_back( strArry.substr(last, index-last));
29     }
30 
31 
32     for(int i = 0; i < strArryList.size(); i++)
33     std::cout<<strArryList[i]<<std::endl;
34 
35     getchar();
36     return 0;
37 }

 下面是又寫的一個split函數的代碼

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 
 5 using namespace std;
 6 // spCharacter [IN] : 分隔符
 7 // objString [IN] : 要分解的字符串
 8 // stringVector [OUT] : 分解了的字符串
 9 bool splitString(char spCharacter, const string& objString, vector<string>& stringVector)
10 {
11     if (objString.length() == 0)
12     {
13         return true;
14     }
15 
16     size_t posBegin = 0;
17     size_t posEnd = 0;
18     bool lastObjStore = true;
19 
20     while (posEnd != string::npos)
21     {
22         posBegin = posEnd;
23         posEnd = objString.find(spCharacter, posBegin);
24 
25         if (posBegin == posEnd)
26         {
27             posEnd += 1;
28             continue;
29         }
30 
31         if (posEnd == string::npos)
32         {
33             stringVector.push_back( objString.substr(posBegin, objString.length()-posBegin) );
34             break;
35         }
36         
37         stringVector.push_back( objString.substr( posBegin, posEnd - posBegin) );
38         posEnd += 1;    
39     }
40     return true;
41 }
42 int main(int argc, char** argv)
43 {
44     vector<string> paths;
45     paths.push_back("abcde");
46     paths.push_back("aaar/");    
47     paths.push_back("aaar//");    
48     paths.push_back("aaar/bbb");    
49     paths.push_back("aaar/c/bbb");    
50     paths.push_back("aaar//bbb");    
51     paths.push_back("/aar");    
52     paths.push_back("/");    
53     paths.push_back("");    
54     
55     for (auto iter = paths.begin(); iter != paths.end(); iter++)
56     {
57         vector<string> temp;
58         splitString('/', *iter, temp);
59 
60         cout<<"--------begin------"<<endl;
61         cout<<"the source string : ["<<*iter<<"]"<<endl;
62         cout<<"-------------------"<<endl;
63         for (auto iterSplit = temp.begin(); iterSplit != temp.end(); iterSplit++)
64         {
65             cout<<*iterSplit<<endl;
66         }
67         cout<<"--------end------"<<endl<<endl;;
68     }
69 
70     return 0;
71 }
編譯命令: g++ -o test main.cpp -std=c++11



 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM