STL
STL的基本概念以及vector简单用法
STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
1.容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。
2.算法:各种常用的算法,如sort、find、copy、for_each等
3.迭代器:扮演了容器与算法之间的胶合剂。
4.仿函数:行为类似函数,可作为算法的某种策略。
5.适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。
6.空间配置器:负责空间的配置与管理。
容器:置物之所也
STL容器就是将运用最广泛的一些数据结构实现出来常用的数据结构:数组,链表,树,栈,队列,集合,映射表等
这些容器分为序列式容器和关联式溶容器两种:
序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置。
关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
算法:问题之解法也
有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)算法分为:
质变算法和非质变算法。
质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等
非质变算法∶是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等
迭代器:容器和算法之间粘合剂
提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。
每个容器都有自己专属的迭代器,迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针
算法必须要通过迭代器来访问容器中的元素
常用的容器中迭代器种类为双向迭代器和随机访问迭代器
vector容器存放内置数据类型
STL中最常用的容器为Vector,可以理解为数组,利用这个容器中可以实现插入数据、并遍历这个容器
iterator是迭代器的名称,是在vector类型下的
//#define _CRT_SECURE_NO_WARNINGS
//#include <iostream>
//using namespace std;
//int main()
//{
// system("pause");
// return EXIT_SUCCESS;
//}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>//标准算法的头文件
//vector容器存放内置数据类型
void myPrint(int val)
{
cout << val << endl;
}
void test01()
{
//创建了一个vector容器,数组
vector<int>v;
//像容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
通过迭代器访问容器中的数据
//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;
//}
//第三种遍历方式 利用STL提供的遍历算法
for_each(v.begin(), v.end(), myPrint);//这里写一个函数名 利用了回调的技术
//在遍历的期间 调用了函数
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
第三种函数的源码:
这里是用了回调函数还有一个for循环来实现的
这里的迭代器可以看成指针,而解引用就是解这个指针,然后把解引用的元素放入到回调函数进行调用
解引用是解这个迭代器,也就是传入数组中的数
vector容器中存放自定义数据类型
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <string>
//vector存放自定义的数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 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 test02()
{
vector<Person*>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);
//向容器中添加数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器
//此时的it可以理解为二级指针
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it)->m_Name << " 年龄:" << (*it)->m_Age << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
vector容器嵌套vector容器
#define _CRT_SECURE_NO_WARNINGS
#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<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()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
string容器
string容器——构造函数
string本质:
string是C++风格的字符串,而string本质上是一个类string和char区别:
char是一个指针
string是一个类,类内部封装了char,管理这个字符串,是一个char型的容器。
特点:
string类内部封装了很多成员方法
例如︰查找find,拷贝copy,删除delete替换replace,插入insertstring管理char所分配的内存,不用担心复制越界和取值越界等由类内部进行负责*
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
//string的构造函数
void test01()
{
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()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
string容器——赋值操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string str1;
str1 = "hello world";
cout << "str1=" << str1 << endl;
string str2;
str2 = str1;
cout << "str2=" << str1 << 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()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
string容器——字符串拼接
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//string字符凭借
void test01()
{
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 ");//append中文是增加
cout << "str3=" << str3 << endl;
str3.append("game abcde", 4);
cout << "str3=" << str3 << endl;
/*str3.append(str2);
cout << "str3=" << str3 << endl;*/
/*str3.append(str2,0,4);
cout << "str3=" << str3 << endl;*/
str3.append(str2, 4, 4);
cout << "str3=" << str3 << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
string容器——字符串的查找和替换
第一个函数原型:查找的是数组的下标,查找不到就输出-1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//string 字符串的查找和替换
//1、查找 find
void test01()
{
string str1 = "abcdefgde";
int pos = str1.find("de");
cout << "pos=" << pos << endl;
int abc = str1.find("df");
cout << "abc=" << abc << endl;
//1、查找 rfind
pos = str1.rfind("de");
cout << "pos=" << pos << endl;
//rfind和find区别
//rfind从右往左查 find从左往右查
}
//2、替换
void test02()
{
string str1 = "abcdefg";
//从1号位置起3个字符替换为"1111"
str1.replace(1, 3, "1111"); //把bcd换成1111
cout << "str1=" << str1 << endl;
}
int main()
{
test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
string容器——字符串比较
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//string 字符串比较
void test01()
{
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 if (str1.compare(str2) < 0)
{
cout << "str1 小于 str2" << endl;
}
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
string容器——字符存取
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//string 字符存取
//底层是字符数组
void test01()
{
string str = "hello";
//cout << "str=" << str << endl;
//1、通过[]访问单个字符
for (unsigned int i = 0; i < str.size(); i++)
{
cout << str[i] <<" ";
}
cout << endl;
//2、通过at方式访问你单个字符
for (unsigned int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
//修改单个字符
str[0] = 's';
cout << "str=" << str << endl;
str.at(1) = 'b';
cout << "str=" << str << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
string容器——字符串的插入和删除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//string 字符串的插入和删除
void test01()
{
string str = "hello";
//插入
str.insert(1, "111");
cout << "str=" << str << endl;
//删除
str.erase(1, 3);
cout << "str=" << str << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
string容器——字串获取
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//string 字串获取
void test01()
{
string str = "abcdef";
//数组下标为1往后数三个
string subStr = str.substr(1, 3);
cout << "subStr=" << subStr << endl;
}
//实用操作
void test02()
{
string email = "zhangsan@sina.com";
//从邮件的地址中 获取 用户名的信息
int pos = email.find("@");
cout << pos << endl;
string userName = email.substr(0, pos);//从email数组下标为0的元素开始截取8哥字符
cout << userName << endl;
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
这里find找到@的位置为8是数组下标,我们要截取的zhangsan这个字符串长度也是8,直接把pos传进去就行了
总结:灵活的运用求子串功能,可以在实际开发中获取有效的信息
vector容器
vector容器——构造函数
功能:
vector数据结构和数组非常相似,也称为单端数组
vector与普通数组区别:
不同之处在于数组是静态空间,而vector可以动态扩展
动态拓展:
并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
vector的迭代器是支持随机访问的迭代器
这里的begin()是闭区间,end()是开区间
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>&v)
{
//vector容器可以看成数组
//迭代器可以看成指针
//在C++中,vector是一个动态数组,可以根据需要自动扩展或缩小。
//v.begin()是vector容器的一个成员函数,它返回一个指向容器中第一个元素的迭代器。
//所以创建了一个名为v的vector容器,v.begin()将返回一个指向v中第一个元素的迭代器
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器
void test01()
{
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);
//n个elem方式构造
//第一个参数是个数 第二个是赋值
vector<int>v3(10, 100);
printVector(v3);
//拷贝构造
vector<int>v4(v3);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
这里打印函数为什么要加入引用的原因是:
不加引用程序就需要重新构建一个vector,这会浪费很多的计算机资源,加引用就是为了避免这个的
vector容器——赋值操作
#define _CRT_SECURE_NO_WARNINGS
#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 test01()
{
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()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
vector容器——容量和大小
判断是否为空— empty
返回元素个数— size
返回容器容量— capacity
重新指定大小 — resize
#define _CRT_SECURE_NO_WARNINGS
#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 test01()
{
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,100);//利用重载版本,可以指定默认的填充值
printVector(v1);//如果重新指定的比原来长了 默认用0填充
v1.resize(5);
printVector(v1);//如果重新指定的比原来短了 超出部分会删除掉
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
vector容器——插入和删除
#define _CRT_SECURE_NO_WARNINGS
#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 test01()
{
vector<int>v1;
//尾插
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//遍历
printVector(v1);
//尾删
v1.pop_back();
printVector(v1);
//插入
v1.insert(v1.begin(), 100);//传入参数是一个迭代器进行插入
printVector(v1);
v1.insert(v1.begin(), 2, 1000);
printVector(v1);
//删除 参数也是迭代器
v1.erase(v1.begin());
printVector(v1);
//类似于情况的操作
//v1.erase(v1.begin(), v1.end());
v1.clear();
printVector(v1);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
vector容器——数据存取
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
//vector容器 数据存取
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//利用[]方式访问数组中元素
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
//利用at方式访问元素
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << "第一个元素为:" << v1.front() << endl;
//获取最后一个元素
cout << "第一个元素为:" << v1.back() << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
总结:
除了用迭代器获取vector容器中元素,[]和l也可以
front返回容器第一个元素
back返回容器最后一个元素
vector容器——互换容器
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
//vector容器互换
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//1、基本使用
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
cout << "交换前:" << endl;
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);
}
//2、实际用途
//可以巧用swap可以收缩内存空间
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);//重新指定大小
cout << "v的容量: " << v.capacity() << endl;
cout << "v的大小: " << v.size() << endl;
//巧用swap收缩内存
//vector<int>(v)//匿名对象 调用了一次拷贝构造函数
//用v所用的内存空间来初始化x的内存空间
//.swap(v)相当于容器之间的交换
//匿名对象的特点 执行完就会回收空间
vector<int>(v).swap(v);
cout << "v的容量: " << v.capacity() << endl;
cout << "v的大小: " << v.size() << endl;
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
vector容器——预留空间
功能:
减少vector在动态扩展容量时的扩展次数
没有设置预留空间:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
//vector容器 预留空间
void test01()
{
int num = 0;//统计开辟次数
int *p = NULL;
vector<int>v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p!=&v[0])
{
p = &v[0];
num++;
}
}
cout << "num=" << num << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
设置了预留空间:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
//vector容器 预留空间
void test01()
{
int num = 0;//统计开辟次数
int *p = NULL;
vector<int>v;
//利用reserve来预留空间
v.reserve(100000);
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p!=&v[0])
{
p = &v[0];
num++;
}
}
cout << "num=" << num << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
deque容器
deque容器——构造函数
功能:双端数组,可以对头端进行插入删除操作
理解为双端队列
deque与vector区别:
vector对于头部的插入删除效率低,数据量越大,效率越低.
deque相对而言,对头部的插入删除速度回比vector快
vector访问元素时的速度会比deque快,这和两者内部实现有关
1. 内部实现方式不同:vector是使用连续的内存空间存储数据,而deque则是使用一段连续的内存空间存储指向另一段内存空间的指针,这样就可以实现在两端高效地插入和删除元素。
2. 访问元素的方式不同:vector可以使用下标访问元素,而deque则需要使用迭代器访问元素。
3. 内存分配方式不同:vector在内存不足时会重新分配一块更大的内存空间,并将原有数据复制到新的内存空间中,这样会导致内存的浪费;而deque则会在需要时分配新的内存空间,不会浪费内存。
4. 其实,deque可以使用数组下标来访问元素。但是需要注意的是,deque是一个双端队列,支持在队列的两端进行插入和删除操作,因此使用数组下标访问元素时需要考虑队列的头尾位置,以及队列是否为空的情况。另外,使用数组下标访问元素时,需要保证下标的合法性,否则可能会导致访问越界的错误。因此,建议在使用deque时,优先使用其提供的成员函数来进行元素的访问和操作。
deque内部工作原理:
deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据
中控器维护的是每个缓冲区的地址,使得使用deque时象一片连续的内存空间
deque容器的迭代器也是支持随机访问的
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>&d)
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
//加上const防止修改代码
//容器中的数据不可以修改了
//*it = 100; err
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
deque<int>d2(d1.begin(), d1.end());
printDeque(d2);
deque<int>d3(10, 100);
printDeque(d3);
deque<int>d4(d3);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
deque容器——赋值操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque容器赋值操作
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
//operator=赋值
deque<int>d2;
d2 = d1;
printDeque(d2);
//assign 赋值
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
deque<int>d4;
d4.assign(10, 100);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
deque容器——大小操作
deque相较于vector少了容量操作,也就是说容量没有限制
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
using namespace std;
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);
if (d1.empty())
{
cout << "d1为空" << endl;
}
else
{
cout << "d1不为空" << endl;
cout << "d1的大小为:" << d1.size() << endl;
//deque容器没有容量概念
}
//重新指定大小
//d1.resize(15);
d1.resize(15, 1);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
deque容器——插入和删除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
using namespace std;
//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;
//尾插
d1.push_back(10);
d1.push_back(20);
//头插
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//尾删
d1.pop_back();
printDeque(d1);
//头删
d1.pop_front();
printDeque(d1);
}
void test02()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//insert插入
d1.insert(d1.begin(), 1000);
printDeque(d1);
d1.insert(d1.begin(), 2, 10000);
printDeque(d1);
//按照区间进行插入
deque<int>d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
//插入d2.begin()到d2.end()的区间的数
d1.insert(d1.begin(), d2.begin(), d2.end());
printDeque(d1);
}
void test03()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
//删除
deque<int>::iterator it = d1.begin();
it++;
d1.erase(it);
printDeque(d1);
//d1.erase(d1.begin() + 1);
//printDeque(d1);
//按照区间的方式删除
d1.erase(d1.begin(), d1.end());
printDeque(d1);
//清空
d1.clear();
printDeque(d1);
}
int main()
{
test01();
cout << "------------------------------" << endl;
test02();
cout << "------------------------------" << endl;
test03();
system("pause");
return EXIT_SUCCESS;
}
deque容器——数据存储
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
using namespace std;
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
//利用[]方式访问数组中元素
for (int i = 0; i < d.size(); i++)
{
cout << d[i] << " ";
}
cout << endl;
//利用at方式访问元素
for (int i = 0; i < d.size(); i++)
{
cout << d.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << "第一个元素为:" << d.front() << endl;
//获取最后一个元素
cout << "第一个元素为:" << d.back() << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
deque容器——排序操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printDeque(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque容器 排序操作
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
// 300 200 100 10 20 30
printDeque(d);
//排序
//sort算法默认的排序规则 从小到大 升序
//对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其进行排序
//vector容器佝可以利用sort讲行排序
sort(d.begin(),d.end());
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
STL案例
评委打分
有5名选手:选手ABCDE,10个评委分别对每1名选手打分,去除最高分,去除评委中最低分,取平均分。
1.创建五名选手,放到vector中
2.遍历vector容器,取出来每一个选手,执行for循环,可以把10个评委打分存到deque容器中
3.sort算法对deque容器中分数排序,去除最高和最低分
4.deque容器遍历一遍,累加总分
5.获取平均分
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <algorithm>
#include <ctime>
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)
{
for (int i = 0; i < 5; i++)
{
string nameSeed = "ABCDE";
string name = "选手";
name += nameSeed[i];
int score = 0;
Person p(name, score);
//将创建的person对象 放入到容器中
v.push_back(p);
}
}
void setScore(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
//将评委的分数 放入到deque容器中
deque<int>d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;//60 ~ 100
d.push_back(score);
}
//cout << "选手: " << it->m_Name << " 打分: " << endl;
//for (deque<int>::iterator dit1 = d.begin(); dit1 != d.end(); dit1++)
//{
// cout << *dit1 << " ";
//}
//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 EXIT_SUCCESS;
}
stack容器
stack容器——基本概念
就是栈
栈不允许有遍历行为
是先进后出的数据结构
stack容器——常用接口
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
using namespace std;
//栈stack容器
void test01()
{
//特点:符合先进后出数据结构
stack<int>s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << "栈的大小:" << s.size() << endl;
//只有栈不为空,查看栈顶,并且执行出栈操作
while (!s.empty())
{
//查看栈顶元素
cout << "栈顶元素为:" << s.top() << endl;
//出栈
s.pop();
}
cout << "栈的大小:" << s.size() << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
queue容器
queue容器——基本概念
是先进先出的数据结构
**队列容器允许从一端新增元素,从另一端移除元素
队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
队列中进数据称为—入队push
队列中出数据称为—出队pop
**
queue容器——常用接口
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//队列 Queue
void test01()
{
//创建队列
queue<Person>q;
//准备数据
Person p1("唐僧", 30);
Person p2("孙悟空", 30);
Person p3("猪八戒", 30);
Person p4("沙僧", 30);
//入队
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout << "队列的大小为:" << q.size() << endl;
//判断只要队列不为空,查看对头,查看队尾,出队
while (!q.empty())
{
//查看对头元素
cout << "对头元素 --- 姓名:" << q.front().m_Name << " 对头元素 --- 年龄:" << q.front().m_Age << endl;
//查看对尾元素
cout << "对尾元素 --- 姓名:" << q.back().m_Name << " 对尾元素 --- 年龄:" << q.back().m_Age << endl;
q.pop();
}
cout << "队列的大小为:" << q.size() << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器
list容器基本概念
在STL中这个链表是双向链表
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点∶
采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list的缺点:
链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。
list和vector是经常被使用的容器
list容器——构造函数
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//加上const防止修改代码
//容器中的数据不可以修改了
//*it = 100; err
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//创建list容器
list<int>L1;//默认构造
//添加数据
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//遍历容器
printList(L1);
//区间方式构造
list<int>L2(L1.begin(), L1.end());
printList(L2);
//拷贝构造
list<int>L3(10, 1000);
printList(L3);
//n个elem
list<int>L4(L3);
printList(L4);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——赋值和交换
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器赋值和交换
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
list<int>L2;
L2 = L1;
printList(L2);
list<int>L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int>L4;
L4.assign(10, 100);
printList(L4);
}
//交换
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2;
L2.assign(10, 100);
cout << "交换前: " << endl;
printList(L1);
printList(L2);
L1.swap(L2);
cout << "交换后:" << endl;
printList(L1);
printList(L2);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
List容器——大小操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器赋值和交换
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
//判断容器是否为空
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空" << endl;
cout << "L1的元素个数为:" << L1.size() << endl;
}
//重新指定大小
L1.resize(10,10000);
printList(L1);
L1.resize(2);
printList(L1);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——插入和删除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器赋值和交换
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L;
//尾插
L.push_back(10);
L.push_back(20);
L.push_back(30);
//头插
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);
//尾删
L.pop_back();
printList(L);
//头删
L.pop_front();
printList(L);
//insert插入
list<int>::iterator it = L.begin();
L.insert(++it, 1000);
printList(L);
//list容器是双向迭代器 不能跳跃移动
//L.insert(L.begin() + 1, 100000);//err
//printList(L);
//删除
it = L.begin();
L.erase(++it);
printList(L);
//移除
L.push_back(10000);
L.push_back(10000);
L.push_back(10000);
L.push_back(10000);
printList(L);
L.remove(10000);//按值删除 删除所有的10000
printList(L);
//清空
L.clear();
printList(L);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——数据存取
list容器底层是链表 存储的不是连续的空间 不支持随机访问 不能用[]或者at直接进行访问
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器赋值和交换
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//L1[0]不可以用 []访问list容器中的元素
//L1[0];err
//L1.at(0);err
//原因是list本质是链表,不是用连续的线性空间存储的,迭代器也是不支持随机访问的
cout << "第一个元素为: " << L1.front() << endl;
cout << "最后一个元素为:" << L1.back() << endl;
//验证迭代器是不支持随机访问的
list<int>::iterator it = L1.begin();
it++;//right 支持双向++ --
it--;//right
//it = it + 1;//err
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——反转和排序
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
//list容器赋值和交换
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(20);
L1.push_back(10);
L1.push_back(50);
L1.push_back(40);
L1.push_back(30);
cout << "反转前:" << endl;
printList(L1);
//反转
L1.reverse();
cout << "反转后:" << endl;
printList(L1);
}
//如果返回为true,则让两个参数进行交换,如果返回false则不动
bool myCompare(int v1, int v2)
{
//降序 就让第一个数 > 第二个数
return v1 > v2;
}
//排序
void test02()
{
list<int>L1;
L1.push_back(20);
L1.push_back(10);
L1.push_back(50);
L1.push_back(40);
L1.push_back(30);
//排序
cout << "排序前: " << endl;
printList(L1);
//所有不支持随机访问迭代器的容器,不可以用标准的算法
//不支持随机访问迭代器的容器,内部会提供对应的一些算法
//sort(L1.begin(),L1.end());//err
L1.sort();//默认排序规则 从小到大 升序
cout << "排序后: " << endl;
printList(L1);
L1.sort(myCompare);
printList(L1);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
list容器——排序案例
案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高排序
规则:按照年龄进行升序,如果年龄相同按照身高进行降序
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Person
{
public:
Person(string name,int age,int height)
{
this->m_Name = name;
this->m_Age = age;
this->m_Height = height;
}
string m_Name;
int m_Age;
int m_Height;
};
//指定排序规则
bool comparePerson(Person &p1,Person &p2)
{
if (p1.m_Age == p2.m_Age)
{
//年龄相同 按照身高降序
return p1.m_Height > p2.m_Height;
}
else
{
//按照年龄 升序
return p1.m_Age < p2.m_Age;
}
}
void test01()
{
list<Person>L1;
Person p1("刘备", 35, 175);
Person p2("曹操", 45, 180);
Person p3("孙权", 40, 170);
Person p4("赵云", 25, 190);
Person p5("关羽", 35, 160);
Person p6("张飞", 35, 200);
L1.push_back(p1);
L1.push_back(p2);
L1.push_back(p3);
L1.push_back(p4);
L1.push_back(p5);
L1.push_back(p6);
for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
}
//排序
cout << "-----------------------------" << endl;
cout << "排序后:" << endl;
L1.sort(comparePerson);
for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
}
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
set容器/multiset容器
set容器——构造和赋值
set容器时关联式容器
所有的元素在插入时都会被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
set不允许容器中有重复的元素. multiset允许容器中有重复的元素
在二叉搜索树中,通常不允许插入相同的数,因为二叉搜索树是一种有序的数据结构,相同的数会破坏它的有序性。如果插入相同的数,可能会导致搜索、删除等操作出现问题。但是在其他类型的二叉树中,比如普通的二叉树,是允许插入相同的数的。
C++ STL中的set容器是一种关联式容器,它的元素按照一定的排序规则自动排序。set容器的insert函数会将元素插入到容器中,并保持容器中元素的有序性。set容器的默认排序顺序是按照元素的升序进行排序,也就是从小到大的顺序
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <set>
//set容器构造和赋值
void printSet(set<int>&s1)
{
for (set<int>::const_iterator it =s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(30);
//遍历容器
//set容器的特点:所有的元素插入的时候会自动被排序
//set容器不允许插入重复的值
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s2;
printSet(s3);;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
set容器——大小和交换
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <set>
//set容器构造和赋值
void printSet(set<int>&s1)
{
for (set<int>::const_iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(30);
printSet(s1);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
cout << "s1的大小为:" << s1.size() << endl;
}
set<int>s2;
//插入数据 只有insert方式
s2.insert(66);
s2.insert(11);
s2.insert(33);
s2.insert(22);
cout << "交换前: " << endl;
printSet(s1);
printSet(s2);
cout << "交换后: " << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
set容器——插入和删除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <set>
//set容器插入和删除
void printSet(set<int>&s1)
{
for (set<int>::const_iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//插入
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
//遍历
printSet(s1);
//删除
s1.erase(s1.begin());
printSet(s1);//删除的是10
//删除重载版本
s1.erase(30);
printSet(s1);
//清空
//s1.erase(s1.begin(), s1.end());
s1.clear();
printSet(s1);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
set容器的查找和统计
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <set>
//set容器 查找和统计
void printSet(set<int>&s1)
{
for (set<int>::const_iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int>::iterator pos = s1.find(30);
if (pos!=s1.end())
{
cout << "找到元素: " << *pos << endl;
}
else
{
cout << "未找到元素" << endl;
}
printSet(s1);
}
void test02()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(30);
//统计30的个数
int num = s1.count(30);
//对于set而言 统计结果要么是1要么是0
cout << "num= " << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
总结:
查找— find(返回的是迭代器)
统计— count(对于set,结果为0或者1)
set和multiset的区别
区别:
set不可以插入重复数据,而multiset可以
set插入数据的同时会返回插入结果,表示插入是否成功
multiset不会检测数据,因此可以插入重复数据
set和multiset都是STL中的关联容器,它们的主要区别在于元素的唯一性和插入操作的方式。
set中的元素是唯一的,即每个元素只能出现一次。当我们向set中插入一个已经存在的元素时,set会忽略这个插入操作。set中的元素按照一定的排序规则进行排序,这个排序规则可以通过自定义比较函数来指定。
multiset中的元素可以重复,即每个元素可以出现多次。当我们向multiset中插入一个已经存在的元素时,multiset会将这个元素插入到容器中。multiset中的元素同样按照一定的排序规则进行排序,这个排序规则也可以通过自定义比较函数来指定。
另外,set和multiset的插入操作方式也不同。set中的插入操作使用insert函数,而multiset中的插入操作使用insert或者emplace函数。insert函数会将元素插入到容器中,而emplace函数会在容器中直接构造元素。
综上所述,set适用于需要保证元素唯一性的场景,而multiset适用于需要允许元素重复的场景。
set的相关操作源码:
insert源码
insert源码的返回值:
multiset的相关操作源码
insert源码:
insert源码的返回值:是一个迭代器
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
using namespace std;
void test01()
{
set<int>s;
pair<set<int>::iterator, bool>ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
multiset<int>ms;
//允许插入重复值
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
multiset的insert函数会返回一个迭代器,指向插入的元素。如果插入成功,这个迭代器就是指向新插入的元素;如果插入失败,这个迭代器就是指向已经存在的元素。
因为multiset允许存储重复的元素,所以在插入元素时不需要检测是否已经存在相同的元素。如果已经存在相同的元素,插入操作会将新元素插入到已有元素的后面,因此不会影响multiset中已有元素的顺序。
pair使用——pair队组的创建
pair对组的作用:
pair对组通常用于将两个值组合在一起,以便更方便地进行处理。pair对组可以用于实现各种数据结构,例如哈希表、二叉搜索树等。在C++中,pair对组是一个标准库类型,可以用于将两个值组合在一起,并提供了一些方便的方法来访问这些值。例如,可以使用pair对组来表示一个坐标点,其中第一个值表示x坐标,第二个值表示y坐标。这样,可以将多个坐标点存储在一个vector或者其他容器中,并方便地进行访问和处理。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//pair对组的创建
void test01()
{
//第一种方式
pair<string, int>p("Tom", 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
//第二种方式
pair<string, int>p2 = make_pair("Jerry", 30);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
set容器——排序
set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:
用仿函数,可以改变排序规则
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <set>
//set容器排序
class MyCompare
{
public:
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void test01()
{
//模板参数列表放的是数据类型
//仿函数本质上是类型
set<int,MyCompare>s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
for (set<int>::const_iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定排序规则 从大到小
set<int>s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(50);
s2.insert(30);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
set容器——自定义数据类型
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <set>
#include <string>
//set容器排序
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class comparePerson
{
public:
bool operator()(const Person&p1, const Person&p2)
{
//按照年龄 降序
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
//自定义数据类型 都会指定排序规则
set<Person,comparePerson>s;
Person p1("刘备", 24);
Person p2("关羽", 28);
Person p3("张飞", 25);
Person p4("赵云", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person,comparePerson>::iterator it =s.begin(); it != s.end(); it++)
{
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
map容器
map容器——基本概念
简介:
map中所有元素都是pair
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
所有元素都会根据元素的键值自动排序
本质:
map/multimap属于关联式容器,底层结构是用二叉树实现。
优点:
可以根据key值快速找到value值
map和multimap区别:
map不允许容器中有重复key值元素. multimap允许容器中有重复key值元素
在C++ STL中,map容器的底层实现通常是红黑树。红黑树是一种自平衡的二叉搜索树,它能够保证在最坏情况下,插入、删除、查找操作的时间复杂度都是O(log n)。由于map容器需要支持快速的查找、插入、删除等操作,因此使用红黑树作为底层实现可以保证其高效性能。但是,具体实现可能因不同的编译器和操作系统而有所不同。
map容器——构造和赋值
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <map>
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key= " << (*it).first << " value= " << it->second << endl;
}
cout << endl;
}
void test01()
{
//模板写两个参数类型
//第一个参数是key值起到了所有的作用
//第二个参数value值起到了实值的作用
//创建map容器
map<int, int>m;
//匿名的队组
//默认的排序是按照key排序的
//map容器插入重复的key插不进去
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 20));
m.insert(pair<int, int>(2, 30));
m.insert(pair<int, int>(4, 40));
printMap(m);
//拷贝构造
map<int, int>m2(m);
printMap(m2);
//赋值
map<int, int>m3;
m3 = m2;
printMap(m3);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
总结: map中所有元素都是成对出现,插入数据时候要使用对组
map容器——大小和交换
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <map>
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key= " << (*it).first << " value= " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大下" << m.size() << endl;
}
}
void test02()
{
map<int, int>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 20));
m1.insert(pair<int, int>(3, 30));
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前: " << endl;
printMap(m1);
printMap(m2);
m1.swap(m2);
cout << "交换后: " << endl;
printMap(m1);
printMap(m2);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
map容器——插入和删除
map容器插入 m[4] = 40的缺点主要有以下几点:
1.可能会覆盖原有的键值对:如果map中已经存在键为4的键值对,那么插入m[4] = 40会覆盖原有的值,导致数据丢失。
2.可能会导致迭代器失效:插入新的键值对可能会导致原有的迭代器失效,因为插入操作可能会导致内存重新分配,从而使原有的迭代器指向的内存地址失效。
3.可能会导致性能下降:插入新的键值对可能会导致map内部的红黑树结构发生变化,从而导致性能下降。特别是如果插入的键值对数量较多,可能会导致树的高度增加,从而使查找、插入、删除等操作的时间复杂度增加。
因此,在插入新的键值对时,应该先判断map中是否已经存在相同的键,如果存在,则可以选择更新原有的值,或者放弃插入操作;如果不存在,则可以使用insert()函数插入新的键值对,这样可以避免上述问题。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
using namespace std;
//map容器 插入和删除
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key= " << (*it).first << " value= " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
//插入
//第一种
m.insert(pair<int, int>(1, 10));
//第二种
m.insert(make_pair(2, 20));
//第三种
//map容器下面的值类型
m.insert(map<int, int>::value_type(3, 30));
//第四种
m[4] = 40;
printMap(m);
//第四种插入的缺点:
//[]不建议插入,用途:可以利用key访问到value
//cout << m[5] << endl;
//删除
m.erase(m.begin());
printMap(m);
//重载版本的删除
m.erase(3);//按照key删除 不是按照value来删除的
printMap(m);
//按照区间来删除
m.erase(m.begin(),m.end());
printMap(m);
//清空
m.clear();
printMap(m);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
map容器——查找和统计
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <map>
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key= " << (*it).first << " value= " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大下" << m.size() << endl;
}
}
void test02()
{
map<int, int>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 20));
m1.insert(pair<int, int>(3, 30));
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前: " << endl;
printMap(m1);
printMap(m2);
m1.swap(m2);
cout << "交换后: " << endl;
printMap(m1);
printMap(m2);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
总结:
查找— find(返回的是迭代器)
统计— count(对于map,结果为0或者1)
map容器——排序
map容器默认排序规则为按照key值进行从小到大排序
接下来利用仿函数,可以改变排序规则
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
using namespace std;
class MyCompare
{
public:
bool operator()(int v1,int v2)const
{
//降序
return v1 > v2;
}
};
//map容器 排序
void test01()
{
//统计
map<int, int,MyCompare>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
m.insert(pair<int, int>(5, 50));
for (map<int, int,MyCompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key= " << (*it).first << " value= " << it->second << endl;
}
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
STL——案例
员工分组
公司今天招聘了10个员工(ABCDEFGHI),10名员工进入公司之后,需要指派员工在那个部门工作
员工信息有:姓名工资组成;部门分为:策划、美术、研发
随机给10名员工分配部门和工资
通过multimap进行信息的插入key(部门编号) value(员工)
分部门显示员工信息
1.创建10名员工,放到vector中
2.遍历vector容器,取出每个员工,进行随机分组
3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
4.分部门显示员工信息
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <ctime>
using namespace std;
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker
{
public:
string m_Name;
int m_Salary;
};
void createWorker(vector<Worker>&v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i];
worker.m_Salary = rand() % 10001 + 10000;
//将员工放入到容器中
v.push_back(worker);
}
}
//员工分组
void setGroup(vector<Worker>&v, multimap<int, Worker>&m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptId = rand() % 3;//0 1 2
//将员工插入到分组中
//key部门编号,value具体员工
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGroup(multimap<int,Worker>&m)
{
//0 A B C 1 D E 2 F G
cout << "策划部门: " << endl;
multimap<int, Worker>::iterator pos = m.find(CEHUA);
int count = m.count(CEHUA);//统计策划部门具体人数
int index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名: " << pos->second.m_Name << " 工资 " << pos->second.m_Salary << endl;
}
cout << "---------------------" << endl;
cout << "美术部门: " << endl;
pos = m.find(MEISHU);
count = m.count(MEISHU);//统计策划部门具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名: " << pos->second.m_Name << " 工资 " << pos->second.m_Salary << endl;
}
cout << "---------------------" << endl;
cout << "研发部门: " << endl;
pos = m.find(YANFA);
count = m.count(YANFA);//统计策划部门具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名: " << pos->second.m_Name << " 工资 " << pos->second.m_Salary << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
//1、创建员工
vector<Worker>vWorker;
createWorker(vWorker);
//2、员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
测试
//for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
//{
// cout << "姓名: " << it->m_Name << " 工资: " << it->m_Salary << endl;
//}
//3、分组显示员工
showWorkerByGroup(mWorker);
system("pause");
return EXIT_SUCCESS;
}