• <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) 評(píng)論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久久久久国产精品无码下载| 亚洲国产一成人久久精品| 精品国产一区二区三区久久| 久久99国产精品99久久 | 久久嫩草影院免费看夜色| 国产精品久久婷婷六月丁香| 亚洲AV日韩精品久久久久久久| 成人综合伊人五月婷久久| 久久免费视频6| 久久国产一区二区| 欧美一区二区久久精品| 99久久精品国产一区二区三区| 亚洲国产精品成人久久蜜臀| 国产午夜精品理论片久久影视| 亚洲午夜无码AV毛片久久| 久久精品国产免费一区| 一本色道久久88精品综合| 免费一级欧美大片久久网| 国产精品久久久久无码av| 久久精品成人欧美大片| 久久久久这里只有精品| 狠狠干狠狠久久| 久久精品无码一区二区无码| 伊人久久久AV老熟妇色| 美女久久久久久| 久久夜色精品国产| 久久99精品九九九久久婷婷| 99久久精品费精品国产一区二区| 精品综合久久久久久97| 久久国内免费视频| 波多野结衣久久一区二区| 亚洲а∨天堂久久精品| 日韩十八禁一区二区久久| 久久久久久极精品久久久| 精品国产热久久久福利| 亚洲伊人久久大香线蕉苏妲己| 国产成人精品免费久久久久| 99999久久久久久亚洲| 久久精品这里热有精品| 久久伊人精品青青草原高清| 国产精品一久久香蕉产线看|