• <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>

            Problem Solving using C++

            Algorithm Study using C++

            常用字符串string操作--find

            #include <iostream>
            #include 
            <string>
            #include 
            <cctype>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            <iterator>

            using namespace std;

            int main(int argc,char** argv[])
            {
                string line1
            ="We were her pride of 10 she named us:";
                string line2
            ="Benjamin, Phoenix, the Prodigal";
                string line3
            ="and perspicacious pacific Suzanne";
                string sentence 
            = line1+' '+line2+' '+line3;
                
                string separator(
            " \n\t:,\r\v\f");
                vector
            <string> longest,shortest;
                
            int num = 0;
                string::size_type startpos
            =0,endpos=0;
                string word;
                
            int longLen=0,shortLen=-1,wordlen;
                
                
            while((startpos=sentence.find_first_not_of(separator,endpos))!=string::npos)
                {
                    
            ++num;
                    
                    endpos
            =sentence.find_first_of(separator,startpos);
                    
            if(endpos==string::npos)
                    {
                        wordlen 
            = sentence.size()-startpos;
                    }
                    
            else
                    {
                        wordlen 
            = endpos-startpos;
                    }
                    
                    word.assign(sentence.begin()
            +startpos,sentence.begin()+wordlen+startpos);
                    
                    startpos 
            = sentence.find_first_not_of(separator,endpos);
                    
                    
            if(shortLen==-1)
                    {
                        shortLen
            =longLen=wordlen;
                        shortest.push_back(word);
                        longest.push_back(word);
                        
                        
            continue;
                    }
                    
            if(shortLen==wordlen)
                    {
                        shortest.push_back(word);
                    }
                    
            if(shortLen>wordlen)
                    {
                        shortest.clear();
                        shortest.push_back(word);
                        shortLen 
            = wordlen;
                    }
                    
            if(wordlen==longLen)
                    {
                        longest.push_back(word);
                    }
                    
            if(wordlen>longLen)
                    {
                        longest.clear();
                        longest.push_back(word);
                        longLen
            =wordlen;
                    }    
                }
                
                cout
            <<"Words:"<<num<<endl;
                cout
            <<"Shortest:"<<shortLen<<endl;
                copy(shortest.begin(),shortest.end(),ostream_iterator
            <string>(cout," "));
                cout
            <<endl;
                cout
            <<"Longest:"<<longLen<<endl;
                copy(longest.begin(),longest.end(),ostream_iterator
            <string>(cout," "));
                cout
            <<endl;
                
                system(
            "pause");
                
            return 0;
            }
            #include <iostream>
            #include 
            <string>
            #include 
            <cctype>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            <iterator>

            using namespace std;

            void str_replace(string& str,const string& src,const string& dst)
            {
                string::size_type pos 
            = 0;
                
            int srclen = src.size();
                
            int dstlen = dst.size();
                
                
            while((pos = str.find(src,pos))!=string::npos)
                {
                    
            //str.replace(pos,srclen,dst);
                    str.erase(pos,srclen);
                    str.insert(pos,dst);
                    pos
            +=dstlen;
                }
            }

            int main(int argc,char** argv[])
            {
                
            //replace/erase/insert
                string str("I like apple,what about you? apple tastes great!");
                cout
            <<str<<endl;
                str_replace(str,
            "apple","banana");
                cout
            <<str<<endl;
                
                
            //assign/append
                string q1("When lilacs last in the dooryard bloom'd");
                string q2(
            "The child is father of the man");
                string sentence;
                
                sentence.assign(q2.begin(),q2.begin()
            +13);
                sentence.append(q1.substr(q1.find(
            "in"),15));
                cout
            <<sentence<<endl;
                
                system(
            "pause");
                
            return 0;
            }

            posted on 2007-08-29 11:12 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(1179) 評(píng)論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            精品欧美一区二区三区久久久 | 欧美日韩精品久久久久| 狠狠综合久久综合88亚洲| 国产精品一区二区久久精品无码 | 久久精品国产精品亜洲毛片 | 久久99久久成人免费播放| 久久精品国产69国产精品亚洲 | 777米奇久久最新地址| 久久人人爽人人爽人人AV| 亚洲精品tv久久久久久久久| 狠狠色丁香久久婷婷综合_中 | 少妇熟女久久综合网色欲| 日韩影院久久| 久久精品aⅴ无码中文字字幕不卡| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 无码8090精品久久一区| 亚洲人成无码久久电影网站| 狠狠综合久久综合88亚洲| 午夜久久久久久禁播电影| 欧美熟妇另类久久久久久不卡| 人妻精品久久无码专区精东影业| 久久精品亚洲精品国产色婷| 久久久久久人妻无码| 国产精品久久影院| 久久久久亚洲AV成人网| 久久久亚洲AV波多野结衣 | 无码人妻少妇久久中文字幕蜜桃| 久久亚洲AV成人出白浆无码国产| 免费精品99久久国产综合精品| 色8激情欧美成人久久综合电| 国内精品伊人久久久久妇| 69久久夜色精品国产69| 久久久精品无码专区不卡| 伊人久久大香线蕉综合Av| 99久久免费国产精品| 欧美精品国产综合久久| 91精品国产91久久| 97精品国产97久久久久久免费| 岛国搬运www久久| 欧美一区二区三区久久综合| 手机看片久久高清国产日韩|