要使用 string 类,必须包含头文件 <string>。string 库提供了许多其他功能,如删除字符串的部分或全部,用一个字符的部分或全部替换另一个字符串的部分或全部,插入、删除字符串中数据,比较、提取、复制、交换等。
函数名称 | 功能 |
---|---|
构造函数 | 产生或复制字符串 |
析构函数 | 销毁字符串 |
=,assign | 赋以新值 |
Swap | 交换两个字符串的内容 |
+ =,append( ),push_back() | 添加字符 |
insert () | 插入字符 |
erase() | 删除字符 |
clear () | 移除全部字符 |
resize () | 改变字符数量 |
replace() | 替换字符 |
+ | 串联字符串 |
==,! =,<,<=,>,>=,compare() | 比较字符串内容 |
size(),length() | 返回字符数量 |
max_size () | 返回字符的最大可能个数 |
empty () | 判断字符串是否为空 |
capacity () | 返回重新分配之前的字符容量 |
reserve() | 保留内存以存储一定数量的字符 |
[],at() | 存取单一字符 |
>>,getline() | 从 stream 中读取某值 |
<< | 将值写入 stream |
copy() | 将内容复制为一个 C - string |
c_str() | 将内容以 C - string 形式返回 |
data() | 将内容以字符数组形式返回 |
substr() | 返回子字符串 |
find() | 搜寻某子字符串或字符 |
begin( ),end() | 提供正向迭代器支持 |
rbegin(),rend() | 提供逆向迭代器支持 |
get_allocator() | 返回配置器 |
常见的 string 类构造函数有以下几种形式:
string strs ;//生成空字符串
string s(str);//生成字符串str的复制品
string s(str, stridx) ; //将字符串str中始于stridx的部分作为构造函数的初值
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() ; //销毁所有内存,释放内存
例1 string的构造
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("12345678");
char ch[ ] = "abcdefgh";
string a; //定义一个空字符串
string str1 (str); //构造函数,全部复制
string str2 (str, 2, 5); //构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str_2
string str3 (ch, 5); //将字符串ch的前5个元素赋值给str_3
string str4 (5,'X'); //将 5 个 'X' 组成的字符串 "XXXXX" 赋值给 str_4
string str5 (str.begin(), str.end()); //复制字符串 str 的所有元素,并赋值给 str_5
cout << str << endl;
cout << a << endl ;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
return 0;
}
程序运行结果:
12345678
12345678
34567
abcde
XXXXX
12345678
String 类型对象包括三种求解字符串长度的函数:size()
和length()
、 maxsize()
和capacity()
:
size() 和 length()
这两个函数会返回 string 类型对象中的字符个数,且它们的执行效果相同。max_size()
函数返回 string 类型对象最多包含的字符数。一旦程序使用长度超过 max_size() 的 string 操作,编译器会拋出 length_error 异常。capacity()
该函数返回在重新分配内存之前,string 类型对象所能包含的最大字符数。例2 string获取长度
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int size = 0;
int length = 0;
unsigned long maxsize = 0;
int capacity=0;
string str ("12345678");
string str_custom;
str_custom = str;
str_custom.resize (5);
size = str_custom.size();
length = str_custom.length();
maxsize = str_custom.max_size();
capacity = str_custom.capacity();
cout << "size = " << size << endl;
cout << "length = " << length << endl;
cout << "maxsize = " << maxsize << endl;
cout << "capacity = " << capacity << endl;
return 0;
}
程序运行结果:
size = 8
length = 8
maxsize = 2147483647
capacity = 15
????字符串中元素的访问是允许的,一般可使用两种方法访问字符串中的单一字符:下标操作符[]
和 成员函数at()
。两者均返回指定的下标位置的字符。第 1 个字符索引(下标)为 0,最后的字符索引为 length()-1。
需要注意的是,这两种访问方法是有区别的:
例3 string获取字符串元素
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s ("abcde");
char temp =0;
char temp_1 = 0;
temp = s [2]; //"获取字符 'c'
temp_1 = s.at(2); //获取字符 'c'
cout << temp << " " << temp_1 << std::endl;
return 0;
}
程序运行结果:
c c
????string 类模板既提供了>、<、==、>=、<=、!=
等比较运算符,还提供了 compare()
函数,其中 compare()
函数支持多参数处理,支持用索引值和长度定位子串进行比较。该函数返回一个整数来表示比较结果。如果相比较的两个子串相同,compare() 函数返回 0,否则返回非零值。
函数原型:
int compare (const basic_string& s) const;
int compare (const Ch* 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;
例4 string比较之compare
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string A ("aBcdef");
string B ("AbcdEf");
string C ("123456");
string D ("123dfg");
//下面是各种比较方法
int m=A.compare (B); //完整的A和B的比较
int n=A.compare(1,5,B,4,2); //"Bcdef"和"AbcdEf"比较
int p=A.compare(1,5,B,4,2); //"Bcdef"和"Ef"比较
int q=C.compare(0,3,D,0,3); //"123"和"123"比较
cout << "m = " << m << ", n = " << n <<", p = " << p << ", q = " << q << endl;
cin.get();
return 0;
}
程序运行结果:
m = 1, n = -1, p = -1, q = 0
由此可知,string 类的比较 compare() 函数使用非常方便,而且能区分字母的大小写。
String 类的常见运算符包括 >、<、==、>=、<=、!=。
例5 String比较之比较运算符
#include <iostream>
#include <string>
using namespace std;
void TrueOrFalse (int x)
{
cout << (x?"True":"False")<<endl;
}
int main ()
{
string S1 = "DEF";
string CP1 = "ABC";
string CP2 = "DEF";
string CP3 = "DEFG";
string CP4 ="def";
cout << "S1= " << S1 << endl;
cout << "CP1 = " << CP1 <<endl;
cout << "CP2 = " << CP2 <<endl;
cout << "CP3 = " << CP3 <<endl;
cout << "CP4 = " << CP4 <<endl;
cout << "S1 <= CP1 returned ";
TrueOrFalse (S1 <=CP1);
cout << "S1 <= CP2 returned ";
TrueOrFalse (S1 <= CP2);
cout << "S1 <= CP3 returned ";
TrueOrFalse (S1 <= CP3);
cout << "CP1 <= S1 returned ";
TrueOrFalse (CP1 <= S1);
cout << "CP2 <= S1 returned ";
TrueOrFalse (CP2 <= S1);
cout << "CP4 <= S1 returned ";
TrueOrFalse (CP4 <= S1);
cin.get();
return 0;
}
程序运行结果:
S1= DEF
CP1 = ABC
CP2 = DEF
CP3 = DEFG
CP4 = def
S1 <= CP1 returned False
S1 <= CP2 returned True
S1 <= CP3 returned True
CP1 <= S1 returned True
CP2 <= S1 returned True
CP4 <= S1 returned False
在使用时比较运算符时,对于参加比较的两个字符串,任一个字符串均不能为 NULL
,否则程序会异常退出。
????字符串的输入输出直接用cin
和cout
就可以,但是cin
在遇到空格后就会停止输入,无法读入带有空格的字符串,读入带空格的字符串可以用getline( cin ,str )
.
例6 String输入输出
#include <iostream>
#include <string>
using namespace std;
void main ()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2, ' ');
cout << "You inputed chars are: " << s1 << endl;
cout << "You inputed chars are: " << s2 << endl;
}
程序运行结果:
123456
asdfgh klj
You inputed chars are: 123456
You inputed chars are: asdfgh
在 STL 中,字符串的查找功能可以实现多种功能,比如说:
????若查找find()
函数和其他函数没有搜索到期望的字符(或子串),则返回 npos
;若搜索成功,则返回搜索到的第 1 个字符或子串的位置
。其中,npos 是一个无符号整数值,初始值为 -1。当搜索失败时, npos 表示“没有找到(not found)”或“所有剩佘字符”。
????值得注意的是,所有查找 find() 函数的返回值均是 size_type 类型,即无符号整数类型。该返回值用于表明字符串中元素的个数或者字符在字符串中的位置。
????字符串有另一种查找为rfind()
实现逆向查找。
原文:https://www.cnblogs.com/lanxiang/p/11252404.html