c++中的string常用函数用法
basic_string::append
      string 的后面加字符或字符串。(+=, push_back 更灵活)
(1)string 的后面加C-string
basic_string& append( const value_type* _Ptr );字符串拷贝函数strcpy作用
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 , d ( ) );
// 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 , d ( ) ); // 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 = spare ( 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 = spare ( 2 , 3 , sp );
int com2 = spare ( 2 , 3 , c );
int com3 = spare ( 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;