String
字符串
C语言从本质上来说,是没有字符串这种类型的,在C语言中如果要表达字符串,只能间接地借助于字符指针或者字符数组来表达,很明显这是由于C语言的诞生年代过于久远而导致的一种设计缺陷。
在C++中,字符串就跟整型、浮点型数据一样,是系统原生支持的一种基本数据类型。
有了字符串string这种类型之后,很多对字符串的操作就变得非常简单而直观了,C++对string类型 的支持包括:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
string s1 = "abdc";
// 定义字符串 s1,内容是 abdc
string s2;
s2 = s1;
// 把 s1 复制给 s2,s2 也变成 abdc
string s3;
s3 = "xyz" + s1;
// s3 = xyz + abdc → xyzabdc
s3 += "123";
// s3 后面追加 123 → xyzabdc123
if(s1 == "abcd") …
// 直接比较字符串是否相等、大小
s1[1] = 'B';
// 把字符串第 2 个字符改成 B,变成 aBdc
|
常用的接口
构造函数(Constructors)
构造函数,用于字符串初始化
1
2
3
4
5
6
|
string();
string( size_type length,char ch); //拷贝length个的ch
string( const char *str ); //填一任意的字符串
string( const char *str, size_type length ); //字符串str 中,取前length个字符创建字符串
string( string &str, size_type index, size_type length ); //从字符串中的第index的索引处开始,取length长度的字符创建字符串
string( input_iterator start, input_iterator end ); //以迭代器区间[start, end) 为源,创建字符串
|
案例
1
2
3
4
5
6
7
8
9
10
11
12
|
string str1( 5, 'c' );
string str2( "Now is the time..." );
string str3( str2, 11, 4 );
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
显示
ccccc
Now is the time...
time
|
操作符(Operators)
1
2
3
4
5
6
7
8
9
|
==
>
<
>=
<=
!=
+
+=
[]
|
可以用 ==, >, <, >=, <=, and !=比较字符串. 可以用 + 或者 += 操作符连接两个字符串, 并且可以用[]获取特定的字符.
添加文本(append)
就是追加,在字符串后面添加想要的数据
1
2
3
4
|
basic_string &append( const basic_string &str );// 追加字符串
basic_string &append( const char *str ); // 追加 char*
basic_string &append( size_type num, char ch );// 追加 n 个字符
basic_string &append( const basic_string &str, size_type index, size_type len ); // 截取子串追加
|
案例
1
2
3
4
5
6
|
string str = "Hello World";
str.append( 10, '!' );
cout << str << endl;
显示
Hello World!!!!!!!!!!
|
赋值(assign)
就是重新赋值
1
2
3
4
5
|
basic_string &assign( const basic_string &str ); // 用str为字符串赋值
basic_string &assign( const char *str ); // 用str为字符串赋值
basic_string &assign( const char *str, size_type num ); //使用str中开始num个字符串进行赋值
basic_string &assign( const basic_string &str, size_type index, size_type len ); //使用str中第index处开始,截取len长度的字符串进行赋值
basic_string &assign( size_type num, char ch ); //用num个ch的字符串进行赋值
|
案例
1
2
3
4
5
6
7
8
9
10
11
|
// 1. 赋值整个字符串
s.assign(str);
// 2. 赋值 C 风格字符串
s.assign("hello");
// 3. 赋值 n 个相同字符
s.assign(5, 'a'); // → "aaaaa"
// 4. 截取子串赋值
s.assign(str, index, len);
|
引用(at )
1
|
reference at( size_type index ); //引用字符串第index出的字符
|
如果index不在字符串中,会报错"out of range"错误
案例
1
2
3
4
|
string text = "ABCDEF";
char ch = text.at( 2 );
显示字符 'C'.
|
开始迭代器(begin)
迭代器:就是指向某个字符的指针
1
|
iterator begin(); //begin()函数返回一个迭代器,指向字符串的第一个元素
|
结束迭代器(end)
1
|
iterator end(); //end()函数返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置)
|
转换为C语言风格字符串(c_str)
1
|
const char *c_str(); //返回一个指向正规C字符串的指针
|
容量(capacity)
1
|
size_type capacity(); //获取当前字符串所能容纳字符串的容量
|
比较(compare)
1
2
3
|
int compare(const string &str); // 整个字符串比较
int compare(const char *str); // 比较 C 字符串
int compare(size_t index, size_t length, const string &str); // 截取子串比较
|
拷贝(copy)
1
|
size_type copy( char *str, size_type num, size_type index );
|
拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
data
反回指向自己的第一个字符的指针.
空字符串(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个字符
|
查找(find)
1
2
3
4
|
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 );
|
插入(insert)
1
2
3
4
|
insert(size_type index, const string &str); //在下标index 处插入 字符串str
insert(size_type index, const char *str); //在下标index处插入 C字符串
insert(size_type index, size_type num, char ch); //在下标index 处插入 num 个字符 ch
insert(size_type index1, const string &str, size_type index2, size_type num); // 在下标index1 处插入 str 的子串(从 index2 开始,取 num 个)
|
长度(length)
返回字符串的长度. 这个数字应该和size()返回的数字相同
逆向迭代器
1
|
const reverse_iterator rbegin();
|
返回一个逆向迭代器,指向字符串的最后一个字符。
1
|
const reverse_iterator rend();
|
返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)。
保留空间(reserve)
1
|
void reserve( size_type num );
|
设置本字符串的capacity 以保留num个字符空间。
重新设置大小(resize)
1
2
|
void resize( size_type num );
void resize( size_type num, char ch );
|
改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充。
交换(swap)
1
|
void swap( basic_string &str ); //把str和本字符串进行交换
|
案例
1
2
3
4
5
6
7
8
9
10
|
string first( "This comes first" );
string second( "And this is second" );
first.swap( second );
cout << first << endl;
cout << second << endl;
显示:
And this is second
This comes first
|