【轉】http://blog.programfan.com/blog.asp?blogid=2797&columnid=3755
basic_string::append
向string 的后面加字符或字符串。(比+=, push_back 更靈活)
(1)向string 的后面加C-string
basic_string& append( const value_type* _Ptr );
string s ( "Hello " ); // s=”Hello ”
const char *c = "Out There ";
s.append ( c ); // s=”Hello Out There”
(2)向string 的后面加C-string 的一部分
basic_string& append( const value_type* _Ptr, size_type _Count );
string s ( "Hello " ); // s=”Hello ”
const char *c = "Out There ";
s.append ( c , 3 ); // s=”Hello Out”
(3)向string 的后面加string(有兩種方法)
basic_string& append( const basic_string& _Str );
string s1 ( "Hello " ), s2 ( "Wide " ), s3( "World " );
s1.append ( s2 ); // s1=”Hello Wide”
s1 += s3; // s1=”Hello Wide World”
(4)向string 的后面加string 的一部分 ---A
basic_string& append( const basic_string& _Str, size_type _Off,
size_type _Count );
string s1 ( "Hello " ), s2 ( "Wide World " );
s1.append ( s2 , 5 , 5 ); // s1=”Hello World”
(5)向string 的后面加string 的一部分 ---B
template<class InputIterator> basic_string& append(
InputIterator _First, InputIterator _Last );
string str1f ( "Hello " ), str2f ( "Wide World" );
str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) );
// s1=”Hello World”
(6)向string 的后面加多個字符
basic_string& append( size_type _Count, value_type _Ch );
string str1e ( "Hello " );
str1e.append ( 4 , '!' ); // s1=”Hello !!!!”
basic_string::assign
給string 賦值。 (比“=”更靈活)
(1)向string 賦C-string
basic_string& assign( const value_type* _Ptr );
string s;
const char *c = "Out There";
s.assign ( c ); // s=”Out There”
(2)向string 賦C-string 的一部分
basic_string& assign( const value_type* _Ptr, size_type _Count );
string s;
const char *c = "Out There";
s.assign ( c , 3 ); // s=”Out”
(3)向string 賦string(有兩種方法)
basic_string& assign( const basic_string& _Str );
string s1 ( "Hello" ), s2 ( "Wide" ), s3( "World" );
s1.assign ( s2 ); // s1=”Wide”
s1 = s3; // s1=”World”
(4)向string 賦string 的一部分 ---A
basic_string& assign( const basic_string& _Str, size_type off,
size_type _Count );
string s1 ( "Hello " ), s2 ( "Wide World " );
s1.assign ( s2 , 5 , 5 ); // s1=”World”
(5)向string 賦string 的一部分 ---B
template<class InIt> basic_string& assign(
InputIterator _First,
InputIterator _Last );
string str1f ( "Hello " ), str2f ( "Wide World" );
str1f.assign ( str2f.begin ( ) + 5 , str2f.end ( ) ); // str1f=”World”
(6)向string 賦 多個字符
basic_string& assign( size_type _Count, value_type _Ch );
string str1e ( "Hello " );
str1e.assign ( 4 , '!' ); // s1=”!!!!”
basic_string::compare
如果所比較的兩個string 相等,則返回0; 操作string 大于參數string,返回
正數;操作string 小于參數string,返回負數。
(1)比較操作string 與_Str 或C-string_Ptr
int compare( const basic_string& _Str ) const;
int compare( const value_type* _Ptr ) const;
int com = s.compare ( sp );
(2)比較操作string 中_Pos1(下標)開始的_Num1 個字符 與 string_Str
比較操作string 中_Pos1(下標)開始的_Num1 個字符 與 C-string _Ptr
比較操作string 中Pos1(下標)開始的Num1 個字符 與Str 中Off(下標)開始Count 個字
符
int compare( size_type _Pos1, size_type _Num1, const basic_string& _Str );
int compare( size_type _Pos1, size_type _Num1, const value_type* _Ptr ) const;
int compare( size_type _Pos1, size_type _Num1, const basic_string& _Str,
size_type _Off, size_type _Count );
int com1 = s.compare ( 2 , 3 , sp );
int com2 = s.compare ( 2 , 3 , c );
int com3 = s.compare ( 1 , 3 , cs , 3 ,1 );
basic_string::erase
刪除string 中的一個或幾個元素。前兩個成員函數,返回要被刪除的子串的下
一個元素的iterator; 第三個函數,返回刪除后的string 的引用。
(1)刪除string 中從_First 到_Last 的字符
iterator erase( iterator _First, iterator _Last );
basic_string <char>::iterator s_Iter;
s_Iter = s.erase ( s.begin ( ) + 3 , s.end ( ) - 1 ); // s_Iter=s.end( )
(2) 刪除string 中_It 所指的字符
iterator erase( iterator _It );
s_Iter = s.erase ( s.begin ( ) + 5 );
(3) 刪除string 中從_Pos(下標)開始的_Count 個字符
basic_string& erase( size_type _Pos = 0, size_type _Count = npos );
str = s.erase ( 6 , 8 ); // str 也是string
basic_string::find
尋找給定的string。返回找到的第一個string 下標值;如果沒找到則返回npos。
(1)找一個character_Ch。(默認從頭找)
size_type find( value_type _Ch, size_type _Off = 0 ) const;
string s ( "Hello Everyone" );
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index1 = s.find ( "e" , 3 ); // index1=8,不是6
index2 = s.find ( "x" ); // index2=-1
if (indexCh1a != npos ) cout <<indexCh1a << endl;
else cout << "The character 'e' was not found in str1 ." << endl;
(2)找一個C-string。(默認從頭找)
size_type find( const value_type* _Ptr, size_type _Off = 0 ) const;
string s ( "Let me make this perfectly clear." );
basic_string <char>::size_type index;
const char *c = "perfect";
index = s.find ( c , 5 ); // index=17
(3)找一個string。(默認從頭找)
size_type find( const basic_string& _Str, size_type _Off = 0 ) const;
string s ( "clearly this perfectly unclear." );
basic_string <char>::size_type index;
string sta ( "clear" );
index = s.find ( sta , 5 ); // index=24
basic_string::max_size
返回string 能放的最大元素個數。(不同于capacity)
size_type max_size( ) const;
basic_string <char>::size_type cap, max;
cap = s.capacity ( );
max = s.max_size ( ); // max=4294967294.
basic_string::rfind
尋找給定的string。返回找到的第一個string 下標值;如果沒找到則返回npos。
與find 不同的是:rfind 默認從npos 開始找。其他相同。
basic_string::replace
將原string 中的元素或子串替換。返回替換后的string。
(1)用string 或C-string 代替操作string 中從_Pos1 開始的_Num1 個字符
basic_string& replace( size_type _Pos1,size_type _Num1, const value_type* _Ptr);
basic_string& replace(size_type _Pos1,size_type _Num1,const basic_string_Str);
string a,b;
string s ( "AAAAAAAA" );
string s1p ( "BBB" );
const char* cs1p = "CCC";
a = s.replace ( 1 , 3 , s1p ); // s=”ABBBAAAA”
b = s.replace ( 5 , 3 , cs1p ); // s=”ABBBACCC”
(2)用string 中從_Pos2 開始的_Num2 個字符,代替操作string 中從_Pos1 開始的_Num1 個字符
用C-string 中的_Num2 個字符,代替操作string 中從_Pos1 開始的_Num1 個字符
basic_string& replace( size_type _Pos1, size_type _Num1, const basic_string& _Str,
size_type _Pos2, size_type );
basic_string& replace( size_type _Pos1, size_type _Num1,
const value_type* _Ptr, size_type _Num2 );
string a, b;
string s ( "AAAAAAAA" );
string s2p ( "BBB" );
const char* cs2p = "CCC";
a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s=”ABBAAAA”
b = s.replace ( 4 , 3 , cs2p , 1 ); // s=”ABBAC”
(3)用_Count 個character_Ch ,代替操作string 中從_Pos1 開始的_Num1 個字符
basic_string& replace( size_type _Pos1, size_type _Num1,
size_type_Count, value_type_Ch );
string result;
string s ( "AAAAAAAA" );
char ch = 'C';
result = s.replace ( 1 , 3 , 4 , ch ); // s=”ACCCCAAAA”
(4)用string 或C-string ,代替操作string 中從First0 到Last0 的字符
basic_string&replace(iterator First0,iterator Last0, const basic_string& _Str);
basic_string&replace(iterator First0,iterator _Last0, const value_type* _Ptr);
string s ( "AAAAAAAA" ); string s4p ( "BBB" );
const char* cs4p = "CCC";
basic_string<char>::iterator IterF0, IterL0;
IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;
string a, b;
a = s.replace ( IterF0 , IterL0 , s4p ); // s=”BBBAAAAA”
b = s.replace ( IterF0 , IterL0 , cs4p ); // s=”CCCAAAAA”
(5)用string 中從_Pos2 開始的_Num2 個字符,代替操作string 中從First0 到Last0 的字符
用C-string 中的_Num2 個字符,代替操作string 中從First0 到Last0 的字符
basic_string& replace( iterator _First0, iterator _Last0,
const value_type* _Ptr, size_type _Num2 );
template<class InputIterator> basic_string& replace(
iterator _First0, iterator _Last0,
InputIterator _First, InputIterator _Last );
IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;
IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;
a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );
b = s.replace ( IterF1 , IterL1 , cs5p , 4 );
(6)用_Count 個character_Ch ,代替操作string 中從First0 到Last0 的字符
basic_string& replace( iterator _First0, iterator _Last0,
size_type _Count , value_type _Ch );
a = s.replace ( IterF2 , IterL2 , 4 , ch );
basic_string::swap
交換兩個string。
void swap( basic_string& _Str );
s1.swap ( s2 );
basic_string::substr
返回從_Off(下標)開始的_Count 個字符組成的string
basic_string substr( size_type _Off = 0, size_type _Count = npos ) const;
string s("I love you!"), sub;
sub=s.substr( ); // sub=”I love you!”
sub=s.substr(1); // sub=” love you!”
sub=s.substr(3,4); // sub=”ove ”