• <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 閱讀(1178) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久777色| 99国产精品久久久久久久成人热| 久久不射电影网| 国产一久久香蕉国产线看观看| 9191精品国产免费久久| 色播久久人人爽人人爽人人片aV| 国产精品久久久久a影院| 国产精品gz久久久| 精品久久久久久无码专区不卡| 狠狠色婷婷综合天天久久丁香| 久久伊人五月天论坛| 久久A级毛片免费观看| 久久人人爽人人精品视频| 久久久久久毛片免费播放| 欧美大战日韩91综合一区婷婷久久青草| 久久精品中文无码资源站| 国产精品免费看久久久香蕉| 中文字幕久久精品无码| 国产激情久久久久影院| 国内精品久久久久影院一蜜桃| 久久久中文字幕日本| 久久综合九色综合97_久久久| 色妞色综合久久夜夜| 久久国产三级无码一区二区| 久久婷婷五月综合97色 | 99久久无码一区人妻a黑| 日韩va亚洲va欧美va久久| 久久婷婷久久一区二区三区| 久久久久免费看成人影片| 囯产极品美女高潮无套久久久 | 久久亚洲国产午夜精品理论片| 久久中文字幕人妻熟av女| 日韩久久久久中文字幕人妻 | 一本久久a久久精品综合香蕉| 91亚洲国产成人久久精品网址| 99久久婷婷免费国产综合精品| 国产成人精品免费久久久久| 奇米影视7777久久精品| 精品久久久久久无码中文字幕一区| 久久精品国产亚洲av麻豆蜜芽| 亚洲综合伊人久久大杳蕉|