string分割



#include <string>
#include <vector>
using std::string;  //使用string對象
using std::vector;  //使用vector
 
 
 
void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest);//函數原型
 
 
void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest) //字符串分割到數組
{
 
        //參數1:要分割的字符串;參數2:作為分隔符的字符;參數3:存放分割后的字符串的vector向量
 
 string str = src;
 string substring;
 string::size_type start = 0, index;
 dest.clear();
 index = str.find_first_of(separator,start);
 do
 {
  if (index != string::npos)
  {   
   substring = str.substr(start,index-start );
   dest.push_back(substring);
   start =index+separator.size();
   index = str.find(separator,start);
   if (start == string::npos) break;
  }
 }while(index != string::npos);
 
 //the last part
 substring = str.substr(start);
 dest.push_back(substring);
}


免責聲明!

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



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