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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲?V乱码久久精品蜜桃 | 日产精品99久久久久久| 久久精品人人做人人爽电影蜜月| 久久综合狠狠综合久久激情 | 日韩欧美亚洲综合久久影院Ds| 久久精品国产2020| 久久精品国产99国产电影网| 久久这里有精品视频| 国产亚洲欧美成人久久片| 久久精品国产清自在天天线| 久久91精品国产91久久麻豆| 久久伊人精品一区二区三区| 91秦先生久久久久久久| 久久超乳爆乳中文字幕| 久久婷婷五月综合97色直播| 国产精品美女久久久网AV| 看久久久久久a级毛片| 欧美精品国产综合久久| 欧美色综合久久久久久| 国产精品99久久精品爆乳| 成人国内精品久久久久影院| 久久精品国产2020| 久久亚洲中文字幕精品一区| 思思久久99热免费精品6| 国产巨作麻豆欧美亚洲综合久久| 少妇久久久久久被弄高潮| 欧美日韩久久中文字幕| 久久人人青草97香蕉| 伊人久久大香线蕉AV一区二区| 久久精品成人一区二区三区| 精品久久久久久99人妻| 一级做a爱片久久毛片| 91久久精品国产91性色也| 精品无码久久久久久尤物| avtt天堂网久久精品| 国产成年无码久久久久毛片| 久久久国产乱子伦精品作者| 精品久久人妻av中文字幕| 久久se精品一区二区| 亚洲国产精品久久久久久| 国产成人久久精品麻豆一区 |