分解字符串

来源:互联网 发布:腿长 知乎 编辑:程序博客网 时间:2024/05/16 19:47

#include <string>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    string test = "a@a;a@a;bb@c;cc@cd";
 string toaddress="cdd@dccd";
    vector<string> strvec;
    string strtemp;
    string::size_type pos1, pos2;
    pos2 = test.find(';');
    pos1 = 0;       
    while (string::npos != pos2)
    {
        strvec.push_back(test.substr(pos1, pos2 - pos1));
        pos1 = pos2 + 1;
        pos2 = test.find(';', pos1);
    }
    strvec.push_back(test.substr(pos1));

    vector<string>::iterator iter1 = strvec.begin(), iter2 = strvec.end();
    while (iter1 != iter2)
    {
  if((*iter1).length())
  {
   std::string::size_type poss((*iter1).find("@"));
   if(poss != std::string::npos) { //found the server beginning
    if(++poss < (*iter1).length())
     cout<<(*iter1).substr(poss, (*iter1).length()- poss)<<endl;
   }
  }
        ++iter1;
    }
   return 0;
}

原创粉丝点击