Some tips for String
笔者因为在做题时老是遇到String的坑,为此特意补一篇blog来尽量解决string 的坑….
构造函数(Constructors)
语法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| string();
string( size_type length, char ch ); string str1(10,'a');
string( const char *str ); string str2("hello wrold");
string( const char *str, size_type length ); string str3(str2,6);
string( string &str, size_type index, size_type length ); string str4(str2,6,9);
string( input_iterator start, input_iterator end ); string str5(str2.begin(),str2.end());
|
操作符(Operators)
1 2 3 4 5 6 7 8 9 10
| > < >= <= !=
+= = == [index]
|
函数
at
1 2
| at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常。
|
begin 和 end
1 2
| begin()函数返回一个迭代器,指向字符串的第一个元素. end()函数返回一个迭代器,指向字符串的最后一个元素.
|
c_str
1 2 3
| c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同. string str("hello wrold"); printf("%s",str.c_str());
|
比较(compare)
1 2 3 4 5 6 7 8 9
| int compare( const basic_string &str ); int compare( const char *str ); int compare( size_type index, size_type length, const basic_string &str );
int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );
|
拷贝(copy)
1 2
| size_type copy( char *str, size_type num, size_type index ); copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
|
empty
1 2
| bool empty(); 如果字符串为空则empty()返回真(true),否则返回假(false).
|
删除(erase)
1 2 3
| iterator erase( iterator pos ); iterator erase( iterator start, iterator end ); basic_string &erase( size_type index = 0, size_type num = npos );
|
查找(find)
1 2 3 4 5 6 7 8 9 10 11 12 13
| size_type find( const basic_string &str, size_type index );
size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index ); string::npos 直接输出:4294967295 用来表示不存在的意思... 例如: string str("hello wrold"); cout<<str.find('a');
|
下面提一下其他查找函数:
find_first_of
1 2 3 4 5 6 7 8
| size_type find_first_of( const basic_string &str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index, size_type num );
size_type find_first_of( char ch, size_type index = 0 );
|
find_first_not_of
1 2 3 4 5 6 7 8
| size_type find_first_not_of( const basic_string &str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index, size_type num );
size_type find_first_not_of( char ch, size_type index = 0 );
|
find_last_of
1 2 3 4 5 6 7
| size_type find_last_of( const basic_string &str, size_type index = npos );
size_type find_last_of( const char *str, size_type index = npos );
size_type find_last_of( const char *str, size_type index, size_type num );
size_type find_last_of( char ch, size_type index = npos );
|
find_last_not_of
1 2 3 4 5 6 7
| size_type find_last_not_of( const basic_string &str, size_type index = npos );
size_type find_last_not_of( const char *str, size_type index = npos);
size_type find_last_not_of( const char *str, size_type index, size_type num );
size_type find_last_not_of( char ch, size_type index = npos );
|
插入(insert)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| iterator insert( iterator i, const char &ch );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );
|
长度(length)
1 2
| size_type length(); length()函数返回字符串的长度,这个函数跟size()函数应该差不多
|
substr()
1 2 3 4 5
| basic_string substr( size_type index, size_type num = npos ); substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串 string str("hello wrold"); string a=str.substr(); cout<<a;
|
替换(replace)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| basic_string &replace( size_type index, size_type num, const basic_string &str );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2, size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );
string str("hello wrold"); str.replace(6,3,"C++"); cout<<str;
|
保留空间(reserve)
1 2
| void reserve( size_type num ) reserve()函数设置本字符串的capacity 以保留num个字符空间。
|
reverse
1 2 3 4 5
| 逆转元素 void reverse (BidirectionalIterator first, BidirectionalIterator last) string str("123456789"); reverse(str.begin(),str.end()); cout<<str;
|
clean
关于string大概就讲这么多…以后再被坑到再接着补充。如果读者想了解更多的话,可以去查看官方文档。
今天的总结就到这里吧…..