目录
string 类型支持长度可变的字符串,C++ 标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作。标准库 string 类型的目的就是满足对字符串的一般应用。与其他的标准库类型一样,用户程序要使用 string 类型对象,必须包含相关头文件。
string 对象的定义和初始化
string 标准库支持几个构造函数。构造函数是一个特殊成员函数,定义如何初始化该类型的对象。下面列出了几个 string 类型常用的构造函数。当没有明确指定对象初始化式时,系统将使用默认构造函数。
string 对象的操作
string::size_type 类型
事实上,size 操作返回的是 string::size_type 类型的值。
string 类类型和许多其他库类型都定义了一些配套类型。通过这些配套类型,库类型的使用就能与机器无关。size_type 就是这些配套类型中的一种。它定义为与 unsigned 型(unsigned int 或 unsigned long)具有相同的含义,而且可以保证足够大能够存储任意 string 对象的长度。为了使用由 string 类型定义的 size_type 类型是由 string 类定义。
任何存储 string 的 size 操作结果的变量必须为 string::size_type 类型。特别重要的是,还要把 size 的返回值赋给一个 int 变量。
虽然我们不知道 string::size_type 的确切类型,但可以知道它是 unsigned 型。对于任意一种给定的数据类型,它的 unsigned 型所能表示的最大正数值比对应的 signed 型要大。这个事实表明 size_type 存储的 string 长度是 int 所能存储的两倍。
使用 int 变量的另一个问题是,有些机器上 int 变量的表示范围太小,甚至无法存储实际并不长的 string 对象。如在有 16 位 int 型的机器上,int 类型变量最大只能表示 32767 个字符的 string 个字符的 string 对象。而能容纳一个文件内容的 string 对象轻易就会超过这个数字。因此,为了避免溢出,保存一个 stirng 对象 size 的最安全的方法就是使用标准库类型 string::size_type。
插入(insert)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.insert(pos,n,ch) 在字符串s的pos位置上面插入n个字符ch
str.insert(6,4,'z'); // str = "hello zzzzworld"
//s.insert(pos,str) 在字符串s的pos位置插入字符串str
str.insert(6,str2); // str = "hello hard world"
//s.insert(pos,str,a,n) 在字符串s的pos位置插入字符串str中位置a到后面的n个字符
str.insert(6,str3,6,9); // str = "hello so happy world"
//s.insert(pos,cstr,n) 在字符串s的pos位置插入字符数组cstr从开始到后面的n个字符
//此处不可将"it is so happy wow"替换为str3
str.insert(6,"it is so happy wow",6); // str = "hello it is world"
替换(replace)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.replace(p0,n0,n,ch) 删除p0开始的n0个字符,然后在p0处插入n个字符ch
str.replace(0,6,4,'z'); // str = "zzzzworld"
//s.replace(p0,n0,str) 删除从p0开始的n0个字符,然后在p0处插入字符串str
str.replace(0,6,str2); // str = "hard world"
//s.replace(p0,n0,str,pos,n) 删除p0开始的n0个字符,然后在p0处插入字符串str中从pos开始的n个字符
str.replace(0,6,str3,6,9); // str = "so happy world"
//s.replace(p0,n0,cstr,n) 删除p0开始的n0个字符,然后在p0处插入字符数组cstr的前n个字符
//此处不可将"it is so happy wow"替换为str3
str.replace(0,6,"it is so happy wow",6); // str = "it is world"
添加(append)
//append函数用在字符串的末尾添加字符和字符串。(同样与插入、替换对应理解)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.append(n,ch) 在当前字符串结尾添加n个字符c
str.append(4,'z'); // str = "hello worldzzzz"
//s.append(str) 把字符串str连接到当前字符串的结尾
str.append(str2); // str = "hello worldhard "
//s.append(str,pos,n) 把字符串str中从pos开始的n个字符连接到当前字符串的结尾
str.append(str3,6,9); // str = "hello worldso happy "
//append(cstr,int n) 把字符数组cstr的前n个字符连接到当前字符串结尾
//此处不可将"it is so happy wow"替换为str3
str.append("it is so happy wow",6); // str = "hello worldit is "
删除(erase)
string str = "welcome to my blog";
//s.erase(pos,n) 把字符串s从pos开始的n个字符删除
str.erase(11,3); // str = "welcome to blog"
剪切(substr)
string str = "The apple thinks apple is delicious";
//s.substr(pos,n) 得到字符串s位置为pos后面的n个字符组成的串
string s1 = str.substr(4,5); // s1 = "apple"
//s.substr(pos) 得到字符串s从pos到结尾的串
string s2 = str.substr(17); // s2 = "apple is delicious"
比较(compare)
string str1 = "small leaf";
string str2 = "big leaf";
//s.compare(str) 比较当前字符串s和str的大小
cout << str1.compare(str2); // 1
//s.compare(pos,n,str) 比较当前字符串s从pos开始的n个字符与str的大小
cout << str1.compare(2,7,str2); // -1
//s.compare(pos,n0,str,pos2,n) 比较当前字符串s从pos开始的n0个字符与str中pos2开始的n个字符组成的字符串的大小
cout << str1.compare(6,4,str2,4,4); // 0
//s.compare(pos,n0,cstr,n) 比较当前字符串s从pos开始的n0个字符与字符数组cstr中前n个字符的大小
//此处不可将"big leaf"替换为str2
cout << str1.compare(6,4,"big leaf",4); // 1
交换(swap)
string str1 = "small leaf";
string str2 = "big leaf";
//或者str1.swap(str2) ,输出结果相同
swap(str1,str2); // str1 = "big leaf" str2 = "small leaf"
swap(str1[0],str1[1]); // str1 = "ibg leaf"
反转(reverse)
string str = "abcdefghijklmn";
reverse(str.begin(),str.end());
迭代器(iterator)
string str = "abcdefghijklmn";
//s.begin() 返回字符串s第一个字符的位置
char a = *(str.begin()); // a
//s.end() 返回字符串s最后一个字符串的后一个位置
char b = *(str.end()-1); // n
//s.rbegin() 返回字符串s最后一个字符的位置
char c = *(str.rbegin()); // n
//s.rend() 返回字符串s第一个字符的前一个位置
char d = *(str.rend()-1); // a
查找(find)
string str = "The apple thinks apple is delicious"; //长度34
string key = "apple";
//s.find(str) 查找字符串str在当前字符串s中第一次出现的位置
int pos1 = str.find(key); // 4
//s.find(str,pos) 查找字符串str在当前字符串s的[pos,end]中第一次出现的位置
int pos2 = str.find(key, 10); // 17
//s.find(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[pos,end]中第一次出现的位置
//此处不可将"delete"替换为str2(如果定义str2 = "delete")
int pos3 = str.find("delete", 0, 2); // 26
//s.find(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置
int pos4 = str.find('s', 0); // 15