STL(Standard Template Library)标准模板库,是C++为建立 数据结构与算法的一套标准。STL从广义上分为:容器(container)、算法(algorithm)、迭代器(iterator),容器和算法之间通过迭代器进行无缝连接。STL几乎所有的代码都采用了类模板或者函数模板。
容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
- 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据
- 算法:各种常用的算法,如sort、find、copy、for_each等
- 迭代器:扮演了容器与算法之间的粘合剂
- 仿函数:行为类似函数,可作为算法的某种策略
- 适配器:一种用来修饰容器或者仿函数或迭代器接囗的东西
- 空间配置器:负责空间的配置与管理
STL容器就是将运用最广泛的一些数据结构实现出来,常用的数据结构包含:数组、链表、树、栈、队列、集合、映射表等;这些容器分为序列式容器与关联式容器两种:
- 序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置
- 关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
有限的步骤解决逻辑或数学上的问题,这一门学科叫做算法。算法分为质变算法与非质变算法:
- 质变算法:是指运算过程中会更改区间内的元素的内容,如拷贝、替换、删除等
- 非质变算法:是指运算过程中不会更改区间内的元素内容,如查找、计数、遍历、寻找极值等
提供一种方法,使用之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。每个容器都有自己专属的迭代器。迭代器使用非常类似于指针,初学阶段也可以先将迭代器理解为指针。迭代器种类划分五种:
- 输入迭代器: 对数据只读访问,支持++、==、!=
- 输出迭代器:对数据只写访问,支持++
- 前向迭代器:读写操作,并能向前推进迭代器,支持++、==、!=
- 双向迭代器:读写操作,并能向前和向后操作,支持++、--
- 随机访问迭代器:读写操作,可以心跳跃的方式访问任意数据,功能最强的迭代器,支持++、--、[n]、-n、<、<=、>、>=
STL中最常用的容器为vector,vector数据结构与数组非常相似,也称为单端数组。
vector与普通数组的区别在于数组是静态空间,而vector可以动态扩展。所谓动态扩展,并不是在原空间之后维埃拉新空间,而是找更大的内存空间,然后将原数据拷贝至新空间中,释放原空间。
vector容器的迭代器是支持随机访问的迭代器。
容器:vector
算法:for_each
迭代器:vector
::iterator
函数原型:
- vector
v; //采用模板实现类,默认构造函数 - vector(v.begin(), v.end()); //将v[begin(), end()]区间中的元素拷贝给其本身
- vecotr(n, elem); //构造函数将n个elem拷贝给本身
- vector(const vector& vec); //拷贝构造函数
- #include
- #include
- #include
- using namespace std;
-
- void print(vector<int>& v)
- {
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << "\t";
- }
- cout << endl;
- }
-
- int main()
- {
- // STL - vector - 构造函数
- /*函数原型:
- 1、vector
v; //采用模板实现类,默认构造函数 - 2、vector(v.begin(), v.end()); //将v[begin(), end()]区间中的元素拷贝给其本身
- 3、vecotr(n, elem); //构造函数将n个elem拷贝给本身
- 4、vector(const vector& vec); //拷贝构造函数
-
- */
-
- //1、vector
v; - vector<int> v1;
-
- //赋值
- for (int i = 0; i < 5; i++)
- {
- v1.push_back(i + 1);
- }
-
- //显示
- print(v1);
-
- //2、vector(v.begin(), v.end());
- vector<int> v2(v1.begin(), v1.end());
- print(v2);
-
- //3、vecotr(n, elem);
- vector<int> v3(3,8);
- print(v3);
-
- //4、vector(const vector& vec);
- vector<int> v4(v1);
- print(v4);
-
- system("pause");
-
- return 0;
- }
输出结果
1 2 3 4 5
1 2 3 4 5
8 8 8
1 2 3 4 5
函数原型:
- vector& operator=(const vector &vec); //重载=运算符
- assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身
- assign(n, elem); //将n个elem拷贝赋值给本身
- #include
- #include
- #include
- using namespace std;
-
- void print(vector<int>& v)
- {
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << "\t";
- }
- cout << endl;
- }
-
- int main()
- {
- // STL - vector - 赋值操作
- /*函数原型:
- 1、vector& operator=(const vector &vec); //重载=运算符
- 2、assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身
- 3、assign(n, elem); //将n个elem拷贝赋值给本身
- */
-
- vector<int> v1;
- for (int i = 0; i < 5; i++)
- {
- v1.push_back(i + 1);
- }
- //1、vector& operator=(const vector &vec);
- vector<int> v2;
- v2 = v1;
- print(v2);
-
- //2、assign(beg, end);
- vector<int> v3(v1.begin(), v1.end());
- print(v3);
-
- //3、assign(n, elem);
- vector<int> v4;
- v4.assign(3, 8);
- print(v4);
-
- system("pause");
-
- return 0;
- }
输出结果
1 2 3 4 5
1 2 3 4 5
8 8 8
函数原型
- empty(); //判断容器是否为空
- capacity(); //容器容量
- size(); //返回容器中元素个数
- resize(int num); //重新指定窗口的长度为num,若容器变长,则以默认值填充新位置,如果变短,则末尾超出容器长度的元素被删除。
- resize(int num, elem); //重新指定容器长度的num,基容器变长,则以elem值填充新位置;如果变短,则末尾超出容器长度的元素被删除。
- #include
- #include
- #include
- using namespace std;
-
- void print(vector<int>& v)
- {
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << "\t";
- }
- cout << endl;
- }
-
- int main()
- {
- // STL - vector - 赋值操作
- /*函数原型:
- 1、empty(); //判断容器是否为空
- 2、capacity(); //容器容量
- 3、size(); //返回容器中元素个数
- 4、resize(int num); //重新指定窗口的长度为num,若容器变长,则以默认值填充新位置,如果变短,则末尾超出容器长度的元素被删除。
- 5、resize(int num, elem); //重新指定容器长度的num,基容器变长,则以elem值填充新位置;如果变短,则末尾超出容器长度的元素被删除。
- */
-
- //1、empty();
- vector<int> v1;
- cout << "1、容器是否为空:" << (v1.empty() ? "容器为空" : "容器不为空") << endl;
-
- for (int i = 0; i < 5; i++)
- {
- v1.push_back(i + 1);
- }
-
- cout << "1、容器是否为空:" << (v1.empty() ? "容器为空" : "容器不为空") << endl;
- cout << "1、容器元素:" << endl;
- print(v1);
-
- //2、capacity();
- cout << "2、容器容量:" << v1.capacity() << endl;
-
- //3、size();
- cout << "3、容器中元素个数:" << v1.size() << endl;
-
- //4、resize(int num);
- v1.resize(7);
- cout << "4、容器长度变长后元素:" << endl;
- print(v1);
- v1.resize(4);
- cout << "4、容器长度变短后元素:" << endl;
- print(v1);
-
- //5、resize(int num, elem);
- v1.resize(7, 8);
- cout << "5、容器长度变长后元素:" << endl;
- print(v1);
- v1.resize(5);
- cout << "5、容器长度变短后元素:" << endl;
- print(v1);
-
- system("pause");
-
- return 0;
- }
输出结果
1、容器是否为空:容器为空
1、容器是否为空:容器不为空
1、容器元素:
1 2 3 4 5
2、容器容量:6
3、容器中元素个数:5
4、容器长度变长后元素:
1 2 3 4 5 0 0
4、容器长度变短后元素:
1 2 3 4
5、容器长度变长后元素:
1 2 3 4 8 8 8
5、容器长度变短后元素:
1 2 3 4 8
函数原型
插入元素
- push_back(ele); //尾部插入元素
- insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
- insert(const_iterator pos, int count, ele); //迭代器指向位置pos插入count个元素ele
删除元素
- pop_back(); //删除最后一个元素
- erase(const_iterator pos); //删除迭代器指向的元素
- earse(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
- clear(); //删除容器中所有元素
- #include
- #include
- using namespace std;
-
- void print(vector<int>& v)
- {
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << "\t";
- }
- cout << endl;
- }
-
- int main()
- {
- // STL - vector - 插入和删除
- /*函数原型
- 插入元素:
- 1、push_back(ele); //尾部插入元素
- 2、insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
- 3、insert(const_iterator pos, int count, ele); //迭代器指向位置pos插入count个元素ele
- 删除元素:
- 4、pop_back(); //删除最后一个元素
- 5、erase(const_iterator pos); //删除迭代器指向的元素
- 6、earse(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
- 7、clear(); //删除容器中所有元素
- */
- vector<int> v1;
- //1、2、3 插入元素;
- v1.push_back(20);
- cout << "1、插入元素后:";
- print(v1);
-
- v1.insert(v1.begin(), 32);
- cout << "2、插入元素后:";
- print(v1);
-
- v1.insert(v1.begin(), 3,50);
- cout << "3、插入元素后:";
- print(v1);
-
- //4、5、6、7 删除元素;
- v1.pop_back();
- cout << "4、删除元素后:";
- print(v1);
-
- v1.erase(v1.begin());
- cout << "5、删除元素后:";
- print(v1);
-
- v1.erase(v1.begin(), v1.end());
- cout << "6、删除元素后:";
- print(v1);
-
- v1.insert(v1.begin(), 3, 50);
- cout << "3、插入元素后:";
- print(v1);
-
- v1.clear();
- cout << "7、删除元素后:";
- print(v1);
-
- system("pause");
-
- return 0;
- }
输出结果
1、插入元素后:20
2、插入元素后:32 20
3、插入元素后:50 50 50 32 20
4、删除元素后:50 50 50 32
5、删除元素后:50 50 32
6、删除元素后:
3、插入元素后:50 50 50
7、删除元素后:
函数原型
- at(int idx); //返回索引idx所指向的数据
- operator[]; //返回索引idx所指向的数据
- front(); //返回容器中第一个数据元素
- back(); //返回容器中最后一个数据元素
- #include
- #include
- using namespace std;
-
- void print(vector<int>& v)
- {
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << "\t";
- }
- cout << endl;
- }
-
- int main()
- {
- // STL - vector - 数据读取
- /*函数原型
- 1、at(int idx); //返回索引idx所指向的数据
- 2、operator[]; //返回索引idx所指向的数据
- 3、front(); //返回容器中第一个数据元素
- 4、back(); //返回容器中最后一个数据元素
- */
- vector<int> v1;
- for (int i = 0; i < 5; i++)
- {
- v1.push_back(i * 5);
- }
- cout << "容器中的元素:";
- print(v1);
-
- //1、at(int idx);
- cout << "1、读取第1个元素:" << v1.at(0) << endl;
-
- //2、operator[];
- cout << "2、读取第2个元素:" << v1[1] << endl;
-
- //3、front();
- cout << "3、读取容器中第1个元素:" << v1.front() << endl;
-
- //4、back();
- cout << "4、读取容器中最后1个元素:" << v1.back() << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
容器中的元素:0 5 10 15 20
1、读取第1个元素:0
2、读取第2个元素:5
3、读取容器中第1个元素:0
4、读取容器中最后1个元素:20
可以巧妙使用互换容器收缩内存空间,使用vector匿名对象与当前vector互换容器,匿名对象特点是当前执行完,编译器立即回收内存空间,因此互换后之间的空间被释放了。
函数原型
- swap(vec); //将vec与本身的元素互换
- #include
- #include
- using namespace std;
-
- void print(vector<int>& v)
- {
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << "\t";
- }
- cout << endl;
- }
-
- int main()
- {
- // STL - vector - 互换容器
- /*函数原型
- swap(vec); //将vec与本身的元素互换
- */
-
- vector<int> v1;
- for (int i = 0; i < 5; i++)
- {
- v1.push_back(i * 5);
- }
- cout << "交换前,v1 容器中的元素:";
- print(v1);
- cout << "交换前,v1 容器容量:" << v1.capacity() << ", 容器大小:" << v1.size() << endl;
-
- vector<int> v2;
- for (int i = 0; i < 7; i++)
- {
- v2.push_back(i + 5);
- }
- cout << "交换前,v2 容器中的元素:";
- print(v2);
- cout << "交换前,v2 容器容量:" << v2.capacity() << endl;
- cout << "交换前,v2 容器大小:" << v2.size() << endl;
-
- v1.swap(v2);
-
- cout << "交换后,v1 容器中的元素:";
- print(v1);
- cout << "交换后,v1 容器容量:" << v1.capacity() << ", 容器大小:" << v1.size() << endl;
-
- cout << "交换后,v2 容器中的元素:";
- print(v2);
- cout << "交换后,v2 容器容量:" << v2.capacity() << ", 容器大小:" << v2.size() << endl;
-
- //可以巧妙使用互换容器收缩内存空间,使用vector匿名对象与当前vector互换容器,
- //匿名对象特点是当前执行完,编译器立即回收内存空间,因此互换后之间的空间被释放了。
- vector<int> v3;
- for (int i = 0; i < 5000; i++)
- {
- v3.push_back(i);
- }
- cout << "交换前,v3 容器容量:" << v3.capacity() << ", 容器大小:" << v3.size() << endl;
-
- v3.resize(5);
- cout << "重置resize容器大小" << endl;
- cout << "重置后,v3 容器容量:" << v3.capacity() << ", 容器大小:" << v3.size() << endl;
-
- //vector
(v3)为使用拷贝构造函数生成一个匿名对象,使用匿名对象调用swap函数 - vector<int>(v3).swap(v3);
- cout << "与匿名对象交换后,v3 容器容量:" << v3.capacity() << ", 容器大小:" << v3.size() << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
交换前,v1 容器中的元素:0 5 10 15 20
交换前,v1 容器容量:6, 容器大小:5
交换前,v2 容器中的元素:5 6 7 8 9 10 11
交换前,v2 容器容量:9
交换前,v2 容器大小:7
交换后,v1 容器中的元素:5 6 7 8 9 10 11
交换后,v1 容器容量:9, 容器大小:7
交换后,v2 容器中的元素:0 5 10 15 20
交换后,v2 容器容量:6, 容器大小:5
交换前,v3 容器容量:5395, 容器大小:5000
重置resize容器大小
重置后,v3 容器容量:5395, 容器大小:5
与匿名对象交换后,v3 容器容量:5, 容器大小:5
减少vector在动态扩展容量时的扩展次数,函数原型:
- reserve(int len); //容器预留len个元素长度,预留位置不初始化,元素不可访问
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - vector - 预留空间
- /*函数原型
- reserve(int len); //容器预留len个元素长度,预留位置不初始化,元素不可访问
- */
-
- vector<int> v1;
- int* p = NULL;
- int count = 0;
- //计算初始化5000个元素,会分配多少次内存空间
- for (int i = 0; i < 5000; i++)
- {
- v1.push_back(i);
- if (p != &v1[0])
- {
- p = &v1[0];
- count++;
- }
- }
- cout << "分配内存空间次数:" << count << endl;
-
- //预留空间
- vector<int> v2;
- v2.reserve(5000);
- p = NULL;
- count = 0;
-
- for (int i = 0; i < 5000; i++)
- {
- v2.push_back(i);
- if (p != &v2[0])
- {
- p = &v2[0];
- count++;
- }
- }
- cout << "预留空间后,分配内存空间次数:" << count << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
分配内存空间次数:22
预留空间后,分配内存空间次数:1
- #include
- #include
- #include
- using namespace std;
-
- void print(int val)
- {
- cout << val << "\t";
- }
-
- int main()
- {
- // STL - vector - 存放内置数据类型
- //创建vetor容器对象,并且通过模板参数指定容器中存放的数据类型
- vector<int> v;
- //向容器中存放数据
- v.push_back(21);
- v.push_back(15);
- v.push_back(87);
-
- //v.begin()返回迭代器,这个迭代器指向容器中第一个数据
- //vector
::iterator 拿到vector这个容器的迭代器类型 - vector<int>::iterator itBegin = v.begin();
- //v.end()返回迭代器,这个迭代器指向容器中最后一个元素的下一个位置
- vector<int>::iterator itEnd = v.end();
-
- cout << "--- 遍历方式一 while ---" << endl;
-
- //遍历方式一
- while (itBegin != itEnd)
- {
- cout << *itBegin << "\t";
- itBegin++;
- }
- cout << endl;
-
- cout << endl << "--- 遍历方式二 for ---" << endl;
-
- //遍历方式二
- for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
- {
- cout << *it << "\t";
- }
- cout << endl;
-
- cout << endl << "--- 遍历方式二 for_each ---" << endl;
-
- //遍历方式三 - STL提供的遍历算法
- for_each(v.begin(), v.end(), print);
-
- system("pause");
-
- return 0;
- }
输出结果
--- 遍历方式一 while ---
21 15 87
--- 遍历方式二 for ---
21 15 87
--- 遍历方式二 for_each ---
21 15 87
- #include
- #include
- using namespace std;
-
- //自定义类型
- class Person
- {
- public:
- string name;
- int age;
-
- Person(string _name, int _age): name(_name), age(_age) {}
- };
-
- int main()
- {
- // STL - vector - 存放自定义数据类型
- Person p1("Tracy", 20);
- Person p2("Timo", 27);
- Person p3("Alice", 15);
-
- cout << "--- vector存放对象 ---" << endl;
-
- vector
v1; -
- v1.push_back(p1);
- v1.push_back(p2);
- v1.push_back(p3);
-
- //遍历
- for (vector
::iterator it = v1.begin(); it < v1.end(); it++) - {
- cout << "name = " << it->name << ", age = " << (*it).age << endl;
- }
-
- cout << endl << "--- vector存放对象指针 ---" << endl;
-
- vector
v2; -
- v2.push_back(&p1);
- v2.push_back(&p2);
- v2.push_back(&p3);
-
- //遍历
- for (vector
::iterator it = v2.begin(); it < v2.end(); it++) - {
- cout << "name = " << (*it)->name << ", age = " << (*it)->age << endl;
- }
-
- system("pause");
-
- return 0;
- }
输出结果
--- vector存放对象 ---
name = Tracy, age = 20
name = Timo, age = 27
name = Alice, age = 15
--- vector存放对象指针 ---
name = Tracy, age = 20
name = Timo, age = 27
name = Alice, age = 15
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - vector - 容器嵌套容器
- vector
int>> v; -
- vector<int> v1;
- vector<int> v2;
- vector<int> v3;
-
- for (int i = 0; i < 3; i++)
- {
- v1.push_back(i + 1);
- v2.push_back(i + 5);
- v3.push_back(i + 10);
- }
-
- v.push_back(v1);
- v.push_back(v2);
- v.push_back(v3);
-
- //遍历
- for (vector
int>>::iterator it = v.begin(); it < v.end(); it++) - {
- for (vector<int>::iterator vit = (*it).begin(); vit < (*it).end(); vit++)
- {
- cout << *vit << "\t";
- }
- cout << endl;
- }
-
- system("pause");
-
- return 0;
- }
输出结果
1 2 3
5 6 7
10 11 12
string是C++风格的字符串,本质上是一个类。
string和char*区别:
- char*是一个指针
- string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
string特点:
- string类内部封装了很多成员方法,如find、copy、delete、replace、insert等。
- string管理char*所分配的内存,不用担心复制越界和取值越界等问题,由类内部负责处理
string构造函数原型:
- string(); //创建一个空的字符 串,如 string str;
- string(const char* s); //使用字符串s初始化
- string(const string& str); //使用一个string对象初始化另一个string对象
- string(int n, char c); //使用n个字符c初始化
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 构造函数
- /*构造原型:
- 1、string(); //创建一个空的字符 串,如 string str;
- 2、string(const char* s); //使用字符串s初始化
- 3、string(const string& str); //使用一个string对象初始化另一个string对象
- 4、string(int n, char c); //使用n个字符c初始化
- */
- //1、string();
- string s1;
- cout << "s1 = " << s1 << endl;
-
- //2、string(const char* s);
- const char* c = "I am Tracy";
- string s2(c);
- cout << "s2 = " << s2 << endl;
-
- //3、string(const string& str);
- string s3(s2);
- cout << "s3 = " << s3 << endl;
-
- //4、string(int n, char c);
- string s4(5, 'k');
- cout << "s4 = " << s4 << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
s1 =
s2 = I am Tracy
s3 = I am Tracy
s4 = kkkkk
赋值函数原型:
- string& operator=(const char* s); //char*类型字符串赋值给当前字符串
- string& operator=(const string& s); //把字符串s赋值给当前字符串
- string& operator=(char c); //字符赋值给当前字符串
- string& assign(const char* s); //把字符串s赋值给当前字符串
- string& assign(const char *s, int n); //把字符串s的前n个字符赋值给当前字符串
- string& assign(const string& s); //把字符串s赋值给当前字符串
- string& assign(int n, char c); //用n个字符c赋值给当前字符串
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 赋值操作
- /*赋值函数原型:
- 1、string& operator=(const char* s); //char*类型字符串赋值给当前字符串
- 2、string& operator=(const string& s); //把字符串s赋值给当前字符串
- 3、string& operator=(char c); //字符赋值给当前字符串
- 4、string& assign(const char* s); //把字符串s赋值给当前字符串
- 5、string& assign(const char *s, int n); //把字符串s的前n个字符赋值给当前字符串
- 6、string& assign(const string& s); //把字符串s赋值给当前字符串
- 7、string& assign(int n, char c); //用n个字符c赋值给当前字符串赋值操作
- */
-
- //1、string& operator=(const char* s);
- const char* s = "Hello, I am Tray.";
- string s1 = s;
- cout << "s1 = " << s1 << endl;
-
- //2、string& operator=(const string& s);
- string s2 = s1;
- cout << "s2 = " << s2 << endl;
-
- //3、string& operator=(char c);
- string s3 = "w";
- cout << "s3 = " << s3 << endl;
-
- //4、string& assign(const char* s);
- string s4;
- s4.assign(s);
- cout << "s4 = " << s4 << endl;
-
- //5、string& assign(const char *s, int n);
- string s5;
- s5.assign(s, 5);
- cout << "s5 = " << s5 << endl;
-
- //6、string& assign(const string& s);
- string s6;
- s6.assign(s5);
- cout << "s6 = " << s6 << endl;
-
- //7、string& assign(int n, char c);
- string s7;
- s7.assign(5, 'k');
- cout << "s7 = " << s7 << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
s1 = Hello, I am Tray.
s2 = Hello, I am Tray.
s3 = w
s4 = Hello, I am Tray.
s5 = Hello
s6 = Hello
s7 = kkkkk
实现字符串在结尾处拼接,字符串拼接函数原型:
- string& operator+=(const char* str); //重载+=操作符
- string& operator+=(const char c); //重载+=操作符
- string& operator+=(const string& str); //重载+=操作符
- string& append(const char *s); //把字符串s连接到当前字符串结尾
- string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
- string& append(const string &s); //同operator+=(const string& str);
- string& append(const string& s, int pos, int n); //字符串s中从pos开始的n个字符连接到当前字符串结尾
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 字符串拼接
- /*字符串拼接函数原型:
- 1、string& operator+=(const char* str); //重载+=操作符
- 2、string& operator+=(const char c); //重载+=操作符
- 3、string& operator+=(const string& str); //重载+=操作符
- 4、string& append(const char *s); //把字符串s连接到当前字符串结尾
- 5、string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
- 6、string& append(const string &s); //同operator+=(const string& str);
- 7、string& append(const string& s, int pos, int n); //字符串s中从pos开始的n个字符连接到当前字符串结尾
- */
- const char* c = "I am busy.";
- //1、string& operator+=(const char* str);
- string s1 = "I";
- s1 += " am Tracy.";
- cout << "s1 = " << s1 << endl;
-
- //2、string& operator+=(const char c);
- s1 += c;
- cout << "s1 = " << s1 << endl;
-
- //3、string& operator+=(const string& str);
- string s2 = "I am working.";
- s1 += s2;
- cout << "s1 = " << s1 << endl;
-
- //4、string& append(const char *s);
- s2.append(c);
- cout << "s2 = " << s2 << endl;
-
- //5、string& append(const char *s, int n);
- s2.append("I can finish my work.", 5);
- cout << "s2 = " << s2 << endl;
-
- //6、string& append(const string &s);
- string s3 = "Hello,";
- s3.append(s2);
- cout << "s3 = " << s3 << endl;
-
- //7、string& append(const string& s, int pos, int n);
- s1.append(s3, 11,4);
- cout << "s1 = " << s1 << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
s1 = I am Tracy.
s1 = I am Tracy.I am busy.
s1 = I am Tracy.I am busy.I am working.
s2 = I am working.I am busy.
s2 = I am working.I am busy.I can
s3 = Hello,I am working.I am busy.I can
s1 = I am Tracy.I am busy.I am working.work
查找指定字符串是否存在,替换为在指定位置替换字符串,函数原型如下:
查找(未找到返回-1):
- int find(const string& str, int pos = 0) const; //查找str第一次出现的位置,从pos开始查找
- int find(const char* s, int pos = 0) const; //查找s第一次出现的位置,从pos开始查找
- int find(const char* s int pos, int n) const; //从pos位置查找 s的前n个字符第一次出现的位置
- int find(const char c, int pos = 0) const; //查找字符c第一次出现的位置
- int rfind(const string& str, int pos = npos) const; //查找str最后一次出现的位置,从pos开始查找
- int rfind(const char* s, int pos = npos) const; //查找s最后一次出现的位置,从pos开始查找
- int rfind(const char* s, int pos, int n) const; //从pos查找 s的前n个字符最后一次出现的位置
- int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现的位置
替换:
- string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
- string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 查找和替换
- /*函数原型:
- 查找:
- 1、int find(const string& str, int pos = 0) const; //查找str第一次出现的位置,从pos开始查找
- 2、int find(const char* s, int pos = 0) const; //查找s第一次出现的位置,从pos开始查找
- 3、int find(const char* s int pos, int n) const; //从pos位置查找 s的前n个字符第一次出现的位置
- 4、int find(const char c, int pos = 0) const; //查找字符c第一次出现的位置
- 5、int rfind(const string& str, int pos = npos) const; //查找str最后一次出现的位置,从pos开始查找
- 6、int rfind(const char* s, int pos = npos) const; //查找s最后一次出现的位置,从pos开始查找
- 7、int rfind(const char* s, int pos, int n) const; //从pos查找 s的前n个字符最后一次出现的位置
- 8、int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现的位置
- 替换
- 9、string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
- 10、string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
- */
-
- string s = "I am happy,I am working now with happiness.";
- string sf = "ha";
- const char* sc = "am";
- //1、int find(const string & str, int pos = 0) const;
- cout << "1、 pos = " << s.find(sf) << endl;
- cout << "1、 pos = " << s.find(sf, 4) << endl;
-
- int pos = s.find("win");
- cout << "1、未找到时, pos = " << pos << endl;
-
- //2、int find(const char* s, int pos = 0) const;
- cout << "2、 pos = " << s.find(sc) << endl;
- cout << "2、 pos = " << s.find(sc, 1) << endl;
-
- //3、int find(const char* s int pos, int n) const;
- cout << "3、 pos = " << s.find(sc, 10, 2) << endl;
-
- //4、int find(const char c, int pos = 0) const;
- cout << "4、 pos = " << s.find('a') << endl;
- cout << "4、 pos = " << s.find('a', 10) << endl;
-
- //5、int rfind(const string & str, int pos = npos) const;
- cout << "5、 pos = " << s.rfind(sf) << endl;
- cout << "5、 pos = " << s.rfind(sf, 15) << endl;
-
- //6、int rfind(const char* s, int pos = npos) const;
- cout << "6、 pos = " << s.rfind(sc) << endl;
- cout << "6、 pos = " << s.rfind(sc, 12) << endl;
-
- //7、int rfind(const char* s, int pos, int n) const;
- cout << "7、 pos = " << s.rfind(sc, 10, 2) << endl;
-
- //8、int rfind(const char c, int pos = 0) const;
- cout << "8、 pos = " << s.rfind('p') << endl;
- cout << "8、 pos = " << s.rfind('p', 15) << endl;
-
- //9、string & replace(int pos, int n, const string & str);
- s.replace(5, 5, sf);
- cout << "9、s = " << s << endl;
-
- //10、string & replace(int pos, int n, const char* s);
- s.replace(5, 2, "happy");
- cout << "10、s = " << s << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
1、 pos = 5
1、 pos = 5
1、未找到时, pos = -1
2、 pos = 2
2、 pos = 2
3、 pos = 13
4、 pos = 2
4、 pos = 13
5、 pos = 33
5、 pos = 5
6、 pos = 13
6、 pos = 2
7、 pos = 2
8、 pos = 36
8、 pos = 8
9、s = I am ha,I am working now with happiness.
10、s = I am happy,I am working now with happiness.
字符串比较是按字符的ASCII码进行比较的,相等=返回0, 大于>返回1,小于<返回-1,函数原型如下:
- int compare(const string& s) const;
- int compare(const char* s) const;
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 字符串比较
- /*函数原型:
- 1、int compare(const string& s) const;
- 2、int compare(const char* s) const;
- */
-
- string s = "I am happy";
- string s1 = "I am happy";
- string s2 = "I am happy.";
- string s3 = "I am busy.";
- //1、int compare(const string& s) const;
- cout << "1、比较结果:" << s.compare(s1) << endl;
- cout << "1、比较结果:" << s.compare(s2) << endl;
- cout << "1、比较结果:" << s.compare(s3) << endl;
-
- //2、int compare(const char* s) const;
- cout << "2、比较结果:" << s.compare("I am happy") << endl;
- cout << "2、比较结果:" << s.compare("I am happy.") << endl;
- cout << "2、比较结果:" << s.compare("I am busy") << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
1、比较结果:0
1、比较结果:-1
1、比较结果:1
2、比较结果:0
2、比较结果:-1
2、比较结果:1
string中单个字符存取有两种方式:
- char& operator[](int n); //通过[]运算符重载方式取获取字符
- char& at(int n); //通过at方法获取字符
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 字符存取
- /*函数原型:
- 1、char& operator[](int n); //通过[]运算符重载方式取获取字符
- 2、char& at(int n); //通过at方法获取字符
- */
-
- cout << "---- 获取字符 ----" << endl;
-
- string s = "operator";
- //1、char& operator[](int n);
- cout << "1、获取第3个字符:" << s[2] << endl;
-
- //遍历所有字符
- for (int i = 0; i < s.size(); i++)
- {
- cout << s[i] << "\t";
- }
-
- cout << endl;
-
- //2、char& at(int n);
- cout << "2、获取第3个字符:" << s.at(2) << endl;
- //cout << "2、获取第3个字符:" << s.at(21) << endl; //若获取越界字符,运行时将抛出异常
-
- //遍历所有字符
- for (int i = 0; i < s.size(); i++)
- {
- cout << s.at(i) << "\t";
- }
-
- cout << endl;
-
-
- cout << endl << "---- 修改字符 ----" << endl;
- s[3] = 'x';
- cout << "1、修改后字符串 = " << s << endl;
-
- s.at(5) = 'y';
- cout << "2、修改后字符串 = " << s << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
---- 获取字符 ----
1、获取第3个字符:e
o p e r a t o r
2、获取第3个字符:e
o p e r a t o r---- 修改字符 ----
1、修改后字符串 = opexator
2、修改后字符串 = opexayor
函数原型:
插入:
- string& insert(int pos, const char* s); //插入字符串
- string& insert(int pos, const string& str); //插入字符串
- string& insert(int pos, int n, char c); //在指定位置插入n个字符c
删除:
- string& erase(int pos, int n= npos); //删除从pos开始的n个字符
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 插入和删除
- /*函数原型:
- 插入:
- 1、string& insert(int pos, const char* s); //插入字符串
- 2、string& insert(int pos, const string& str); //插入字符串
- 3、string& insert(int pos, int n, char c); //在指定位置插入n个字符c
- 删除:
- 4、string& erase(int pos, int n= npos); //删除从pos开始的n个字符
- */
- string s = "happy";
- string si = " too.";
- //1、string& insert(int pos, const char* s);
- s.insert(0, " am ");
- cout << "1、s = " << s << endl;
-
- //2、string& insert(int pos, const string& str);
- s.insert(9, si);
- cout << "2、s = " << s << endl;
-
- //3、string& insert(int pos, int n, char c);
- s.insert(0, 3, 'I');
- cout << "3、s = " << s << endl;
-
- //4、string& erase(int pos, int n= npos);
- s.erase(0, 2);
- cout << "4、s = " << s << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
1、s = am happy
2、s = am happy too.
3、s = III am happy too.
4、s = I am happy too.
从字符串中获取想要的子串,函数原型:
- string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
- #include
- #include
- using namespace std;
-
- int main()
- {
- // STL - string - 子串获取
- /*函数原型:
- string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
- */
- string s = "happy2022@gmail.com";
-
- int pos = s.find("@");
-
- cout << s.substr(0, pos) << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
happy2022