您的位置:首页 > 编程语言 > C语言/C++

c++中的string的用法总结

2014-07-01 09:07 337 查看
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 "
你可能不知道的一些用法
toupper, tolower
地球人都知道 C++ 的 string 没有 toupper ,好在这不是个大问题,因为我们有 STL 算法:
string s("heLLo");
transform(s.begin(), s.end(), s.begin(), toupper);
cout << s << endl;
transform(s.begin(), s.end(), s.begin(), tolower);
cout << s << endl;
当然,我知道很多人希望的是 s.to_upper() ,但是对于一个这么通用的 basic_string 来说,的确没办法把这些专有的方法放进来。如果你用 boost stringalgo ,那当然不在话下,你也就不需要读这篇文章了。
------------------------------------------------------------------------
trim
我们还知道 string 没有 trim ,不过自力更生也不困难,比 toupper 来的还要简单:
    string s("   hello   ");
    s.erase(0, s.find_first_not_of(" \n"));
    cout << s << endl;
    s.erase(s.find_last_not_of('' '') + 1);
    cout << s << endl;
注意由于 find_first_not_of 和 find_last_not_of 都可以接受字符串,这个时候它们寻找该字符串中所有字符的 absence ,所以你可以一次 trim 掉多种字符。
-----------------------------------------------------------------------
erase
string 本身的 erase 还是不错的,但是只能 erase 连续字符,如果要拿掉一个字符串里面所有的某个字符呢?用 STL 的 erase + remove_if 就可以了,注意光 remove_if 是不行的。
    string s("   hello, world. say bye   ");
    s.erase(remove_if(s.begin(),s.end(),
        bind2nd(equal_to<char>(), '' '')),
    s.end());
上面的这段会拿掉所有的空格,于是得到 hello,world.saybye。
-----------------------------------------------------------------------
replace
string 本身提供了 replace ,不过并不是面向字符串的,譬如我们最常用的把一个 substr 换成另一个 substr 的操作,就要做一点小组合:
    string s("hello, world");
    string sub("ello, ");
    s.replace(s.find(sub), sub.size(), "appy ");
    cout << s << endl;
输出为 happy world。注意原来的那个 substr 和替换的 substr 并不一定要一样长。
-----------------------------------------------------------------------
startwith, endwith
这两个可真常用,不过如果你仔细看看 string 的接口,就会发现其实没必要专门提供这两个方法,已经有的接口可以干得很好:
    string s("hello, world");
    string head("hello");
    string tail("ld");
    bool startwith = s.compare(0, head.size(), head) == 0;
    cout << boolalpha << startwith << endl;
    bool endwith = s.compare(s.size() - tail.size(), tail.size(), tail) == 0;
    cout << boolalpha << endwith << endl;
当然了,没有 s.startwith("hello") 这样方便。
------------------------------------------------------------------------
toint, todouble, tobool...
这也是老生常谈了,无论是 C 的方法还是 C++ 的方法都可以,各有特色:
    string s("123");
    int i = atoi(s.c_str());
    cout << i << endl;
   
    int ii;
    stringstream(s) >> ii;
    cout << ii << endl;
   
    string sd("12.3");
    double d = atof(sd.c_str());
    cout << d << endl;
    double dd;
    stringstream(sd) >> dd;
    cout << dd << endl;
   
    string sb("true");
    bool b;
    stringstream(sb) >> boolalpha >> b;
    cout << boolalpha << b << endl;
C 的方法很简洁,而且赋值与转换在一句里面完成,而 C++ 的方法很通用。
------------------------------------------------------------------------
split
这可是件麻烦事,我们最希望的是这样一个接口: s.split(vect, '','') 。用 STL 算法来做有一定难度,我们可以从简单的开始,如果分隔符是空格、tab 和回车之类,那么这样就够了:
    string s("hello world, bye.");
    vector<string> vect;
    vect.assign(
        istream_iterator<string>(stringstream(s)),
        istream_iterator<string>()
    );
不过要注意,如果 s 很大,那么会有效率上的隐忧,因为 stringstream 会 copy 一份 string 给自己用。
------------------------------------------------------------------------
concat
把一个装有 string 的容器里面所有的 string 连接起来,怎么做?希望你不要说是 hand code 循环,这样做不是更好?
    vector<string> vect;
    vect.push_back("hello");
    vect.push_back(", ");
    vect.push_back("world");
   
    cout << accumulate(vect.begin(), vect.end(), string(""));
不过在效率上比较有优化余地。
-------------------------------------------------------------------------
reverse
其实我比较怀疑有什么人需要真的去 reverse 一个 string ,不过做这件事情的确是很容易:
std::reverse(s.begin(), s.end());
上面是原地反转的方法,如果需要反转到别的 string 里面,一样简单:
s1.assign(s.rbegin(), s.rend());
效率也相当理想。
-------------------------------------------------------------------------
解析文件扩展名
字数多点的写法:
    std::string filename("hello.exe");
    std::string::size_type pos = filename.rfind(''.'');
    std::string ext = filename.substr(pos == std::string::npos ? filename.length() : pos + 1);
不过两行,合并成一行呢?也不是不可以:
    std::string ext = filename.substr(filename.rfind(''.'') == std::string::npos ? filename.length() : filename.rfind(''.'') + 1);
我知道,rfind 执行了两次。不过第一,你可以希望编译器把它优化掉,其次,扩展名一般都很短,即便多执行一次,区别应该是相当微小。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: