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 阅读(1154) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


My Links

Blog Stats

常用链接

留言簿(1)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜