stl初识
馨er BOSS

stl总体的简单认识

1 STL初识

1.1 STL六大组件

容器,算法,迭代器,仿函数,适配器(配接器),空间配置器

容器: 各种数据结构,如vector,list,deque,set,map等,用来存放数据

  • 序列式容器:强调值的排序,序列式容器中每个元素均有固定的位置
  • 关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系

算法: 各种常用的算法,如sort,find,copy,for_each等。分为质变算法和非质变算法

  • 质变算法:是指运算过程中会更改区间的元素的内容。例如拷贝,替换,删除等等
  • 非质变算法:是指运算过程中不会更改区间的元素内容,例如查找,计数,遍历,寻找极值等等

迭代器: 扮演了容器与算法之间的胶合剂

仿函数: 行为类似函数,可作为算法的某种策略

适配器: 一种用来修饰容器或者仿函数或迭代器接口的东西

空间配置器: 负责空间的配置和管理

1.2 容器算法迭代器初识

1.2.1 vector存放内置数据类型

容器:vector

算法:for_each

迭代器:vector::iterator

v.begin()返回迭代器,这个迭代器指向容器中的第一个元素
v.end()返回迭代器,这个迭代器指向容器元素的最后一个元素的下一个位置
v.rend()返回迭代器,这个迭代器指向容器中的第一个元素的上一个位置
v.rbegin()返回迭代器,这个迭代器指向容器中的最后一个元素
insert()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<vector>
#include<algorithm>//标准算法头文件
using namespace std;

void myPrint(int val) {
cout << val << endl;
}

//vector容器存放
void test1() {
//创建了一个vector容器,数组
vector<int> v;
//向容器插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//每一个容器都有自己的迭代器,迭代器是用来遍历容器中的元素
//vector<int>::iterator拿到vector<int>这种容器的迭代器类型
//通过迭代器来访问容器中的数据
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
//第一种遍历方式
while (itBegin != itEnd) {
cout << *itBegin << endl;
itBegin++;
}
//第二种遍历方式
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
//第三种遍历方式
for_each(v.begin(), v.end(), myPrint);
}

int main() {
test1();
system("pause");
}

1.2.2 vector存放自定义数据类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

//vector容器中存放自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};

void test1() {
vector<Person>v;
Person p1("aaa", 10);
Person p2("aaa", 20);
Person p3("aaa", 30);
Person p4("aaa", 40);
Person p5("aaa", 50);
//向容器中添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历容器中的数据
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
//cout << "姓名:" << (*it).m_Name << "年龄:" << (*it).m_Age << endl;
cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
}
}

//存放自定义数据类型,指针
void test2() {
vector<Person*>v;
Person p1("aaa", 10);
Person p2("aaa", 20);
Person p3("aaa", 30);
Person p4("aaa", 40);
Person p5("aaa", 50);
//向容器中添加数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器中的数据
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl;
}
}

int main() {
//test1();
test2();
return 0;
}

1.2.3 vector容器嵌套容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<vector>
using namespace std;

//容器嵌套容器
void test1() {
//创建大容器
vector<vector<int>>v;
//创建小容器
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
//向小容器中添加数据
for (int i = 0; i < 4; i++) {
v1.push_back(i + 1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
//将小容器插入到大容器中
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//通过大容器,把所有数据遍历一遍
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++) {
//(*it)-----容器vector<int>
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << *vit << " ";
}
cout << endl;
}
}

int main() {
test1();
system("pause");
}

2 vector

2.1 vector基本概念

功能:vector数据结构和数组非常相似,也称为单端数组

区别:数组是静态空间,而vector可以动态扩展

动态扩展:并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间。vector迭代器是支持随机访问的迭代器

2.2 vector构造函数

功能:创建vector容器

函数原型:

  • vector<T> v; //采用模板实现类实现,默认构造函数
  • vector(v.begin(),v.end()); //将v[begin(),end())区间中的元素拷贝给本身(注意区间为前闭后开)
  • vector(n,elem); //构造函数将n个elem拷贝给本身
  • vector(const vector &vec); //拷贝构造函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<vector>
using namespace std;

void printVector(vector<int>&v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}

//vector容器的构造
void test1() {
vector<int>v1;//默认构造 无参构造
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printVector(v1);
//通过区间方式进行构造
vector<int>v2(v1.begin(), v1.end());
printVector(v2);//均输出0到9
//n个elem方式构造
vector<int>v3(10, 100);
printVector(v3);//输出10个100
//拷贝构造
vector<int>v4(v3);
printVector(v4);
}

int main() {
test1();
return 0;
}

2.3 vector赋值操作

函数原型:

  • vector& operator=(const vector &vec); //重载等号操作符
  • assign(beg,end); //将[beg,end)区间中的数据拷贝赋值给本身
  • assign(n,elem); //将n个elem拷贝赋值给本身
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<vector>
using namespace std;

void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}

//vector赋值
void test1() {
vector<int>v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printVector(v1);
//赋值 operator=
vector<int>v2;
v2 = v1;
printVector(v2);
//assign
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
//n个elem方式赋值
vector<int>v4;
v4.assign(10, 100);
printVector(v4);
}

int main() {
test1();
return 0;
}

2.4 vector容量和大小

函数原型:

empty();//判断容器是否为空

capacity(); //容器的容量

size(); //返回容器中元素的个数

resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置;若容器变短,则末尾超出容器长度的元素被删除

resize(int num,elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置;若容器变短,则末尾超出容器长度的元素被删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<vector>
using namespace std;

void printVector(vector<int>&v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}

//vector容器的容量和大小操作
void test1() {
vector<int>v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printVector(v1);
if (v1.empty()) {
cout << "v1为空" << endl;
}
else {
cout << "v1不为空" << endl;
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
//重新指定大小
v1.resize(15);//利用重载版本,可以指定默认填充值,参数2
printVector(v1);//如果重新指定的比原来长了,默认用0填充原来的位置
v1.resize(5);
printVector(v1);//如果重新指定的比原来的短了,超出部分会删除掉
}

int main() {
test1();
return 0;
}

3 string

3.1 string基本概念

  • string是C++风格的字符串,而string本质上是一个类,类内部封装了char ,管理这个字符串,是一个char 型的容器
  • char*是一个指针

特点:

  • string类内部封装了很多成员方法,例如:查找find,拷贝copy,删除delete替换repliace,插入insert
  • string管理char *所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

3.2 string构造函数

构造函数原型:

  • string(); //创建一个空的字符串 例如:string str;

    string(const char* s); //使用字符串s初始化

  • string(const string& str); //使用一个string对象初始化另一个string对象

  • string(int n,char c) //使用n个字符c初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<cstring>
using namespace std;

//string的构造函数
void test1() {
string s1;//默认构造
const char* str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}

int main() {
test1();
return 0;
}

3.3 string赋值操作

赋值的函数原型:

  • 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 char& s); //把字符串s赋给当前的字符串
  • string& assign(int n,char c); //用n个字符c赋给当前字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<cstring>
using namespace std;

void test1() {
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("hello c++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello c++", 5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(10, 'w');
cout << "str7 = " << str7 << endl;
}

int main() {
test1();
return 0;
}

operator方式较为常见

3.4 string字符串拼接

函数原型:

  • 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个字符连接到字符串结尾
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<cstring>
using namespace std;

void test1() {
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" love ");
cout << "str3 = " << str3 << endl;
str3.append("game abcde", 4);
cout << "str3 = " << str3 << endl;
//str3.append(str2);
str3.append(str2, 4, 6);
cout << "str3 = " << str3 << endl;
}

int main() {
test1();
system("pause");
}

3.5 string查找和替换

查找:查找指定字符串是否存在

替换:在指定位置替换字符串

函数原型:

  • 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位置查找的前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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<cstring>
using namespace std;

//1. 查找
void test1() {
string str1 = "abcdefg";
int pos = str1.find("de");
if (pos == -1) {
cout << "未找到字符串" << endl;
}
else {
cout << "找到字符串,pos = " << pos << endl;
}
//rfind
str1.rfind("de");
cout << "pos = " << pos << endl;
}

//2. 替换
void test2() {
string str1 = "abcdefg";
str1.replace(1, 3, "1111");//此处替换4个1
cout << "str1 = " << str1 << endl;
}

int main() {
//test1();
test2();
return 0;
}
  • find从左往右查,rfind从右往左查找
  • find找到字符串后返回查找的第一个字符位置,找不到返回-1
  • replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

3.6 string字符串比较

根据ASCII码比较,=返回0,>返回1,<返回-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;

//字符串比较
void test1() {
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0) {
cout << "str1等于str2" << endl;
}
else if (str1.compare(str2) > 0) {
cout << "str1大于str2" << endl;
}
else {
cout << "str1小于str2" << endl;
}
}

int main() {
test1();
return 0;
}

3.7 string字符存取

  • char& operator[](int n); //通过[]方式取字符
  • char& at(int n); //通过at方法获取字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>

using namespace std;

//string字符存取
void test1() {
string str = "hello";
cout << "str = " << str << endl;
//1 通过[]访问单个字符
for (int i = 0; i < str.size(); i++) {
cout << str[i] << " ";
}
cout << endl;
//2 通过at方式访问单个字符
for (int i = 0; i < str.size(); i++) {
cout << str.at(i) << " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << "str = " << str << endl;
//xello
str.at(1) = 'x';
cout << "str = " << str << endl;
}

int main() {
test1();
return 0;
}

string插入和删除

  • 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个字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<cstring>
using namespace std;

//字符串的插入和删除
void test1() {
string str = "hello";
//插入
str.insert(1, "111");
//h111ello
cout << "str = " << str << endl;
//删除
str.erase(1, 3);
cout << "str = " << str << endl;
}

int main() {
test1();
return 0;
}

插入和删除的起始下标都是从0开始

string子串

从字符串中获取想要的子串

函数原型:

  • string substr(int pos = 0,int n=npos) const; //返回由pos开始的n个字符组组成的字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<cstring>
using namespace std;

//string求子串
void test1() {
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
}

//实用操作
void test2() {
string email = "zhangsan@sina.com";
//从邮件地址中 获取 用户名信息
int pos = email.find("@");
string userName = email.substr(0, pos);
cout << "userName = " << userName << endl;
}

int main() {
test1();
test2();
return 0;
}
  • 本文标题:stl初识
  • 本文作者:馨er
  • 创建时间:2021-04-20 21:18:23
  • 本文链接:https://sjxbbd.vercel.app/2021/04/20/030a2439c3a5/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!