c++中的string用法

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= 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;
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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值