// Created by AchesonD16 贪玩巴斯 on 2021/8/6.
/*
1 STL初识
1.1 STL的诞生
长久以来,软件界一直希望建立一种可重复利用的东西
C++的面向对象和泛型编程思想,目的就是复用性的提升
大多情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作
为了建立数据结构和算法的一套标准,诞生了STL
1.2 STL基本概念
STL(Standard Template Library,标准模板库)
STL 从广义上分为: 容器(container) 算法(algorithm) 迭代器(iterator)
容器和算法之间通过迭代器进行无缝连接。
STL 几乎所有的代码都采用了模板类或者模板函数
1.3 STL六大组件
STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。
算法:各种常用的算法,如sort、find、copy、for_each等
迭代器:扮演了容器与算法之间的胶合剂。
仿函数:行为类似函数,可作为算法的某种策略。
适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。
空间配置器:负责空间的配置与管理。
1.4 STL中容器、算法、迭代器
1.4.1 **容器:**置物之所也
STL容器就是将运用最广泛的一些数据结构实现出来
常用的数据结构:数组, 链表,树, 栈, 队列, 集合, 映射表 等
这些容器分为序列式容器和关联式容器两种:
序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置。
关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
1.4.2 **算法:**问题之解法也
#include<algorithm>
有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)
算法分为:质变算法和非质变算法。
质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等
非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等
1.4.3 **迭代器:**容器和算法之间粘合剂
提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。
每个容器都有自己专属的迭代器
迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针
1.5 迭代器种类:
种类 功能 支持运算
输入迭代器 对数据的只读访问 只读,支持++、==、!=
输出迭代器 对数据的只写访问 只写,支持++
前向迭代器 读写操作,并能向前推进迭代器 读写,支持++、==、!=
双向迭代器 读写操作,并能向前和向后操作 读写,支持++、–,
随机访问迭代器 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 读写,支持++、–、[n]、-n、<、<=、>、>=
常用的容器中迭代器种类为双向迭代器,和随机访问迭代器
1.6 容器算法迭代器初识
了解STL中容器、算法、迭代器概念之后,我们利用代码感受STL的魅力
STL中最常用的容器为Vector,可以理解为数组,下面我们将学习如何向这个容器中插入数据、并遍历这个容器
*/
/*
1.6.1 vector初认识:
vector存放内置数据类型 可以理解为数组
容器: vector
算法: for_each
迭代器: vector<int>::iterator
*/
/*
// 代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 第三种方式 第三个位置要放入的 打印函数
void myPrint(int val)
{
cout << val << endl;
}
void test01(){
// 创建vector容器对象,并且通过模板参数指定容器中存放的数据类型
vector<int> v;
//使用尾插法
// 尾插法
// void Push_back(const T & val)
// {
// if (this->m_Capacity == this->m_Size)
// {
// return;
// }
// this->pAddress[this->m_Size] = val;
// this->m_Size++;
// }
// 向容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
// 通过迭代器来访问容器中的数据
// 每一个容器都有自己的迭代器,迭代器是用来遍历容器中的元素的
// v.begin() 起始迭代器,这个迭代器指向容器中的第一个元素。
// v.end() 结束迭代器,指向容器中最后一个元素的 下一个位置
// 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; // *it 对迭代去取值的操作
// }
// 第三种遍历方式 利用STL提供遍历算法
for_each(v.begin(), v.end(), myPrint); // 包含算法头文件; 回调函数 要放一个函数最后
}
int main()
{
test01();
}
1.6.2 vector存放自定义数据类型:
// 学习目标:vector中存放自定义数据类型,并打印输出
//代码如下:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
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("吴彦祖", 10);
Person p2("陈冠希", 17);
Person p3("成龙", 44);
Person p4("马化腾", 34);
Person p5("马云", 55);
// 尾插法向容器中添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
// 遍历容器中的数据
// 此处的 s 本身就是一个指针 (*s)可以和<Person>对标来看 两个地方的值是相同的
for(vector<Person>::iterator s = v.begin(); s != v.end(); s++)
{
cout << "通过指针解引用*来" << endl;
cout << "姓名: " << (*s).m_Name << " 年龄: " << (*s).m_Age << endl;
cout << "通过指针->符号来" << endl;
cout << "姓名: " << s->m_Name << " 年龄: " << s->m_Age << endl;
}
}
// 存放自定义数据类型 指针
void test02()
{
vector<Person*> v;
Person p1("吴彦祖", 10);
Person p2("陈冠希", 17);
Person p3("成龙", 44);
Person p4("马化腾", 34);
Person p5("马云", 55);
// 尾插法向容器中添加数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
// 遍历容器
// 此处的 (*it)是一 <Person *> 为一个指针
for(vector<Person *>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "自定义数据类型为指针的 \n姓名: " << (*it)->m_Name << " 年龄: " << (*it)->m_Age << endl;
}
}
int main()
{
//test1();
test02();
system("pause");
return 0;
}
1.6.3 Vector 容器嵌套容器
// 学习目标:容器中嵌套容器,我们将所有数据进行遍历输出
// vector 容器等同于数组,那么嵌套数组则为 二维数组
//代码如下:
#include<iostream>
#include<vector>
using namespace std;
// 容器嵌套容器
void test01()
{
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
for(vector< vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
// 因为vector<vector<int>>是嵌套的——所以我们要先把里面的循环
// 这里面的 (*it) 是指的 <vector<int>>
for(vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++ )
{
cout << *vit << " ";
}
cout << endl;
}
}
int main()
{
test01();
return 0;
}
/*
打印结果
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
*/
/*
2 STL- 常用容器
2.1 string容器
2.1.1 string基本概念
本质:
string是C++风格的字符串,而string本质上是一个类
string和char * 区别:
char * 是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
特点:
string 类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete 替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
2.1.2 string构造函数:
构造函数原型:
string(); //创建一个空的字符串 例如: string str;
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化
代码如下:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
string s1; // 默认构造
// 使用字符串 初始化
const char * str = "hello world";
string s2(str);
cout << " s2 = " <<s2 << endl;
//使用一个string对象初始化另一个string对象
string s3(s2);
cout << " s3 = " << s3 << endl;
string s4(10, 'a'); // 这里注意只能用 '' 单引号
cout << " s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
// 输出结果
//s2 = hello world
//s3 = hello world
//s4 = aaaaaaaaaa
//总结:
//string的多种构造方式没有可比性,灵活使用即可。
2.1.3 string赋值操作:
功能描述:
给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 string &s); //把字符串s赋给当前字符串
string& assign(int n, char c); //用n个字符c赋给当前字符串
//示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
//string& operator=(const char* s); // char*类型字符串 赋值给当前的字符串 ”里的内容“赋给str1
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
// string& operator=(const string &s); // 把字符串赋给当前的字符串
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
// string& operator=(char c); //字符赋值给当前的字符串 即把单个字符赋给字符串
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
// string& assign(const char *s); //把字符串s赋给当前的字符串 运用assign参数
string str4;
str4.assign("hello C++");
cout << "str4 = " << str4 << endl;
// string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
string str5;
str5.assign("hello C++", 5);
cout << "str5 = " << str5 << endl;
// string& assign(const string &s); //把字符串s赋给当前字符串
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
// string& assign(int n, char c); //用n个字符c赋给当前字符串
string str7;
str7.assign(10,'s');
cout << "str7 = " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
str1 = hello world
str2 = hello world
str3 = a
str4 = hello C++
str5 = hello
str6 = hello
str7 = ssssssssss
总结:
string 的赋值方式很多 operator= 使用= 的方式比较实用
2.1.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个字符连接到字符串结尾
//示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 注意符号为 +=
// string& operator+=(const char* str); //重载+=操作符 意思可以直接加上 一段char*
string str1 = "我";
str1 += "爱CSDN";
cout << "str1 = " << str1 << endl;
// string& operator+=(const char c); //重载+=操作符 表示可以加上 一个char字符
str1 += ':';
cout << "str1 = " << str1 << endl;
// string& operator+=(const string& str); //重载+=操作符 表示可以在后面加上 一个string
string str2 = " 因为能学到很多编程知识!";
str1 += str2;
cout << "str1 = " << str1 << endl;
// string& append(const char *s); //把字符串s连接到当前字符串结尾
string str3 = "I";
str3.append(" Love ");
cout << "str3 = " << str3 << endl;
// string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
str3.append("CSDN haha", 4);
cout << "str3 = " << str3 << endl;
// string& append(const string &s); //同operator+=(const string& str) 即在后面加上一段字符串
str3.append(str2);
cout << "str3 = " << str3 << endl;
// string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾 可以只追加一部分
str3.append(str2, 0, 19);
cout << "str3 = " << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/**
总结:
字符串拼接的重载很多,初学只需要掌握几种就好,也不需要死记硬背,需要的时候来贪玩巴斯博客查找即可。
输出如下:
str1 = 我爱CSDN
str1 = 我爱CSDN:
str1 = 我爱CSDN: 因为能学到很多编程知识!
str3 = I Love
str3 = I Love CSDN
str3 = I Love CSDN 因为能学到很多编程知识!
str3 = I Love CSDN 因为能学到很多编程知识! 因为能学到很
*/
2.1.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位置查找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 <iostream>
using namespace std;
#include <string>
//字符串查找和替换
// 1、查找
void test01()
{
// int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找,查找到了返回的为第一个字符串出现的位置
//如果没有查到则为-1;
string str1 = "abcdefgde";
int p = str1.find("de");
if(p == -1)
{
cout << "没找到字符串" << endl;
}
else
{
cout << "找到字符串,pos = " << p << endl;
}
//cout << "pos = " << p << endl;
// rfind
// rfind 和 find 区别:
// rfind 从右往左查找
// find 从左往右查找
// 但是下标位置都从左边开始数
p = str1.rfind("de");
cout << "rfind下的 pos = " << p << endl;
}
// 2、替换
void test02()
{
string str1 = "abcdefg";
// 注意这里替换的 1111会全部插入,并把替换的3个数全部替换掉
// 从1号位置起,3个字符 替换为”1111“
str1.replace(1, 3, "1111");
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
/**
打印如下:
找到字符串,pos = 3
rfind下的 pos = 7
str1 = a1111efg
总结:
find查找是从左往右,rfind从右往左。
find找到字符串之后返回查找的第一个字符位置,找不到则返回-1
replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串。
*/
2.1.6 string字符串比较
功能描述:
字符串之间的比较
比较方式:
字符串比较是按字符的ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
函数原型:
int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
//示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "hello";
string s2 = "xello";
if(s1.compare(s2) == 0)
{
cout << "s1 等于 s2" << endl;
}
else if(s1.compare(s2) > 0 )
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
s1 小于 s2
总结:
字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大。
*/
2.1.7 string字符存取
string中单个字符存取方式有两种
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方法获取字符
//示例:
#include <iostream>
using namespace std;
#include <string>
void test01()
{
string str = "hello world";
for(int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
for(int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
// 字符修改
str[0] = 'x';
str.at(1) = 'x';
cout << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/*
结果:
h e l l o w o r l d
h e l l o w o r l d
xxllo world
总结:
string字符串中单个字符存取有两种方式,利用[]或at
*/
2.1.8 string插入和删除
功能描述:
对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个 字符
// 示例:
#include<iostream>
using namespace std;
#include <string>
void test01()
{
string str = "hello";
// 插入
str.insert(1, "111");
cout << str << endl;
// 删除
str.erase(1, 3);
cout << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果如下:
h111ello
hello
总结:
插入和删除的起始线下标都是从0开始
*/
2.1.9 string子串
功能描述:
从字符串中获取想要的子串
函数原型:
string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
//代码如下:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str = "abcdefg";
string subStr = str.substr(1, 3); // bcd
cout << "subStr = " << subStr << endl;
string email = "tanwanbasi@qq.com";
int pos = email.find("@"); // 会返回这个位置的下标
string username = email.substr(0, pos); // pos的下标位置即表示了这个前面的字符串的个数
cout << "username: " << username << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果如下:
subStr = bcd
username: tanwanbasi
总结:
灵活的运用求子串功能,可以在实际开发中获取有效的信息!
*/
2 .2 vector容器
2.2 vector容器
2.2.1 vector基本概念
功能:
vector数据结构和数组非常相似,也称为单端数组
vector与普通数组区别:
不同之处在于数组是静态空间,而vector可以动态扩展
动态扩展:
并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
注意:
vector容器的迭代器是支持随机访问的迭代器
2.2.2 vector构造函数
功能描述:
创建vector容器
函数原型:
vector<T> v; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
vector(n, elem); //构造函数将n个elem拷贝给本身。
vector(const vector &vec); //拷贝构造函数。
//示例:
#include<iostream>
using namespace std;
#include<vector>
// 创建一个输出打印函数,方便后续打印输出调用
void printVector(vector<int>& v){
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
// vector<T> v; //采用模板实现类实现,默认构造函数
vector<int> v1; //无参构造
for(int i = 0; i < 10; i++)
{
v1.push_back(i); // 尾插法
}
printVector(v1);
// vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
vector<int> v2(v1.begin(), v1.end());
printVector(v2);
// vector(n, elem); //构造函数将n个elem拷贝给本身。
vector<int> v3(10, 100);
printVector(v3);
// vector(const vector &vec); //拷贝构造函数。
vector<int> v4(v3);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}
/*
结果:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100
总结:
vector的多种构造方式没有可比性,灵活使用即可。
*/
2.2.3 vector赋值操作
功能描述:
给vector容器进行赋值
函数原型:
vector& operator=(const vector &vec);//重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
//示例:
#include<iostream>
using namespace std;
#include<vector>
// 创建一个输出打印函数,方便后续打印输出调用
void printVector(vector<int>& v){
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
// vector<T> v; //采用模板实现类实现,默认构造函数
vector<int> v1; //无参构造
for(int i = 0; i < 10; i++)
{
v1.push_back(i); // 尾插法
}
printVector(v1);
// vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
vector<int> v2(v1.begin(), v1.end());
printVector(v2);
// vector(n, elem); //构造函数将n个elem拷贝给本身。
vector<int> v3(10, 100);
printVector(v3);
// vector(const vector &vec); //拷贝构造函数。
vector<int> v4(v3);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
总结:
vector赋值方式比较简单,使用operator= , 或者assign都可以
*/
2.2.4 vector容量和大小
功能描述:
对vector容器的容量和大小操作
函数原型:
empty(); //判断容器是否为空
capacity(); //容器的容量
size(); //返回容器中元素的个数
resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除
//示例:
#include<iostream>
using namespace std;
#include<vector>
// 构造打印函数
void printVector(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
// empty(); //判断容器是否为空
// 此处返回的是 true或false 布尔类型
if(v1.empty())
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
// capacity(); //容器的容量 容量会因为STL中的一个算法而随着存入的值越大而增大,会大于实际的存储值
cout << "v1的容量 = " << v1.capacity() << endl;
// size(); //返回容器中元素的个数 size值表示实际的值
cout << "v1的大小 = " << v1.size() << endl;
}
// resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值 0 填充新位置。
// //如果容器变短,则末尾超出容器长度的元素被删除。
// resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
v1.resize(15);
printVector(v1);
//只有在容器变长的情况下 才能用elem来填充新位置
v1.resize(18, 10);
printVector(v1);
// //如果容器变短,则末尾超出容器长度的元素被删除
v1.resize(5);
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
0 1 2 3 4 5 6 7 8 9
v1不为空
v1的容量 = 16
v1的大小 = 10
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 10 10 10
0 1 2 3 4
总结:
判断是否为空 — empty
返回元素个数 — size
返回容器容量 — capacity
重新指定大小 — resize
*/
2.2.5 vector插入和删除
功能描述:
对vector容器进行插入、删除操作
函数原型:
push_back(ele); //尾部插入元素ele
pop_back(); //删除最后一个元素
insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele
erase(const_iterator pos); //删除迭代器指向的元素
erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
clear(); //删除容器中所有元素
//示例:
#include<iostream>
using namespace std;
#include<vector>
//打印输出函数
void printVector(vector<int> & v){
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//插入和删除
void test01()
{
vector<int> v1;
// 尾插
// push_back(ele); //尾部插入元素ele
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
printVector(v1);
// 尾删
// pop_back(); //删除最后一个元素
v1.pop_back();
printVector(v1);
// 插入
// insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele 注意只能是迭代器
v1.insert(v1.begin(), 100);
printVector(v1);
// insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele 注意只能是迭代器
v1.insert(v1.begin(), 2, 888);
printVector(v1);
// 删除
// erase(const_iterator pos); //删除迭代器指向的元素
v1.erase(v1.begin());
printVector(v1);
// erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
// 具有清空作用
v1.erase(v1.begin(), v1.end());
printVector(v1);
// 清空
// clear(); //删除容器中所有元素
v1.clear();
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
10 20 30 40 50
10 20 30 40
100 10 20 30 40
888 888 100 10 20 30 40
888 100 10 20 30 40
总结:
尾插 —— push_back
尾删 —— pop_back
插入 —— insert(位置迭代器)
清空 —— clear
*/
2.2.6 vector数据存取
功能描述:
对vector中的数据的存取操作
函数原型:
at(int idx); //返回索引idx所指的数据
operator[]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
//示例:
#include<iostream>
#include<vector>
using namespace std;
void test01()
{
// 存入数据
vector<int>v1;
for(int i = 0; i < 10; i++)
{
v1.push_back(i);
}
for(int i = 0; i < v1.size(); i++)
{
// operator[]; //返回索引idx所指的数据
cout << v1[i] << " ";
}
cout << endl;
for(int i = 0; i < v1.size(); i++)
{
// at(int idx); //返回索引idx所指的数据
cout << v1.at(i) << " ";
}
cout << endl;
// front(); //返回容器中第一个数据元素
cout << "v1的第一个元素为: " << v1.front() << endl;
// back(); //返回容器中最后一个数据元素
cout << "v1的最后一个元素为: " << v1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
v1的第一个元素为: 0
v1的最后一个元素为: 9
总结:
除了用迭代器获取vector容器中元素,[ ]和at也可以
front返回容器第一个元素
back返回容器最后一个元素
*/
// vector互换容器
3.2.7 vector互换容器
功能描述:
实现两个容器内元素进行互换
函数原型:
swap(vec); // 将vec与本身的元素互换
//示例:
#include<iostream>
using namespace std;
#include<vector>
//
void printVector(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
// 创建两个容器 v1 v2
vector<int> v1;
for(int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int> v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
// 互换容器
cout << "互换后:" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
void test02()
{
vector<int> v;
for(int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
v.resize(3); // 自定义大小,发现容量仍为131072
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
// 收缩内存 固定格式 把容量缩小为转换的大小
vector<int>(v).swap(v);
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
/**
总结:
swap可以使两个容器互换,可以达到实用的收缩内存的效果
结果:
0 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1
互换后:
10 9 8 7 6 5 4 3 2 1
0 1 2 3 4 5 6 7 8 9
v的容量为:131072
v的大小为:100000
v的容量为:131072
v的大小为:3
v的容量为:3
v的大小为:3
*/
// vector 预留空间
2.2.8 vector预留空间
功能描述:
减少vector在动态扩展容量时的扩展次数
函数原型:
reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
//示例:
#include<iostream>
using namespace std;
#include<vector>
void test01()
{
vector<int> v;
//预留空间
v.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
// 此处使用一个算法 来计算这个循环内 一共开辟几次内存num;
if(p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num: " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
*/
/**
结果:
num: 1
总结:
如果数据量较大,可以一开始利用reserve预留空间
*/
2.3 deque容器
2.3.1 deque容器基本概念
功能:
双端数组,可以对头端进行插入删除操作
deque与vector区别:
vector对于头部的插入删除效率低,数据量越大,效率越低
deque相对而言,对头部的插入删除速度回比vector快
vector访问元素时的速度会比deque快,这和两者内部实现有关
deque内部工作原理:
deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据
中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间
deque容器的迭代器也是支持随机访问的
2.3.2 deque构造函数
功能描述:
deque容器构造
函数原型:
deque<T> deqT; //默认构造形式
deque(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身。
deque(n, elem); //构造函数将n个elem拷贝给本身。
deque(const deque &deq); //拷贝构造函数
//示例:
#include<iostream>
using namespace std;
#include<deque>
// 使用const防止被改变
void printDeque(const deque<int> & d)
{
// 在这里的迭代器必须用 const_iterator 否则会报错
for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// deque构造
void test01()
{
// deque<T> deqT; //默认构造形式
deque<int> d1; //无参构造函数
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
// deque(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身。
deque<int> d2(d1.begin(), d1.end());
printDeque(d2);
// deque(n, elem); //构造函数将n个elem拷贝给本身。
deque<int> d3(10, 100);
printDeque(d3);
// deque(const deque &deq); //拷贝构造函数
deque<int>d4 = d3;
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100
总结:
deque容器和vector容器的构造方式几乎一致,灵活使用即可
*/
2.3.3 deque赋值操作
功能描述:
给deque容器进行赋值
函数原型:
deque& operator=(const deque &deq); //重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
//代码如下:
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int> & d)
{
for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int> d1;
for(int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
// deque& operator=(const deque &deq); //重载等号操作符
deque<int>d2;
d2 = d1;
printDeque(d2);
// assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
// assign(n, elem); //将n个elem拷贝赋值给本身。
deque<int> d4;
d4.assign(10, 888);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
/**
结果:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
888 888 888 888 888 888 888 888 888 888
总结:
deque赋值操作也与vector相同,需熟练掌握
*/
2.3.4 deque大小操作
// 注意deque的大小操作中不存在 capacity容量统计,因为deque 内部有中控器,有缓冲区。
功能描述:
对deque容器的大小进行操作
函数原型:
deque.empty(); //判断容器是否为空
deque.size(); //返回容器中元素的个数
deque.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除。
deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除。
//示例:
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int> & d)
{
for( deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// 大小操作
void test01()
{
deque<int> d1;
for(int i = 0; i < 10; i++)
{
//尾插法
d1.push_back(i);
}
printDeque(d1);
// deque.empty(); //判断容器是否为空 返回true false
// 判断容器是否为空
if(d1.empty()){
cout << "d1为空!" << endl;
}
else{
cout << "d1不为空!" << endl;
// 统计大小 deque.size(); //返回容器中元素的个数
cout << "d1的大小为:" << d1.size() << endl;
}
// 重新指定大小
// deque.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
// deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
// //如果容器变短,则末尾超出容器长度的元素被删除。
d1.resize(15, 1);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}
/**
总结:
deque没有容量的概念
判断是否为空 — empty
返回元素个数 — size
重新指定个数 — resize
结果:
0 1 2 3 4 5 6 7 8 9
d1不为空!
d1的大小为:10
0 1 2 3 4 5 6 7 8 9 1 1 1 1 1
0 1 2 3 4
*/
2.3.5 deque 插入和删除
功能描述:
向deque容器中插入和删除数据
函数原型:
注意只能指定位置为 迭代器的位置 不能够是自定义位置
两端插入操作:
push_back(elem); //在容器尾部添加一个数据
push_front(elem); //在容器头部插入一个数据
pop_back(); //删除容器最后一个数据
pop_front(); //删除容器第一个数据
指定位置操作:
insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
clear(); //清空容器的所有数据
erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos); //删除pos位置的数据,返回下一个数据的位置。
//示例:
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>& d)
{
// const 常量修饰 防止误操作, 此处必须为——const_iterator
for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// 两端操作插入删除
void test01()
{
deque<int> d;
// push_back(elem); //在容器尾部添加一个数据
// 尾插法
d.push_back(10);
d.push_back(20);
// push_front(elem); //在容器头部插入一个数据
// 头插法
d.push_front(222);
d.push_front(888);
printDeque(d);
// pop_back(); //删除容器最后一个数据
// 尾删
d.pop_back();
// pop_front(); //删除容器第一个数据
// 头删
d.pop_front();
printDeque(d);
}
// 指定位置插入
void test02()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(222);
d.push_front(888);
printDeque(d);
// insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
d.insert(d.begin(), 1000);
printDeque(d);
// insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
d.insert(d.begin(), 2, 80000);
printDeque(d);
deque<int> d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
// insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
d.insert(d.begin(), d2.begin(), d2.end());
printDeque(d);
}
// 删除
void test03()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(222);
d.push_front(888);
printDeque(d);
// erase(pos); //删除pos位置的数据,返回下一个数据的位置。
d.erase(d.begin());
printDeque(d);
// erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
d.erase(d.begin(), d.end());
d.clear();
printDeque(d);
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
/**
总结:
插入和删除提供的位置是迭代器!
尾插 — push_back
尾删 — pop_back
头插 — push_front
头删 — pop_front
结果:
888 222 10 20
222 10
888 222 10 20
1000 888 222 10 20
80000 80000 1000 888 222 10 20
1 2 3 80000 80000 1000 888 222 10 20
888 222 10 20
222 10 20
*/
2.3.6 deque 数据存取
功能描述:
对deque 中的数据的存取操作
函数原型:
at(int idx); //返回索引idx所指的数据
operator[]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
//示例:
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int> & d)
{
for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// 数据存放
void test01()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
for(int i = 0; i < d.size(); i++)
{
// operator[]; //返回索引idx所指的数据
cout << d[i] << " ";
}
cout << endl;
for(int i = 0; i < d.size(); i++)
{
// at(int idx); //返回索引idx所指的数据
cout << d.at(i) << " ";
}
cout << endl;
// front(); //返回容器中第一个数据元素
cout << "front:" << d.front() << endl;
// back(); //返回容器中最后一个数据元素
cout << "back:" << d.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/*
结果:
200 100 10 20
200 100 10 20
front:200
back:20
总结:
除了用迭代器获取deque容器中元素,[ ]和at也可以
front返回容器第一个元素
back返回容器最后一个元素
*/
2.3.7 deque 排序
功能描述:
利用算法实现对deque容器进行排序
算法:
sort(iterator beg, iterator end) //对beg和end区间内元素进行排序
//示例:
#include<iostream>
#include<deque>
using namespace std;
#include<algorithm>
void printDeque(const deque<int> & d)
{
for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
printDeque(d);
// sort(iterator beg, iterator end) //对beg和end区间内元素进行排序
// 默认为从小到大排序
sort(d.begin(), d.end());
printDeque(d);
}
int main()
{
test01();
system("pause");
return 0;
}
/*
结果:
200 100 10 20
10 20 100 200
总结:
sort算法非常实用,但是C++中所有元素必须包含头文件algorithm即可。
*/
/*
3 案例-评委打分
3.1.1 案例描述
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
3.1.2 实现步骤
创建五名选手,放到vector中
遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
sort算法对deque容器中分数排序,去除最高和最低分
deque容器遍历一遍,累加总分
获取平均分
*/
3.1.3 实现代码
#include<iostream>
#include<vector>
#include<ctime>
#include<deque>
#include<algorithm>
using namespace std;
// 创建选手类
class Person
{
public:
Person(string name, int score)
{
this->m_Name = name;
this->m_Score = score;
}
string m_Name; // 姓名
int m_Score; //平均分
};
// 创建存入数据的函数,必须使用引用
void createPerson(vector<Person> & v)
{
// 创建名字种子
string nameSeed = "ABCDE";
for(int i = 0; i < 5; i++)
{
string name = "选手";
name += nameSeed[i];
int score = 0;
// 创建类对象,并把名字放入
Person p (name, score);
// 把类对象 放入到容器中
v.push_back(p);
}
}
// 打分函数
void setScore(vector<Person> &v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
// 将评委的分数 存放到deque容器中 使用deque可以方便的操作头、尾
deque<int> d;
for(int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60 ; // 表示从61开始到100分
d.push_back(score);
}
// 测试代码
cout << "选手: " << it -> m_Name << "打分: " << endl;
for(deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
cout << *dit << " ";
}
cout << endl;
// 排序
sort(d.begin(), d.end());
// 去除最高分,最低分
d.pop_back();
d.pop_front();
// 取平均分
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
sum += *dit; // 累加每个评委打得分数
}
int avg = sum / d.size();
// 将平均分 赋值给选手身上
it -> m_Score = avg;
}
}
// 展示最后分数的函数
void showScore(vector<Person> &v)
{
for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_Name << "平均分:" << it->m_Score << endl;
}
}
int main()
{
// 随机数种子
srand((unsigned int)time(NULL));
// 1、创建5名选手
vector<Person>v; // 创建自定义数据类型的 选手容器
createPerson(v);
// 测试 , 循环打印输出
for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it).m_Name << "分数:" << (*it).m_Score << endl;
}
// 2、 给5名选手打分
setScore(v);
// 3、 显示最后得分
showScore(v);
system("pause");
return 0;
}
/**
结果:
姓名:选手A分数:0
姓名:选手B分数:0
姓名:选手C分数:0
姓名:选手D分数:0
姓名:选手E分数:0
选手: 选手A打分:
73 68 72 75 81 67 69 61 95 78
选手: 选手B打分:
65 63 72 90 99 76 68 77 87 77
选手: 选手C打分:
97 74 85 73 61 65 92 65 68 68
选手: 选手D打分:
84 90 70 71 76 66 100 100 87 82
选手: 选手E打分:
62 91 63 65 91 100 80 99 67 61
姓名:选手A平均分:72
姓名:选手B平均分:76
姓名:选手C平均分:73
姓名:选手D平均分:82
姓名:选手E平均分:77
总结:
选取不同的容器操作数据,可以提升代码的效率。
*/