background image

(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="Hello 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 ( ) ); // s1="Wide 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;