当前位置: 首页 > news >正文

【C++】STL详解之string类

目录

什么是STL

STL的版本

STL的六大组件

STL的缺陷

一.string的定义方式

二. string的插入

1.使用push_back进行尾插

2.使用insert插入

三.string的拼接

四.string的删除

1.使用pop_back进行尾删

2.使用erase进行删除

五.string的查找

1.使用find正向搜索第一个匹配项

2. 使用rfind函数反向搜索第一个匹配项

六.string的比较

七,string的替换

八. string的交换

1.使用swap函数完成两个string类的交换

九. string的大小和容量

1.使用size函数和length函数获取当前有效字符个数

2.使用max_size函数获取对象最多可包含的字符数

3.使用capacity函数获取当前对象所获取的内存空间的的大小

4. 使用resize改变当前对象的有效字符的个数

5.使用reserve改变当前对象的容量大小

6.使用clear删除对象的内容,删除后对象变为空字符串

7.使用empty判断对象是否为空

十. string中元素的访问

1. operator[ ] 

2.使用at访问对象中的元素

3.使用范围for访问对象中的元素

4.使用迭代器访问对象中的元素

十一. string中运算符的使用

1.operator=

2.operator+=

3.operator+

4.operator>> 和 operator<<

5.relational operators(关系运算符)

十二. 与迭代器相关的函数

1.与正向迭代器相关的函数

2.与反向迭代器相关的函数

十三. string与字符串之间的转换

1.将字符串转化为string

2.使用c_str或data转换为字符串

十四.string中子字符串的提取

1.使用substr函数的提取string中的子字符串

2.使用copy函数将string中的子字符串复制到字符数组中

十五. string中的getline函数


本次内容大纲:

什么是STL

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。

STL的版本

原始版本 Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何人任意 运用、拷贝、修改、传播、商业使用这些代码,无需付费。唯一的条件就是也需要向原始版本一样做开源使 用。 HP 版本--所有STL实现版本的始祖。

P. J. 版本 由P. J. Plauger开发,继承自HP版本,被Windows Visual C++采用,不能公开或修改,缺陷:可读性比较低, 符号命名比较怪异。

RW版本 由Rouge Wage公司开发,继承自HP版本,被C+ + Builder 采用,不能公开或修改,可读性一般。

SGI版本 由Silicon Graphics Computer Systems,Inc公司开发,继承自HP版 本。被GCC(Linux)采用,可移植性好, 可公开、修改甚至贩卖,从命名风格和编程 风格上看,阅读性非常高。我们后面学习STL要阅读部分源代码, 主要参考的就是这个版本。

STL的六大组件

STL的缺陷

1. STL库的更新太慢了。这个得严重吐槽,上一版靠谱是C++98,中间的C++03基本一些修订。C++11出来已经相隔了13年,STL才进一步更新。

2. STL现在都没有支持线程安全。并发环境下需要我们自己加锁。且锁的粒度是比较大的。

3. STL极度的追求效率,导致内部比较复杂。比如类型萃取,迭代器萃取。

4. STL的使用会有代码膨胀的问题,比如使用vector/vector/vector这样会生成多份代码,当然这是模板语 法本身导致的。

一.string的定义方式

在string类中实现了多个构造函数的重载,常用的构造函数如下:

string(); //构造一个空字符串string (const string& str);	//使用一个已经存在的对象拷贝构造给另一个对象string (const string& str, size_t pos, size_t len = npos); //分割字符串	string (const char* s);	//使用常量字符串实例化一个对象string (const char* s, size_t n); //从第一个元素开始取得向后的元素,限制个数string (size_t n, char c);	//复制C字符,复制n个

string类,构造函数的使用方式:

#include <iostream>
#include <string>
using namespace std;int main() {//实例化一个空字符对象string s7();cout << s7 << endl;//使用常字符串给对象初始化,对象实例化string s1("zyx");cout << s1 << endl;//用一个已经存在的对象给另外一个对象拷贝构造string s2(s1);cout << s2 << endl;//分割字符串string s3("https://cplusplus.com//reference//string//string//string");string s4(s3, 0, 5);string s5(s3, 8, 13);string s6(s3, 23);cout << s4 << endl;cout << s5 << endl;cout << s6 << endl;//从第一个元素开始向后取字符串(第一个参数只能是常量字符串,不能是对象)const char *c1 = "https://cplusplus.com";string s8(c1, 5);cout << s8 << endl;//使用对象会从第五个元素向后取string s9(s3, 5);cout << s9 << endl;//输出"**********"string s10(10, '*');cout << s10 << endl;return 0;
}

二. string的插入

1.使用push_back进行尾插

void push_back (char c)   //在字符串后尾插字符c
//尾插, push_back在string类中是一个成员函数
string s1;
s1.push_back('w');
s1.push_back('x');
s1.push_back('x');
s1.push_back('x');cout << s1 << endl;

2.使用insert插入

string& insert (size_t pos, const string& str);
string& insert (size_t pos, const char* s);
iterator insert (iterator p, char c);
//string类中insert插入的使用
string s0("nxbw");
s0.insert(4, "zyx");
cout << s0 << endl;  //输出:nxbwzyxstring s1 = "FFF";
s0.insert(4, s1);
cout << s0 << endl; //输出:nxbwFFFzyxs0.insert(s0.begin(), 'Z'); //输出:ZnxbwFFFzyx
cout << s0 << endl;

三.string的拼接

string& append (const string& str);
string& append (const char* s);
string& append (size_t n, char c);
string s0("nxbw");
//使用append对string完成拼接
string s2 = "FFF";
//使用的对象进行拼接
cout << s0.append(s2) << endl; //输出:nxbwFFF
//使用字符串进行拼接
cout << s0.append("FF") << endl; //输出:nxbwFFFFFF
//复制字符个数,拼接在对象后面
cout << s0.append(5, 'F') << endl; //输出:nxbwFFFFFFFFFF

四.string的删除

1.使用pop_back进行尾删

void pop_back();
string s0("nxbw");
//使用pop_back进行尾删
s0.pop_back();
s0.pop_back();
s0.pop_back();
cout << s0 << endl; //输出:n

2.使用erase进行删除

string& erase (size_t pos = 0, size_t len = npos);
iterator erase (iterator p);
iterator erase (iterator first, iterator last);
string s0 = "nxbw";
//使用erase删除
s0.erase(0, 2); 
cout << s0 << endl; //输出: bw
//删除某个位置的字符
s0.erase(s0.begin() + 2); 
cout << s0 << endl; //输出nxw
//删除[first, last)这个范围内的字符,左闭右开
s0.erase(s0.begin() + 1, s0.end() - 1);
cout << s0 << endl; //输出:xb

五.string的查找

1.使用find正向搜索第一个匹配项

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const;
//使用find函数正向搜索第一个匹配项
string s1 = "Linux is not NUIX";
string s2 = "is";
//使用string类对象寻找匹配项
size_t count1 = s1.find(s2);
cout << count1 << endl; //输出:6
//使用字符串常量寻找匹配项
const char* str1 = "is";
size_t count1 = s1.find(str1);
cout << count1 << endl; //输出:6
//使用字符串寻找匹配项
const char str2 = 's';
size_t count3 = s1.find(str2);
cout << count3 << endl; //输出:7

2. 使用rfind函数反向搜索第一个匹配项

size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (char c, size_t pos = npos) const;

可以看到系统跳过了第一个 . 直接寻找的第二个

//使用rfind函数反向搜索第一个匹配项
string s0 = "blog.csdn.net";
string s1 = ".";
//使用string对象进行搜索
size_t count1 = s0.rfind(s1);
cout << count1 << endl; //输出:9
//使用常量字符串进行搜索
const char* str1 = ".";
size_t count2 = s0.rfind(str1);
cout << count2 << endl; //输出:9
//使用字符进行搜索
const char s2 = '.';
size_t count3 = s0.rfind(s2);
cout << count3 << endl; //输出:9

六.string的比较

int compare (const string& str) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;

compare比较规则:

1.从第一个不相同的字符进行比较,如果被比较的字符值较大,或者其他字符都匹配,比较字符串的个数较少,则返回非负数

2.从第一个不相同的字符进行比较,如果被比较的字符值较小,或者其他字符都匹配,比较字符串的个数较多,则返回非正数

3.如果两个字符串每个字符以及个数都相等,则返回0

//比较字符串
string str1 = "stringa";
string str2 = "stringb";
//str1和str2比较
int judge = str1.compare(str2);
cout << judge << endl; //输出:-1 str1小于str2
//切割str1部分与str2比较
judge = str1.compare(0, 5, str2); 
cout << judge << endl; //输出: -1
//切割str1和str2的部分进行比较
judge = str1.compare(0, 4, str2, 0, 4);
cout << judge << endl; //输出: 0 

注:

compare除了可以和string对象比较之外,还可以和字符串进行比较

七,string的替换

使用replace函数完成string的替换

string& replace (size_t pos, size_t len, const char* s);
string& replace (size_t pos, size_t len, size_t n, char c);
//string的替换
string s1 = "hello world";
s1.replace(1, 4, "nxbw");
cout << s1 << endl; //输出:hnxbw world
//replace(pos, len, n, c)
//从pos位置开始往后len个位置,替换为n个字符c
s1.replace(11, 3, 3, '!');
cout << s1 << endl; //输出: hnxbw world!!!

八. string的交换

1.使用swap函数完成两个string类的交换

void swap (string& x, string& y);
void swap (string& str);
//swap交换
string s1 = "nxbw";
string s2 = "wxxx";swap(s1, s2);
cout << s1 << ' ' << s2 << endl; //输出:wxxx nxbws1.swap(s2);
cout << s1 << ' ' << s2 << endl; //输出:nxbw wxxx

九. string的大小和容量

1.使用size函数和length函数获取当前有效字符个数

size_t size() const;
size_t length() const;
//使用函数获取该对象当前有效字符个数
string s1("I love learning");cout << s1.size() << endl; //输出:15
cout << s1.length() << endl; //输出:15

算大小,这里推荐使用size,当热你想使用length也可以

2.使用max_size函数获取对象最多可包含的字符数

size_t max_size() const;
string s1("I love learning");
cout << s1.max_size() << endl; //输出: 2147483647

3.使用capacity函数获取当前对象所获取的内存空间的的大小

size_t capacity() const;
//获取容量
string s1 = "nxbw";
cout << s1.capacity() << endl; //输出: 15

4. 使用resize改变当前对象的有效字符的个数

void resize (size_t n);
void resize (size_t n, char c);

resize使用规则

1. 当n大于size时,将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0‘

2. 当n小于size时,将size缩小到n

//改变对象当前有效字符的数量
string s1("hello world");
//扩大该对象有效字符的个数,扩大的部分默认为:'\0'
s1.resize(20);
cout << s1 << endl; //hello world
cout << s1.size() << endl; //20
cout << s1.capacity() << endl; //31//扩大该对象有效字符的个数,扩大的部分为:'x'
s1.resize(30, 'x');
cout << s1 << endl; // hello worldxxxxxxxxxx
cout << s1.size() << endl; //30
cout << s1.capacity() << endl; //31//缩小该对象有效字符个数
s1.resize(5);
cout << s1 << endl; //输出:hello
cout << s1.size() << endl; //5
cout << s1.capacity() << endl; //31 

5.使用reserve改变当前对象的容量大小

void reserve (size_t n = 0);
//改变当前对象容量大小
string s1("hello");
//缩小该对象的容量,对象的容量不会改变
s1.reserve(20);
cout << s1 << endl; //hello
cout << s1.size() << endl; //5
cout << s1.capacity() << endl; //31
//增大该对象的容量,将当前对象的capacity扩大到n或者大于n
s1.reserve(40);
cout << s1 << endl; //hello
cout << s1.size() << endl; //5
cout << s1.capacity() << endl; //47

6.使用clear删除对象的内容,删除后对象变为空字符串

void clear();
//删除某个对象的内容,删除后变成空字符串
string s1 = "csdn";
s1.clear();
cout << s1 << endl;

7.使用empty判断对象是否为空

bool empty() const;

判定规则:

对象不为空,返回1

对象为空,返回0

//判断是否为空
string s1("nxbw");
cout << s1.empty() << endl; // 0//删除之后在判断
s1.clear();
cout << s1.empty() << endl; // 1

十. string中元素的访问

1. operator[ ] 

因为string类对[ ]运算符进行了重载,所以我们可以使用[ ]+下标的方式来访问对象中的元素。并且该重载使用的是引用返回,所以我们可以通过下标加[ ]的方式改修对应位置的元素

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
//operator[]
string s1("hello world");cout << s1.size() << endl; //11
int i = 0;
//使用operator[]打印出s1的内容
for (i = 0; i < s1.size(); ++i)
{//没有打印出'\0',因为size函数计算不包含('\0')cout << s1[i];  //hello world
}
cout << endl;//[]+下标来修改对应对象元素内容
int j = 0;
for (j = 0; j < s1.size(); ++j)
{s1[j]++;cout << s1[j]; //ifmmp!xpsme
}
cout << endl;

2.使用at访问对象中的元素

char& at (size_t pos);
const char& at (size_t pos) const;
//使用at对s1进行访问
string s1("hello world");cout << s1.size() << endl; //11
int i = 0;
//使用at访问s1打印出s1的内容
for (i = 0; i < s1.size(); ++i)
{//没有打印出'\0',因为size函数计算不包含('\0')cout << s1.at(i);  //hello world
}
cout << endl;//通过at来修改对应对象元素内容
int j = 0;
for (j = 0; j < s1.size(); ++j)
{s1.at(j)++;cout << s1.at(j); //ifmmp!xpsme
}
cout << endl;

3.使用范围for访问对象中的元素

使用该语法,需要特别注意的是,如果你需要使用范围for来修改对象的元素,那么接收元素的变量必须是引用类型,否则e只是该对象元素的一个拷贝,修改e不会对对象元素有任何影响

//使用范围for来访问s1元素
string s1("nxbw,zswdmb");
for (auto e : s1)
{cout << e;//nxbw,zswdmb
}
cout << endl;
//使用范围for修改s1对应元素
for (auto& e : s1) //注意想要修改元素e的类型必须引用
{e++;cout << e;//oycx-{txenc
}
cout << endl;

auto可以是其他类型。范围for底层实现就是用迭代器实现的,一个类只要支持迭代器他就支持范围for

可以通过汇编看到

4.使用迭代器访问对象中的元素

迭代器的使用方式:容器<类型>::iterator 变量名 (迭代器的用法和指针类似)

//使用迭代器访问对象元素,并对其进行修改
string s1 = "nxbw";
string::iterator it = s1.begin();
for (int i = 0; i < s1.size(); ++i)
{cout << (*it); //nxbwit++;
}
it = s1.begin();
cout << endl;
//使用迭代器访问修改对应元素
for (int i = 0; i < s1.size(); ++i)
{(*it)++;cout << (*it); //oycxit++;
}
cout << endl;

迭代器:

iterator提供一种统一的方式修改和访问容器的数据

迭代器可以和算法配合来对访问容器中的数据进行操作

例:

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);vector<int>::iterator it = v.begin();
for (auto e : v)
{cout << e << ' '; //1 2 3 4
}
cout << endl;
//迭代器通过和算法配合可以对容器中的数据进行操作
reverse(v.begin(), v.end());
for (auto e : v)
{cout << e << ' '; //4 3 2 1
}
cout << endl;

十一. string中运算符的使用

1.operator=

string中对=运算符进行重载,重载后的=运算度支持string类的赋值,字符串的赋值以及字符的赋值

//使用operator=
string s1("nxbw");
string s2;//支持对象之间的赋值
s2 = s1;
cout << s2 << endl; //nxbw
//支持字符串赋值给对象
s2 = "csdn";
cout << s2 << endl; //csdn
//支持字符赋值给对象
s2 = 'z';
cout << s2 << endl; //zyx

2.operator+=

string类中对+=运算符进行了重载,重载后的+=运算符支持string类的复合赋值,字符串的复合赋值,字符的复合赋值

//使用operator+=
string s1("n");
string s2("x");//支持对象之间的赋值
s1 += s2;
cout << s1 << endl; //nx
//支持字符串赋值给对象
s1 += "bw";
cout << s1 << endl; //nxbw
//支持字符赋值给对象
s1 += '!';
cout << s1 << endl; //nxbw!

3.operator+

string类中支持+运算符的重载,重载之后支持以下几种操作:

string类 + string类   string类 + 字符串   字符串 + string类

string类 + 字符   字符 + string类

//operator+
string s1("nxbw");
string s2("!wxxx");
string s3;
//string类 + string类
s3 = s1 + s2;
cout << s3 << endl; //nxbw!wxxx
//string类 + 字符串
s3 = s1 + "!wxxx";
cout << s3 << endl; //nxbw!wxxx
//字符串 + string类
s3 = "nxbw" + s2;
cout << s3 << endl; //nxbw!wxxx
//string类 + 字符 
s3 = s1 + '!';
cout << s3 << endl; //nxbw!
//字符 + string类
s3 = '!' + s1;
cout << s3 << endl; //nxbw!

4.operator>> 和 operator<<

string类中也重载了<<和>>运算符,正是如此,我们才能直接对string类对象进行输入和输出

//operator<<和opeartor>>
string s1;
cin >> s1;
cout << s1 << endl;	

5.relational operators(关系运算符)

string中还对一些关系运算符继续了重载,它们分别是:==,!=, <, <=, >, >=,这些运算符都支持string类和string类,string类和字符串,。string类和字符

//关系运算符
string s1("nxbw");
string s2("wxxx");cout << (s1 > s2) << endl; //0
cout << (s1 >= s2) << endl; //0
cout << (s1 < s2) << endl; //1
cout << (s1 <= s2) << endl; //1
cout << (s1 == s2) << endl; //0
cout << (s1 != s2) << endl; //1

注:与C比较方法一样,都是比较的AscII码值

十二. 与迭代器相关的函数

1.与正向迭代器相关的函数

begin:返回一个指向字符串第一个字符迭代器

iterator begin();
const_iterator begin() const;

end:返回一个指向字符串结束字符的迭代器,即:最后一字符的后面一位 

iterator end();
const_iterator end() const;

以下图为例:

//使用迭代器访问对象元素,并对其进行修改
string s1 = "nxbw";
string::iterator it = s1.begin();
while(it != s1.end())
{cout << (*it); //nxbwit++;
}
cout << endl;

2.与反向迭代器相关的函数

rbegin函数:返回指向一个字符串最后一个字符的反向迭代器

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;

rend函数:返回指向一个字符串第一个字符的前面的反向迭代器

reverse_iterator rend();
const_reverse_iterator rend() const;

如图:

//反向迭代器的使用
string s1("nxbw");
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{cout << *rit; //wbxnrit++;
}
cout << endl;

觉得类型太长,也可以使用auto帮你自动识别类型

//反向迭代器的使用
string s1("nxbw");
auto rit = s1.rbegin();
while (rit != s1.rend())
{cout << *rit; //wbxnrit++;
}
cout << endl;

例:

为了避免权限扩大,这里只能使用方向迭代器:const_reverse_iterator,觉得类型很长可以使用auto自动识别类型,

//翻转
void Func(const string& s1) //s1是nxbw
{string::const_reverse_iterator rit = s1.rbegin();/*for(auto e : s1)*/while(rit != s1.rend()){cout << *rit; //wbxn++rit;}cout << endl;
}

十三. string与字符串之间的转换

1.将字符串转化为string

//将字符串转化为string类
string s1("hello world");
cout << s1 << endl; //hello worldchar str[] = "hello world";
string s2(str);
cout << s2 << endl; //hello world

2.使用c_str或data转换为字符串

const char* c_str() const;
const char* data() const;
  • 在C++98中,c_str()返回 const char* 类型,返回的字符串会以空字符结尾。
  • 在C++98中,data()返回 const char* 类型,返回的字符串不以空字符结尾。
  • 但是在C++11版本中,c_str()与data()用法相同。
//将string类对象转换为字符串
string s1("hello world");const char* str1 = s1.c_str();
const char* str2 = s1.data();cout << str1 << endl; //hello world
cout << str2 << endl; //hello world

十四.string中子字符串的提取

1.使用substr函数的提取string中的子字符串

string substr (size_t pos = 0, size_t len = npos) const;
//提取string中的字符串
string s1("nxbw");
string s2;s2 = s1.substr(2, 3);
cout << s2 << endl; //bw

2.使用copy函数将string中的子字符串复制到字符数组中

size_t copy (char* s, size_t len, size_t pos = 0) const;
//使用copy函数将string中的子字符串拷贝到字符数组中
string s1("nxbw");
char str1[20];//copy函数不会帮你在末尾加'\0',所以得手动添加'\0'
size_t length = s1.copy(str1, 4, 0);
str1[length] = '\0';
cout << str1 << endl; //nxbw

十五. string中的getline函数

使用>>(流提取操作符)进行输入操作,当>>读取到空格时就会停止读取,我们不能使用>>将一串含有空格的字符串输入到string对象中

例:

空格cin完全读取不到

string s1;cin >> s1; //hello world
cout << s1 << endl; //hello

遇到空格就停止读取,怎么办呢?这时就需要使用getline函数

使用方法一:

istream& getline (istream& is, string& str);
//使用getline往string中输入
string s1;
getline(cin, s1);
cout << s1 << endl; //hello world

使用方法二:

istream& getline (istream& is, string& str, char delim);
//使用getline往string中输入
string s1;
getline(cin, s1, 'b'); //第三个参数是读取结束标志或者换行符(\n)为止
cout << s1 << endl; //nx


http://www.mrgr.cn/news/35622.html

相关文章:

  • 【Python】探索 PyTorch 在机器学习中的应用
  • 智慧照明,安全度夏:揭秘如何前置防御暴雨中的路灯杆漏电隐患
  • 【云原生安全篇】Trivy助力离线Harbor漏洞扫描实践
  • Python批量合并365个工作表的2种方法
  • Qt-DateEditTimeEdit输入类控件(33)
  • 【AIGC】ChatGPT提示词解析:如何生成爆款标题、节日热点文案与完美文字排版
  • Chunk-based Chinese Spelling Check with Global Optimization(EMNLP2020)
  • Doris之使用优化
  • 一天认识一个硬件之测线器
  • Redis实战--Redis的数据持久化与搭建Redis主从复制模式和搭建Redis的哨兵模式
  • 前端框架对比与选择指南:React.js、Angular、Vue.js及其他
  • 【JS】Reflect
  • Java后端开发中的RESTful API版本控制策略
  • uniapp js向json中增加另一个json的全部数据,并获取json长度
  • Elasticsearch7.7修改network.host IP地址 start启动失败及Elasticsearch7的配置项详解
  • 栈的深度解析:顺序栈与链栈的实现
  • Oracle逻辑备份脚本【生产环境适用】
  • 苏轼为何要写石钟山记?时间节点是关键
  • 问:Java线程为不直接run(),而是要先Start()?
  • service 命令:管理系统服务