• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 11,  comments - 12,  trackbacks - 0
            這是從網(wǎng)上看到的源代碼:(感覺太麻煩,請問哪位大哥能指教我一個簡單一點全面一點的分割方法嗎?十分期待)
            #include <vector>
            #include 
            <string>
            #include 
            <iostream>
            #include 
            <algorithm>
            using namespace std;

            typedef basic_string
            <char>::size_type S_T;  
            static const S_T npos = -1;  

            ////trim指示是否保留空串,默認(rèn)為保留。tok可以為任意多個字符
            vector<string> tokenize(const string& src, string tok,            
                                    
            bool trim=falsestring null_subst="")  
            {  
                
            if( src.empty() || tok.empty() ) 
                    
            throw "tokenize: empty string\0";  
                    
                vector
            <string> v;  
                S_T pre_index 
            = 0, index = 0, len = 0;  
                
            while( (index = src.find_first_of(tok, pre_index)) !=npos )  
                
            {  
                    
            if( (len = index-pre_index)!=0 )  
                        v.push_back(src.substr(pre_index, len));  
                    
            else if(trim==false)  
                        v.push_back(null_subst);  
                    pre_index 
            = index+1;  
                }
              
                
            string endstr = src.substr(pre_index);  
                
            if( trim==false ) 
                    v.push_back( endstr.empty()
            ?null_subst:endstr );  
                
            else if!endstr.empty() ) 
                    v.push_back(endstr);  
                
            return v;  
            }
             
            //delimit為一個字符,嚴(yán)格分割
            vector<string> split(const string& src, string delimit, string null_subst="")  
            {  
                
            if( src.empty() || delimit.empty() ) 
                    
            throw "split:empty string\0";    
                vector
            <string> v;  
                S_T deli_len 
            = delimit.size();  
                
            long index = npos, last_search_position = 0;  
                
            while( (index=src.find(delimit,     
                    last_search_position))
            !=npos )  
                
            {  
                    
            if(index==last_search_position)  
                        v.push_back(null_subst);  
                    
            else  
                        v.push_back( src.substr(last_search_position, index
            -   
                        last_search_position) );  
                    last_search_position 
            = index + deli_len;  
                }
              
                
            string last_one = src.substr(last_search_position);  
                v.push_back( last_one.empty()
            ? null_subst:last_one );  
                
            return v;  
            }
               

            //測試如下:
            int main(int argc, char* argv[])
            {
                
            string src = ",ab,cde;,,fg,," ;  
                
            string tok = ",;" ;   
                vector
            <string> v1 = tokenize(src, tok ,true);  
                vector
            <string> v2 = tokenize(src, tok ,false,     
                    
            "<null>");    
                cout
            <<"-------------v1:"<<endl;  
                
            for(int i=0; i<v1.size();i++)  
                
            {  
                    cout
            <<v1[i]<<endl;  
                }
               
                cout
            <<"-------------v2:"<<endl;  
                
            for(int j=0; j<v2.size();j++)  
                
            {  
                    cout
            <<v2[j].c_str()<<endl;  
                }
              
                
            try{  
                    
                    
            string s = "1;2;3;4";  
                    
            string del = ";";//"###";  
                    vector<string> v3 = split(s, del, "<null>");  
                    cout
            <<"-------------v3:"<<endl;  
                    
            for(int k=0; k<v3.size();k++)  
                    
            {  
                        cout
            <<v3[k].c_str()<<endl;  
                    }
              
                }
              
                
            catch (char *s) {  
                    cout
            <<s<<endl;  
                }

                
            return 0;
            }

            posted on 2009-08-07 14:24 人生在于攀登! 閱讀(2929) 評論(3)  編輯 收藏 引用

            FeedBack:
            # re: 求標(biāo)準(zhǔn)C++字符串string分割
            2009-08-07 22:35 | Sunshine Alike
            日志里有代碼功能,為啥不用?
            你這樣的東西放在上面沒人愿意看了  回復(fù)  更多評論
              
            # re: 求標(biāo)準(zhǔn)C++字符串string分割
            2009-08-09 07:18 | 人生在于攀登!
            哦,謝謝提醒啊,以前沒有注意到!@Sunshine Alike
              回復(fù)  更多評論
              
            # re: 求標(biāo)準(zhǔn)C++字符串string分割[未登錄]

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2009年8月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            精品国产青草久久久久福利| 浪潮AV色综合久久天堂| 久久午夜夜伦鲁鲁片免费无码影视 | 久久成人18免费网站| 99久久精品国产一区二区三区| 狠狠精品干练久久久无码中文字幕| AV无码久久久久不卡蜜桃| 狠狠色丁香婷综合久久| 国产精品狼人久久久久影院 | 国内精品人妻无码久久久影院导航| 久久这里只有精品首页| 久久婷婷国产剧情内射白浆| 色诱久久av| www性久久久com| 久久亚洲AV成人无码国产| 精品久久久久中文字幕日本| WWW婷婷AV久久久影片| 久久99热这里只有精品国产| 久久久WWW成人免费毛片| 国内精品伊人久久久久AV影院| AV狠狠色丁香婷婷综合久久 | 久久精品国产福利国产秒| 国产精品va久久久久久久| 久久综合伊人77777麻豆| 69SEX久久精品国产麻豆| 久久亚洲日韩看片无码| 久久夜色精品国产欧美乱| 久久免费美女视频| 精品熟女少妇a∨免费久久| 日本免费久久久久久久网站| 伊人久久大香线蕉综合网站 | 日本久久久精品中文字幕| 久久人人爽人人爽人人片AV东京热| 亚洲国产成人久久一区WWW| 色欲久久久天天天综合网| 伊人久久大香线蕉成人| 国产精品美女久久久久av爽| 亚洲国产精品成人久久蜜臀 | 欧美激情一区二区久久久| 婷婷综合久久狠狠色99h| 国产精品久久久久久久人人看 |