一般地,由n个字符构成的串记作:S = " a 0 a 1 . . . a n − 1 " S = "a_0 a_1...a_{n-1} "S="a0a1...an−1",其中0 ≤ i ≤ n 0\leq i \leq n0≤i≤n。
·空串:长度为0,与只包含空格的串不同。
·子串:串中任意个连续字符组成的子序列。
·前缀:起始于位置0,长度为k的子串。
·后缀:终止于位置n-1,长度为k的子串。
·字符串除本身之外的所有非空子串、前缀和后缀,分别称作真子串、真前缀和真后缀。STL string 类提供的各种操作函数大致分为八类:构造器和析构器、大小和容量、元素存取、字符串比较、字符串修改、字符串接合、I/O 操作以及搜索和查找。要使用 string 类,必须包含头文件 <string>。
①构造器和析构器
·在 C++ 中确实存在一个从 const char * 到 string 的隐式型别转换,却不存在从 string 对象到 C_string 的自动型别转换。对于 string 类型的字符串,可以通过 c_str() 函数返回该 string 类对象对应的 C_string。
//常见的 string 类构造函数有以下几种形式:
string strs;//生成空字符串
string s(str);//生成字符串str的复制品
string s(str, index);//将字符串str中始于index的部分作为构造函数的初值
string s(str, strbegin, strlen);//将字符串str中始于strbegin、长度为strlen的部分作为字符串初值
string s(cstr);//以C_string类型cstr作为字符串s的初值
string s(cstr,char_len);//以C_string类型cstr的前char_len个字符串作为字符串s的初值
string s(num, c);//生成一个字符串,包含num个c字符
string s(strs, beg, end);//以区间[beg, end]内的字符作为字符串s的初值
//析构函数的形式如下:
~string();//销毁所有内存,释放内存
②大小和容量
·size() 和 length():返回 string 类型对象中的字符个数,且它们的执行效果相同。
·max_size():返回 string 类型对象最多包含的字符数。一旦程序使用长度超过 max_size() 的 string 操作,编译器会拋出 length_error 异常。
·capacity():返回在重新分配内存之前,string 类型对象所能包含的最大字符数。
·reserve() :为 string 类型对象重新分配内存;重新分配的大小由其参数决定;默认参数为 0。
·resize():更改string类对象长度。
size = str.size();
length = str.length();
maxsize = str.max_size();
capacity = str.capacity();
str.resize(num);
str.resize(num, c);
③元素存取
·下标操作符[] 和 成员函数at()。两者均返回指定的下标位置的字符。第 1 个字符索引(下标)为 0,最后的字符索引为 length()-1。
·二者区别:下标操作符 [] 在使用时不检查索引的有效性,如果下标超出字符的长度范围,会示导致未定义行为;函数 at() 在使用时会检查下标是否有效,如果给定的下标超出字符的长度范围,系统会抛出 out_of_range 异常。
tmp_1 = str[index];
tmp_2 = str.at(index);
④字符串比较
·basic_string是一个模版类,string是模版形参为char的basic_string模版类的类型定义
·Basic_string 类模板既提供了 >、<、==、>=、<=、!= 等比较运算符(按位比较)。
·Basic_string 类模板还提供了compare() 函数:支持用索引值和长度定位子串进行比较,返回一个整数来表示比较结果。如果相比较的两个子串相同,compare() 函数返回 0,否则返回非零值。
//compare() 的原型
int compare(const basic_string& s) const;
int compare(const char* p) const;
int compare(size_type pos, size_type n, const basic_string& s) const;
int compare(size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
int compare(size_type pos, size_type n, const Ch* p, size_type = npos) const;
str.compare(s);
str.compare(ptr_char);
str.compare(index, num, s);
str.compare(index1, num1, s, index2, num2);
⑤字符串修改
·assign() :直接给字符串赋值,该函数既可以将整个字符串赋值给新串,也可以将字符串的子串赋值给新串。
//原型
basic_string& assign(const char*s); //直接使用字符串赋值
basic_string& assign(const char*s, size_type n);
basic_string& assign(const basic_string & str, size_type pos, size_type n);//将str的子串赋值给调用串
basic_string& assign(const basic_string& str);//使用字符串的“引用”賦值
basic_string& assign(size_type n, char c) ;//使用 n个重复字符賦值
basic_string& assign(const_iterator first, const_iterator last);//使用迭代器赋值
str.assign(s);
sre.assign(s, num);
str.assign(s, index, num);
str.assign(num, c);
str.assign(iter1, iter2);
·operator=:赋值。
·erase():删除。
//原型
iterator erase(iterator first, iterator last);//删除字符串中迭代器区间[first,last)上所有字符
iterator erase(iterator it);//删除字符串中it所指的字符
basic_string& erase(size_type p0 = 0, size_type n = npos);//删除字符串中从索引p0开始的n个字符
str.erase(iter1, iter2);
ste.erase(index);
str.erase(index, num);
·swap():交换。
//原型
void swap (basic_string& str);
str.swap(str2);
·insert():插入。
//原型
basic_string& insert(size_type p0 , const char * s); //p0位置插人1个字符
basic_string& insert(size_type p0 , const char * s, size_type n); //将s的前3个字符插入p0位置
basic_string& insert(size_type p0, const basic_string& str);
basic_string& insert(size_type p0, const basic_string& str,size_type pos, size_type n); //选取str的子串
basic_string& insert(size_type p0, size_type n, char c); //在下标p0位置插入n个字符c
iterator insert(iterator it, char c);//在it位置插入字符c
void insert(iterator it, const_iterator first, const_iterator last);//在it位置重复插入插入迭代器区间内字符串
void insert(iterator it, size_type n, char c) ; //在it位置重复插入n个字符c
str.insert(index, s);
str.insert(index, s, num);
str.insert(index, s, index_s, num_s);
str.insert(index, num, c);
str.insert(iter, iter1, iter2);
str.insert(iter, num, c);
·push_back():在最后插入一个字符
str.push_back(c);
·replace():替换。
basic_string& replace(size_type p0, size_type n0, const char * s);//将当前字符串从p0索引开始的n0个字符,替换成字符串s
basic_string& replace(size_type p0, size_type n0, const char *s, size_type n);//从源串P0处开始的n0个字符,替换成s开始的n个字符
basic_string& replace(size_type p0, size_type n0, const basic_string& str); //从源串P0处开始的n0个字符,替换成str
basic_string& replace(size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开始替换,替换字符 n0 个
basic_string& replace(size_type p0, size_type n0, size_type n, char c); //使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符
basic_string& replace(iterator first0, iterator last0, const char * s);//使用迭代器替换,和 1) 用法类似
basic_string& replace(iterator first0, iterator last0, const char * s, size_type n);//和 2) 类似
basic_string& replace(iterator first0, iterator last0, const basic_string& str); //和 3) 类似
basic_string& replace(iterator first0, iterator last0, size_type n, char c); //和 5) 类似
basic_string& replace(iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替换
str.replace(index, num, s);
str.replace(index, num, s, num_s);
str.replace(index, num, s, index_s, num_s);
str.replace(index, num, num_c, c);
str.replace(iter1, iter2, s);
str.replace(iter1, iter2, s, num_s);
str.replace(iter1, iter2, num_c, c);
str.replace(iter1, iter2, iter_s1, iter_s2);
⑥字符串接合
·append():在原始字符串后追加。
//原型
basic_string& append(const char * s); //在原始字符串后面追加字符串s
basic_string& append(const char * s, size_type n);//在原始字符串后面追加字符串s的前n个字符
basic_string& append(const basic_string& str, size_type pos,size_type n);//在原始字符串后面追加字符串s的子串 [ pos,…,pos +n -1]
basic_string& append(const basic_string& str);
basic_string& append(size_type n, char c); //追加n个重复字符
basic_string& append(const_iterator first, const_iterator last); //使用迭代器追加
str.append(s);
str.append(s, num);
str.append(s, index, num);
str.append(num, c);
str.append(iter1, iter2);
·operator+:将前后两个字符串拼接起来。
⑦I/O 操作
·"<<" 和 “>>” 提供了 C++ 语言的字符串输入和字符串输出功能。"<<" 可以将字符读入一个流中(例如 ostream);">>" 可以实现将以空格或回车为 “结束符” 的字符序列读入到对应的字符串中,并且开头和结尾的空白字符不包括进字符串中。
·getline():包括两种原型。读取字符时,遇到文件结束符、分界符、回车符时,将终止读入操作,且文件结束符、分界符、回车符在字符串中不会保存;当已读入的字符数目超过字符串所能容纳的最大字符数时,将会终止读入操作。
template <class CharType, class Traits, class Allocator >
basic_istream<CharType, Traits>& getline(basic_istream<CharType, Traits>& _Istr,basic_string <CharType,Traits, Allocator> &_Str);
//上述原型包含 2 个参数:第 1 个参数是输入流;第 2 个参数是保存输入内容的字符串
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(basic_istream <CharType, Traits>&_ Istr, basic_string <CharType, Traits, Allocator>& _Str,CharType_Delim);
//上述原型包含 3 个参数:第 1 个参数是输入流,第 2 个参数保存输入的字符串,第 3 个参数指定分界符。
getline(cin, s1);
getline(cin, s2, ' ');
⑧搜索和查找
·find()
size_type find(char c, size_type pos = 0) const;
//在当前字符串的pos索引位置开始,查找子串s,返回找到的位置索引,-1表示查找失败
size_type find(const char* s , size_type pos = 0) const;
//在当前字符串的pos索引位置开始,查找字符c,返回找到的位置索引,-1表示查找失败
size_type find(const basic_string& s, size_type pos = 0) const;
·rfind():原型和find()函数的原型类似,参数情况也类似,只不过 rfind() 函数适用于实现逆向查找。
str.find(s);
str.find(s, index);
str.rfind(c);//当前位置开始
str.rfind(c, index);
·find_first_of() :在源串中搜索某字符串,返回值是被搜索字符串的第 1 个字符第 1 次出现的下标(位置)。
size_type find_first_not_of(char c, size_type pos = 0) const;
size_type find_first_of(const char* _Ptr, size_type pos= 0) const;
size_type find_first_of(const basic_string & _Str, size_type pos = 0) const;
·find_last_of() :在源串中搜索某字符串的,返回值是被搜索字符串的最后 1 个字符的下标(位置)。
str.find_first_of(s);
str.find_first_of(s, index);
str.find_last_of(c);
str.find_last_of(c, index);
·find_first_not_of():在源字符串中搜索与指定字符(串)不相等的第 1 个字符。
·find_last_not_of() :在源字符串中搜索与指定字符(串)不相等的最后 1 个字符。
str.find_first_not_of(s);
str.find_first_not_of(s, index);
str.find_last_not_of(c);
str.find_last_not_of(c, index);
- string常用算法
·大小写转换:需包含<algorithm>头文件
transform(s.begin(),s.end(),s.begin(),::tolower);//转换成小写,大写将::tolower改成::toupper
·排序:需包含<algorithm>头文件
sort(s.begin(),s.end());
·求子串
s2 = s1.substr(index1, index2);