c++将字符串按照某种符号进行分割

    科技2025-12-24  18

    #include <iostream> #include <vector> #include <sstream>

    using namespace std; void SplitString(const string&, vector<string>&, const string&);

    int main() {     ostringstream oss;     vector<string> data;     string value = "30,45,22,34,56,99";     SplitString(value,data,","); // 按逗号将字符串分割     for(int i = 0;i != data.size();i++)     {         cout << data[i] << " ";     }     cout << endl;

        for(int i = 0;i != data.size();i++)     {         oss << data[i] << ",";     }     cout << oss.str() << endl;     return 0; }

    void SplitString(const string& s, vector<string>& v, const string& c) {      string::size_type pos1, pos2;      pos2 = s.find(c);      pos1 = 0;      while(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)); }  

    Processed: 0.011, SQL: 9