#include<iostream>
#include<string>
using namespace std;
int main()
{
	string sa,sb,s;
	while(getline(cin,s))
	{
		getline(cin,sa);
		getline(cin,sb);
		int begin=0;
		begin=s.find(sa,begin);
		int l=sa.length();
		while(begin!=-1)
		{
			s.replace(begin,l,sb);
			begin=s.find(sa,begin);
		}
		cout<<s<<endl;
	}
	return 0;
}

重点:getline(),find(),replace()函数

1.一个有用的string IO操作:getling。这个函数接受两个参数:一个输入流对象和一个string对象。getline函数从输入流的下一行读取,并保存读取的内容到string中,但不包括换行符。和输入操作符不一样的是,getline并不忽略行开头的换行符。只要getline遇到换行符,即便它是输入的第一个字符,getline也将停止读入并返回。如果第一个字符就是换行符,则string参数将被置为空string。

getline函数将istream参数作为返回值,和输入操作符一样也把它用作判断条件。例如:

int main()
{
string line;
// read line at time until end-of-file
while (getline(cin, line))
   cout << line << endl;
return 0;
}

由于line不含换行符,若要逐行输出需要自行添加。照常,用endl来输出一个换行符来刷新输出缓冲区。

注解:由于getline函数返回时丢弃换行符,换行符将不会存储在string对象中。

2.find()

  1.  ////find函数返回类型 size_type  
  2. string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");  
  3. string flag;  
  4. string::size_type position;  
  5.   
  6. //find 函数 返回jk 在s 中的下标位置   
  7. position = s.find("jk");  
  8.  if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,  
  9.  {  
  10.   cout << "position is : " << position << endl;  
  11.  }  
  12.  else  
  13.  {  
  14.   cout << "Not found the flag" + flag;  
  15.  }   
  1. //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置  
  2.  flag = "c";  
  3.  position = s.find_first_of(flag);  
  4.  cout << "s.find_first_of(flag) is : " << position << endl;  
  1. //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标  
  2. position=s.find("b",5);  
  3. cout<<"s.find(b,5) is : "<<position<<endl;  

 

  1. //查找s 中flag 出现的所有位置。  
  2.  flag="a";  
  3.  position=0;  
  4.  int i=1;  
  5.  while((position=s.find_first_of(flag,position))!=string::npos)  
  6.  {  
  7.   //position=s.find_first_of(flag,position);  
  8.   cout<<"position  "<<i<<" : "<<position<<endl;  
  9.   position++;  
  10.   i++;  
  11.  }  
  1.    
  1. //查找flag 中与s 第一个不匹配的位置  
  2. flag="acb12389efgxyz789";  
  3. position=flag.find_first_not_of (s);  
  4. cout<<"flag.find_first_not_of (s) :"<<position<<endl;  
  1.    
  1.  //反向查找,flag 在s 中最后出现的位置  
  2.  flag="3";  
  3.  position=s.rfind (flag);  
  4.  cout<<"s.rfind (flag) :"<<position<<endl;  
3.replace()函数

  1. basic_string::max_size  

C++ replace()函数返回string 能放的最大元素个数。(不同于capacity)

  1. size _ type max _ size( ) const;   
  2. basic_string <char>::size_type cap, max;   
  3. cap = s.capacity ( );   
  4. max = s.max_size ( ); // max=4294967294.   
  5. basic_string::rfind  

寻找给定的string。返回找到的第一个string 下标值;如果没找到则返回npos。

与find 不同的是:rfind 默认从npos 开始找。其他相同。

  1. basic_string::replace  

将原string 中的元素或子串替换。返回替换后的string。

(1)用string 或C-string 代替操作string 中从 _Pos1 开始的 _Num1 个字符

  1. basic _ string& replace( size _ type _Pos1 ,
    size _ type _Num1 , const value _ type* _Ptr );   
  2. basic _ string& replace(size _ type _Pos1 ,
    size _ type _Num1 ,const basic _ string _Str );   
  3. string a,b;   
  4. string s ( "AAAAAAAA" );   
  5. string s1p ( "BBB" );   
  6. const char* cs1p = "CCC" ;   
  7. a = s.replace ( 1 , 3 , s1p ); // s= ” ABBBAAAA ”   
  8. b = s.replace ( 5 , 3 , cs1p ); // s= ” ABBBACCC ”  

(2)用C++ replace()函数中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

用C-string 中的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

  1. basic _ string& replace( size _ type _Pos1 , 
    size _ type _Num1 , const basic _ string& _Str ,   
  2. size _ type _Pos2 , size _ type );   
  3. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   
  4. const value _ type* _Ptr , size _ type _Num2 );   
  5. string a, b;   
  6. string s ( "AAAAAAAA" );   
  7. string s2p ( "BBB" );   
  8. const char* cs2p = "CCC";   
  9. a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= ” ABBAAAA ”   
  10. b = s.replace ( 4 , 3 , cs2p , 1 ); // s= ” ABBAC ”  

(3)用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符

  1. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   
  2. size _ type _Count , value _ type _Ch );   
  3. string result;   
  4. string s ( "AAAAAAAA" );   
  5. char ch = 'C';   
  6. result = s.replace ( 1 , 3 , 4 , ch ); // s= ” ACCCCAAAA ”  

(4)用string 或C-string ,代替操作string 中从 First0 到 Last0 的字符

  1. basic _ string&replace(iterator First0 ,iterator Last0 , 
    const basic _ string& _Str );   
  2. basic _ string&replace(iterator First0 ,iterator _Last0 , 
    const value _ type* _Ptr );   
  3. string s ( "AAAAAAAA" ); string s4p ( "BBB" );   
  4. const char* cs4p = "CCC";   
  5. basic_string<char>::iterator IterF0, IterL0;   
  6. IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;   
  7. string a, b;   
  8. a = s.replace ( IterF0 , IterL0 , s4p ); // s= ” BBBAAAAA ”   
  9. b = s.replace ( IterF0 , IterL0 , cs4p ); // s= ” CCCAAAAA ”  

(5)用C++ replace()函数中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

用C-string 中的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. const value _ type* _Ptr , size _ type _Num2 );   
  3. template<class InputIterator> basic _ string& replace(   
  4. iterator _First0 , iterator _Last0 ,   
  5. InputIterator _First , InputIterator _Last );   
  6. IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;   
  7. IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;   
  8. a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );   
  9. b = s.replace ( IterF1 , IterL1 , cs5p , 4 );  

(6)用 _Count 个character _Ch , 代替操作string 中从 First0 到 Last0 的字符

  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. size _ type _Count , value _ type _Ch );   
  3. a = s.replace ( IterF2 , IterL2 , 4 , ch );   
  4. basic_string::swap  

交换两个string。

  1. void swap( basic _ string& _Str );   
  2. s1.swap ( s2 );   
  3. basic_string::substr  

返回从 _Off ( 下标)开始的 _Count 个字符组成的string

  1. basic _ string substr( size _ type _Off = 0
    size _ type 
    _Count = npos ) const;   
  2. string s("I love you!") , sub;   
  3. ssub=s.substr( ); // sub= ” I love you! ”   
  4. ssub=s.substr(1); // sub= ” love you! ”   
  5. ssub=s.substr(3,4); // sub= ” ove ”  


 posted on 2014-04-29 18:18  mnmlist1  阅读(142)  评论(0编辑  收藏  举报