c++之split

 
 
  1. #include <stdio.h> 
  2. #include <string.h> 
  3.  
  4. int main () 
  5.   char str[] ="- This, a sample string."; 
  6.   char * pch; 
  7.   printf ("Splitting string \"%s\" into tokens:\n",str); 
  8.   pch = strtok (str," ,.-"); 
  9.   while (pch != NULL) 
  10.   { 
  11.     printf ("%s\n",pch); 
  12.     pch = strtok (NULL, " ,.-"); 
  13.   } 
  14.   return 0; 

 

 

 
 
  1. #include <iostream> 
  2. #include <sstream> 
  3. #include <string> 
  4. using namespace std; 
  5.  
  6. int main() 
  7.     string s("Somewhere down the road"); 
  8.     istringstream iss(s); 
  9.  
  10.     do 
  11.     { 
  12.         string sub; 
  13.         iss >> sub; 
  14.         cout << "Substring: " << sub << endl
  15.     } while (iss); 
  16.  
  17.     return 0; 

 

 

 
 
  1. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { 
  2.     std::stringstream ss(s); 
  3.     std::string item; 
  4.     while(std::getline(ss, item, delim)) { 
  5.         elems.push_back(item); 
  6.     } 
  7.     return elems; 
  8.  
  9.  
  10. std::vector<std::string> split(const std::string &s, char delim) { 
  11.     std::vector<std::string> elems; 
  12.     return split(s, delim, elems); 


 

 
 
  1. #include <vector> 
  2. #include <string> 
  3. #include <sstream> 
  4.  
  5. using namespace std; 
  6.  
  7. int main() 
  8.     string str("Split me by whitespaces"); 
  9.     string buf; // Have a buffer string 
  10.     stringstream ss(str); // Insert the string into a stream 
  11.  
  12.     vector<string> tokens; // Create vector to hold our words 
  13.  
  14.     while (ss >> buf) 
  15.         tokens.push_back(buf); 

源地址:http://ghostjay.blog.51cto.com/2815221/927439


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值