0%

Some tips for String

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 );//构造length个ch
string str1(10,'a');//构造十个a

string( const char *str );
string str2("hello wrold");//构造一个"hello wrold"的字符串

string( const char *str, size_type length );//以index为索引开始的子串
string str3(str2,6);//构造一个"wrold"的字符串

string( string &str, size_type index, size_type length );//以index为索引开始的子串,长度为length
string str4(str2,6,9);//构造一个"wrol"的字符串

string( input_iterator start, input_iterator end );
string str5(str2.begin(),str2.end());//构造一个"hello wrold"的子串

操作符(Operators)

1
2
3
4
5
6
7
8
9
10
>
<
>=
<=
!=
// 上面五个都是比较字符串用的
+=
=
== //判断两个字符串是否相等
[index] //获取索引为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());//我理解为string 转换为char [];

比较(compare)

1
2
3
4
5
6
7
8
9
int compare( const basic_string &str );//比较自己和str
int compare( const char *str );//比较自己和str
int compare( size_type index, size_type length, const basic_string &str );
//比较自己的子串和str,自己的子串以index索引开始,长度为length
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );
//比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
int compare( size_type index, size_type length, const char *str, size_type length2 );
//比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

拷贝(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 );//删除pos指向的字符, 返回指向下一个字符的迭代器,
iterator erase( iterator start, iterator end );//删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
basic_string &erase( size_type index = 0, size_type num = npos );//删除从index索引开始的num个字符, 返回*this.

查找(find)

1
2
3
4
5
6
7
8
9
10
11
12
13
//返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos
size_type find( const basic_string &str, size_type index );
//返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos
size_type find( const char *str, size_type index );
//返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos
size_type find( const char *str, size_type index, size_type length );
//返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos
size_type find( char ch, size_type index );
string::npos 直接输出:4294967295
用来表示不存在的意思...
例如:
string str("hello wrold");
cout<<str.find('a'); //就会打印出4294967295

下面提一下其他查找函数:

find_first_of

1
2
3
4
5
6
7
8
//查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
size_type find_first_of( const basic_string &str, size_type index = 0 );
//查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
size_type find_first_of( const char *str, size_type index = 0 );
//查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos
size_type find_first_of( const char *str, size_type index, size_type num );
//查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。
size_type find_first_of( char ch, size_type index = 0 );

find_first_not_of

1
2
3
4
5
6
7
8
//在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_first_not_of( const basic_string &str, size_type index = 0 );
//在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_first_not_of( const char *str, size_type index = 0 );
//在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
size_type find_first_not_of( const char *str, size_type index, size_type num );
//在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
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 );
//在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_last_of( const char *str, size_type index = npos );
//在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
size_type find_last_of( const char *str, size_type index, size_type num );
//在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
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 );
//在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_last_not_of( const char *str, size_type index = npos);
//在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
size_type find_last_not_of( const char *str, size_type index, size_type num );
//在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
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
//在迭代器i表示的位置前面插入一个字符ch
iterator insert( iterator i, const char &ch );
//在字符串的位置index插入字符串str
basic_string &insert( size_type index, const basic_string &str );
//在字符串的位置index插入字符串str
basic_string &insert( size_type index, const char *str );
//在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
//在字符串的位置index插入字符串str的num个字符
basic_string &insert( size_type index, const char *str, size_type num );
//在字符串的位置index插入num个字符ch的拷贝
basic_string &insert( size_type index, size_type num, char ch );
//在迭代器i表示的位置前面插入num个字符ch的拷贝,
void insert( iterator i, size_type num, const char &ch );
//在迭代器i表示的位置前面插入一段字符,从start开始,以end结束.
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;//输出hello wrold

替换(replace)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//用str中的num个字符替换本字符串中的字符,从index开始
basic_string &replace( size_type index, size_type num, const basic_string &str );
//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );
//用str中的num个字符(从index开始)替换本字符串中的字符
basic_string &replace( size_type index, size_type num, const char *str );
//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
//用num2个ch字符替换本字符串中的字符,从index开始
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
//用str中的字符替换本字符串中的字符,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const basic_string &str );
//用str中的字符替换本字符串中的字符,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str );
//用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
//用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
basic_string &replace( iterator start, iterator end, size_type num, char ch );

string str("hello wrold");
str.replace(6,3,"C++");
cout<<str;//输出hello C++ld

保留空间(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;//输出987654321

clean

1
2
清空str的元素
str.clear();

关于string大概就讲这么多…以后再被坑到再接着补充。如果读者想了解更多的话,可以去查看官方文档。
今天的总结就到这里吧…..

-------------本文结束感谢您的阅读-------------