当前位置:编程学习 > C/C++ >>

sting split

print?#include <string>  
#include <iostream>  
#include <vector>  
 
 
using namespace std; 
 
 
int main() 

        string test = "aa aa bbc cccd"; 
        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) 
        { 
                cout << *iter1 << endl; 
                ++iter1; 
        }  
         
        return 0; 

#include <string>
#include <iostream>
#include <vector>


using namespace std;


int main()
{
        string test = "aa aa bbc cccd";
        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)
        {
                cout << *iter1 << endl;
                ++iter1;
        }
       
        return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,