C++标准类库将面向对象的串的概念加入到C++语言中,预定义了字符串类(string).string类提供了对字符串进行处理的所需操作。使用string了需要包含头文件string。
string类封装了串的属性并提供了一系列允许访问这些属性的函数。
下面总结一下string类的构造函数,几个常用的成员函数和操作。、
创建string对象
字符串的操作,需要调用其构造函数,构造出一个 string 对象,为随后添加的字符分配内存,并初始化 string 对象的内部变量。
如下是它的几个常用的构造函数原型。
- string(); // 默认的构造函数,用于创建一个不包含任何非空字符的 string 对象,长度为0
例如,下面一行代码创建空的 string 对象 s .
string s;
2.string(const string& rhs); //复制构造函数
3.string(const char s); //用指针s所指向的字符串常量初始化string类的对象
将 char 数组的字符拷贝到新创建的 string 对象空间中。
例如,下面代码将字符数组 cArray 的3个字符,拷贝到 string 对象 s 中。
char* cArray="abc";
string s(cArray); // s 包含字符 'a', 'b', 'c' 和一个 null 字符
4.string(const string& s, size_type pos = 0, size_type n=npos)//拷贝构造函数,将 string 对象 s 的 [pos, pos+n) 中的字符拷贝到新创建的 string 对象空间中
当 pos取值0,n取值 string::npos 时,就是用整个字符串 s 拷贝创建一个新的 string 对象。
例如,下面代码拷贝创建 string 对象 s2
string s1;
string s2(s1);
字符添加
/ 字符的添加
string 对象具有如下几个常用的添加字符或字符串的函数,并提供了与这些添加函数相对应的更方便的字符串 "+" 和 "+=" 操作。
void push_back(char c) // 在 string 对象的字符串尾部添加一个【字符】 c
string& append(const char s) // 在 string 对象的字符串尾部添加【字符串】 s
string& append(const string& s) // 在 string 对象的字符串尾部添加 s 对象的【字符串】
iterator insert(iterator pos, const char& c) // 在 string 对象的 pos 位置之前,插入一个字符 c
下面的示例程序创建 string 对象 str1 和 str2 ,然后使用上面的函数或操作,进行字符或字符串的添加操作。
*/
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 int main()
6 {
7 string str1("abcd");
8 cout<< "str1: " << str1 << endl;
9
10 char *cArray = "hello";
11 string str2(cArray);
12 cout<< "str2: " << str2 <<endl;
13 /*字符串的 + 操作*/
14 cout<< "str1+str2: " << str1+str2 <<endl;
15 /*字符串 str1后添加字符串 str2*/
16 cout<< "str1 append str2: " << str1.append(str2) <<endl;
17 /*在 str1 的第2个字符位置前插入字符 'A'*/
18 string::iterator i;
19 i = str1.begin();
20 i++;
21 str1.insert(i,'A');
22 cout<< "str1 insert : " << str1 <<endl;
23 // 字符串的 "+=" 操作
24 str1+=str2;
25 cout<< "str1+str2: "<< str1 <<endl;
26
27 return 0;
28 }
字符添加
字符的遍历
/ 字符的遍历访问
string 字符串提供了数组操作符 “[]” ,用于访问指定索引位置处的字符。一般的字符遍历则是使用前向迭代器和反向迭代器来进行。
此时,通常要用 begin/rbegin 和 end/rend 函数找出遍历开始的首字符和末字符,然后通过迭代器的 “++” 和 “” 操作,读取字符串中的字符。
下面的示例程序,先用数组方式,将字符串的字符逐个打印出来。然后,用反向迭代器,将字符串逆序打印:
/
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 int main()
5 {
6 char* cArray = "Hello world!";
7 string str(cArray);
8 /*数组方式打印*/
9 for (size_t j=0; j<str.size(); j++)
10 cout << "第" << j << "个字符:" << str[j] << endl;
11
12 /*迭代器方式打印*/
13 string::reverse_iterator ri, rend;
14 rend = str.rend();
15 cout << "反向打印字符:" << endl;
16 for (ri=str.rbegin(); ri!=rend; ++ri)
17 cout << *ri << " ";
18 cout << endl;
19
20 return 0;
21 }
字符遍历
字符的删除
/ 字符的删除
字符串中字符的删除,可使用如下几个重载的 erase 和 clear 函数,分别删除指定位置上的若干字符或删除所有的字符。
iterator erase(iterator pos) // 删除字符串中 position 所指的字符
iterator erase(iterator first, iterator last) // 删除字符串中迭代器区间 [first, last) 上的所有字符
string& erase(size_type pos=0, size_type n=npos) // 删除字符串中从索引位置 pos 开始的 n个字符。
void clear() // 删除字符串中的所有字符
下面的程序给出了字符删除的示例:
/
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 int main()
6 {
7 char* cArray = "abcdefghijk";
8 string str(cArray);
9 // 删除第一个字符
10 str.erase(str.begin());
11 // 打印出 bcdefghijk
12 cout << str << endl;
13 // 删除操作
14 str.erase(str.begin()+3, str.end()-2);
15 // 打印出 bcdjk
16 cout << str << endl;
17
18 str.clear(); // 在VC6.0 下面又没有编译通过,VC6.0的 STL 有些老旧了
19
20 return 0;
21 }
字符删除
字符的替换
/ 字符的替换
string 提供了相当多的字符替换的重载函数 replace ,可将指定索引位置或迭代器位置处的字符替换。如下是几个常用的函数原型说明:
string& replace(size_type pos, size_type n, const char s) // 将当前字符串从 pos 索引位置开始的 n 个字符,替换为字符串 s
string& replace(size_type pos, size_type n, size_type n1, char c) // 将当前字符串从 pos 索引位置开始的 n 个字符,替换为 n1 个字符 c
string& replace(iterator first, iterator last, const char s) // 将当前字符串的 [first, last) 区间中的字符替换为字符串 s
/
1 #include <iostream>
2 #include <iostream>
3 #include <string>
4 using namespace std;
5 int main()
6 {
7 char* cArray = "hello,boy!";
8 string str(cArray);
9 // boy 替换 girl
10 str.replace(6,3,"girl");
11 // 打印 hello,girl!
12 cout << str << endl;
13
14 // 将“!”替换为 “.”
15 str.replace(10, 1, 1, '.');
16 // 打印 hello,girl.
17 cout << str << endl;
18
19 str.replace(str.begin(), str.begin()+5, "good morning");
20 // 打印 good morning girl.
21 cout << str << endl;
22
23 return 0;
24 }
字符替换
字符的搜索
/ 字符的搜索
string 提供了如下几个比较常用的在字符串中搜索匹配的字符串或字符的函数
size_type find(const char s, size_type pos=0) // 在当前字符串 pos 索引位置开始,查找子串 s ,返回找到的位置索引, -1 表示查找不到子串。
size_type find(char c, size_type pos=0) // 在当前字符串的 pos 索引位置开始,查找字符串 c 的索引位置,返回 -1 表示查找不到字符 c
size_type rfind(const char s, size_type pos=npos) // 从当前字符串的 pos 索引位置开始,反向顺序查找子串 s ,返回找到字符串的索引位置, -1 表示查找不到该子串。
size_type rfind(char c, size_type pos=npos) // 从当前字符串的 pos 索引位置开始,查找位于子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到该字符。
size_type find_first_of(const char s, size_type pos=0) // 从当前字符串的 pos 索引位置开始,查找位于子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到。
size_type find_first_not_of() //从当前字符串的 pos 索引位置开始,查找第1个不位于子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到。
size_type find_last_of() // 从当前字符串的 pos 索引位置开始,查找最后一个子串 s 的字符,返回找到的字符的索引位置, -1 表示查找不到字符。
size_type find_last_not_of() // 从当前字符串的 pos 索引位置开始,查找最后一个不位于子串 s 的字符,返回找到的字符的索引位置, -1 表示查找不到字符。
下面的示例程序,使用了以上的函数进行字符串和字符的搜索
*/
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 int main()
6 {
7 string str("dog bird chicken bird cat");
8
9 // 字符串查找
10 cout << str.find("bird") << endl; // 打印 4
11 cout << (int)str.find("pig") << endl; // 打印 -1
12
13 // 字符查找
14 cout << str.find('i', 7) << endl; // 打印 11
15
16 // 从字符串的 末尾 开始查找字符串
17 cout << str.rfind("bird") << endl; // 打印 17
18
19 // 从字符串的 末尾 开始查找字符
20 cout << str.rfind('i') << endl; // 打印 18
21
22 // 查找第1个属于某子串的字符
23 cout << str.find_first_of("13r98") << endl; // 找到字符 r, 打印 6
24
25 // 查找第1个不属于某字符串的字符
26 cout << str.find_first_not_of("dog bird 2009") << endl; // 找到字符 c , 打印 9
27
28 // 查找最后一个属于某子串的字符
29 cout << str.find_last_of("13r98") << endl; // 字符 r,打印19
30
31 // 查找最后一个不属于某字符串的字符
32 cout << str.find_last_not_of("tea") << endl; // 字符 c, 打印 22
33
34
35 return 0;
36 }
字符查找
字符串的比较
/ 字符串的比较
string 提供了字典式的比较函数 compare ,它的几个常用函数原型如下:
int compare(const string& s) // 将当前字符串与字符串 s 比较,相等返回 0 ,大于 s 返回 1 ,小于 s 返回 -1
int compare(size_type pos, size_type n, const string& s) // 将当前字符串从 pos 索引位置开始的 n 个字符构成的字符串与字符串 s 比较,相等返回 0 ,大于 s 返回 1 ,小于 s 返回 -1
int compare(const char s) // 直接将当前字符串与字符串 s 比较。相等返回 0 ,大于 s 返回 1 ,小于 s 返回 -1
下面的示例程序使用 compare 函数,比较并打印字符串比较结果
/
1 #include <string>
2 #include <iostream>
3 using namespace std;
4 int main()
5 {
6 string str1("abcdef");
7 string str2("abc");
8 // 相等,打印0
9 cout << str1.compare("abcdef") << endl;
10
11 // str1 > str2 , 打印 1
12 cout << str1.compare(str2) << endl;
13
14 // str1 < "abyz" , 打印 -1
15 cout << str1.compare("abyz") << endl;
16
17 // str1 的前3个字符 == str2 , 打印 0
18 cout << str1.compare(0, 3, str2) << endl;
19
20
21 return 0;
22 }
字符串比较
string 的其他常用函数用法
/ 其他常用函数
string 字符串还有以下几个比较常用的函数,如下是他们的使用原型说明。
size_type length() // 字符串的非空字符个数,即字符串的长度
size_type size() // 返回字符串的长度
bool empty() // 字符串是否为空,空则返回 1,否则返回 0
const char c_str() // 将string 对象转换为 c 字符数组。 (*)
/
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 int main()
5 {
6 string str;
7 // 空字符串,返回 1
8 cout << str.empty() << endl;
9 str += "1234567";
10 cout << str.empty() << endl; // 不为空,返回0
11 cout << str.size() << endl; // 7个字符,返回7
12 cout << str.length() << endl; // 返回 7
13
14 // 转换为字符数组 cArray
15 const char* cArray=str.c_str();
16 // 返回字符 3
17 cout << cArray[2] << endl;
18
19 return 0;
20 }
其他函数
原文链接: https://www.cnblogs.com/sjlove/archive/2013/05/18/3085522.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/89078
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!