1. stack栈容器
1.1先进后出
1.2栈顶 top
1.3压栈 push
1.4弹出栈顶 pop
1.5大小 size
1.6为空 empty
02 stack栈容器.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <stack>
using namespace std;
/*
stack构造函数
stack<T> stkT;//stack采用模板类实现, stack对象的默认构造形式:
stack(const stack &stk);//拷贝构造函数
3.4.3.2 stack赋值操作
stack& operator=(const stack &stk);//重载等号操作符
3.4.3.3 stack数据存取操作
push(elem);//向栈顶添加元素
pop();//从栈顶移除第一个元素
top();//返回栈顶元素
3.4.3.4 stack大小操作
empty();//判断堆栈是否为空
size();//返回堆栈的大小
*/
void test01()
{
stack<int>s;
//放入数据 push
s.push(10);
s.push(30);
s.push(20);
s.push(40);
while (s.size() != 0)
{
cout << "栈顶为 " << s.top() << endl; //40 20 30 10
//弹出栈顶元素
s.pop();
}
cout << "size = " << s.size() << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
2. queue 队列容器
2.1先进先出
2.2队头 front 队尾 back
2.3入队 push
2.4弹出队头 pop
2.5大小 size
2.6为空 empty
03 queue容器.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <queue>
/*
queue构造函数
queue<T> queT;//queue采用模板类实现,queue对象的默认构造形式:
queue(const queue &que);//拷贝构造函数
3.5.3.2 queue存取、插入和删除操作
push(elem);//往队尾添加元素
pop();//从队头移除第一个元素
back();//返回最后一个元素
front();//返回第一个元素
3.5.3.3 queue赋值操作
queue& operator=(const queue &que);//重载等号操作符
3.5.3.4 queue大小操作
empty();//判断队列是否为空
size();//返回队列的大小
*/
void test01()
{
queue<int>q;
q.push(10);//往队尾添加元素
q.push(20);
q.push(30);
q.push(40);
while (!q.empty())
{
// 10 40 20 40 30 40 40 40
cout << "队头: " << q.front() << endl;
cout << "队尾: " << q.back() << endl;
//弹出队头元素
q.pop();
}
cout << "size : " << q.size() << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
3. List容器
3.1赋值、构造、大小、为空、删除 、添加
3.2移除 remove( 10 ) 删除容器中所有与10 匹配的元素
3.3双向循环链表
3.4迭代器是不支持随机访问的
3.5反转排序
3.5.1reverse 反转
3.5.2排序 成员函数 sort
3.5.3默认排序 从小到大
3.5.4自定义数据类型,必须指定排序规则
3.5.5高级
3.6remove删除list容器中自定义数据类型
04 list容器.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
//list是双向循环链表
void test01()
{
list<int> myList;
for (int i = 0; i < 10; i++){
myList.push_back(i);
}
list<int>::_Nodeptr node = myList._Myhead->_Next;
for ( int i = 0; i < myList._Mysize * 2; i++){
cout << "Node:" << node->_Myval << endl;
node = node->_Next;
if (node == myList._Myhead){
node = node->_Next;
}
}
}
//list常用API
/*
list构造函数
list<T> lstT;//list采用采用模板类实现,对象的默认构造形式:
list(beg,end);//构造函数将[beg, end)区间中的元素拷贝给本身。
list(n,elem);//构造函数将n个elem拷贝给本身。
list(const list &lst);//拷贝构造函数。
3.6.4.2 list数据元素插入和删除操作
push_back(elem);//在容器尾部加入一个元素
pop_back();//删除容器中最后一个元素
push_front(elem);//在容器开头插入一个元素
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位置的数据,返回下一个数据的位置。
remove(elem);//删除容器中所有与elem值匹配的元素。
*/
void printList(list<int>&L)
{
for (list<int>::iterator it = L.begin(); it != L.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test02()
{
list<int>L(10,10);
list<int>L2(L.begin(), L.end());
printList(L);
printList(L2);
L2.push_back(100);
//逆序打印
for (list<int>::reverse_iterator it = L2.rbegin(); it != L2.rend();it++)
{
cout << *it << " ";
}
cout << endl;
//list迭代器不支持随机访问
list<int>::iterator itBegin = L2.begin();
//itBegin = itBegin + 1;
//插入数据
list<int>L3;
L3.push_back(10);
L3.push_back(30);
L3.push_back(20);
L3.push_front(100);
L3.push_front(300);
L3.push_front(200);
printList(L3); // 200 300 100 10 30 20
//删除两端数据
L3.pop_front(); //头删
L3.pop_back(); //尾删
printList(L3); // 300 100 10 30
L3.insert(L3.begin(), 1000);
printList(L3); // 1000 300 100 10 30
//remove(elem);//删除容器中所有与elem值匹配的元素。
L3.push_back(10);// 1000 300 100 10 30 10
L3.remove(10); //参数 直接放值
printList(L3);//1000 300 100 30
}
/*
list大小操作
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(num);//重新指定容器的长度为num,
若容器变长,则以默认值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);//重新指定容器的长度为num,
若容器变长,则以elem值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
3.6.4.4 list赋值操作
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
list& operator=(const list &lst);//重载等号操作符
swap(lst);//将lst与本身的元素互换。
3.6.4.5 list数据的存取
front();//返回第一个元素。
back();//返回最后一个元素。
*/
void test03()
{
list<int>L3;
L3.push_back(10);
L3.push_back(30);
L3.push_back(20);
L3.push_front(100);
L3.push_front(300);
L3.push_front(200);
cout << "大小:" << L3.size() << endl;
if (L3.empty())
{
cout << "L3为空" << endl;
}
else
{
cout << "L3不为空" << endl;
}
L3.resize(10);
printList(L3);
L3.resize(3);
printList(L3);
list<int> L4;
L4.assign(L3.begin(), L3.end());
//200 300 100
cout << "front: " << L4.front() << endl;
cout << "back: " << L4.back() << endl;
}
/*
list反转排序
reverse();//反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
sort(); //list排序
*/
bool myCompare(int v1, int v2)
{
return v1 > v2; //降序
}
void test04()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(40);
L.push_back(30);
L.reverse();
printList(L); // 30 40 20 10
//所有不支持随机访问的迭代器 不可以用系统提供的算法
// 如果不支持用系统提供算法,那么这个类内部会提供
//sort(L.begin(), L.end());
L.sort(); //从小到大
printList(L);
//从大到小
L.sort(myCompare);
printList(L);
}
//自定义数据类型
class Person
{
public:
Person(string name, int age,int height)
{
this->m_Name = name;
this->m_Age = age;
this->m_Height = height;
}
//重载 == 让remove 可以删除自定义的person类型
bool operator==( const Person & p)
{
if ( this->m_Name == p.m_Name && this->m_Age == p.m_Age && this->m_Height == p.m_Height)
{
return true;
}
return false;
}
string m_Name;
int m_Age;
int m_Height; //身高
};
//Person排序规则 如果年龄 相同 按照身高的升序排序
bool myComparePerson( Person & p1,Person & p2 )
{
//if (p1.m_Age > p2.m_Age)
//{
// return true;
//}
//return false;
if (p1.m_Age == p2.m_Age)
{
return p1.m_Height < p2.m_Height;
}
else
{
return p1.m_Age > p2.m_Age;
}
}
void test05()
{
list<Person> L;
Person p1("亚瑟", 10 , 165);
Person p2("德玛西亚", 20 , 170);
Person p3("火枪", 17,177);
Person p4("德雷福斯", 19, 120);
Person p5("MT", 18,200);
Person p6("狗蛋", 18, 166);
Person p7("狗剩", 18, 210);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
L.push_back(p7);
//需求 打印数据时候 按照年龄的降序 输出
//对于自定义数据类型 ,必须要指定排序规则
L.sort( myComparePerson);
for (list<Person>::iterator it = L.begin(); it != L.end();it++)
{
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << " 身高:"<< it->m_Height<< endl;
}
//删除 狗蛋
cout << " -------------------- " << endl;
L.remove(p6);
for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << " 身高:" << it->m_Height << endl;
}
}
int main(){
//test01();
//test02();
//test03();
//test04();
test05();
system("pause");
return EXIT_SUCCESS;
}
4. set容器
4.1关联式容器
4.2插入数据自动排序 按照key
4.3insert 插入值
4.4erase 参数可以传值 或者 迭代器
4.5find() 返回值 迭代器 找不到返回的 end()
4.6count 计数 对于set而言 结果 就是 0 或者1
4.7lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
4.8upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
4.9equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
4.10对组 pair
4.10.1 第一个值 first
4.10.2 第二个值 second
4.10.3 默认括号
4.10.4 make_pair()
4.11set插入返回值是 对组 < 迭代器, 是否成功标示>
4.12指定set排序规则,利用仿函数
4.13set插入自定义数据类型
05 set容器.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//set multiset头文件
#include <set>
#include <string>
/*
set构造函数
set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数:
set(const set &st);//拷贝构造函数
3.7.2.2 set赋值操作
set& operator=(const set &st);//重载等号操作符
swap(st);//交换两个集合容器
3.7.2.3 set大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
3.7.2.4 set插入和删除操作
insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的元素。
*/
void printSet( set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//关联式容器 key进行排序,从小到大
s1.insert(5);
s1.insert(1);
s1.insert(9);
s1.insert(3);
s1.insert(7);
printSet(s1);
if (s1.empty())
{
cout << "空" << endl;
}
else
{
cout << "size = " << s1.size() << endl;
}
s1.erase(s1.begin()); // 3 5 7 9
printSet(s1);
s1.erase(3); // 5 7 9
printSet(s1);
}
/*
set查找操作
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/
void test02()
{
set<int>s1;
s1.insert(5);
s1.insert(1);
s1.insert(9);
s1.insert(3);
s1.insert(7);
//对于set 没有value key就是value
set<int>::iterator pos = s1.find(2);
//判断是否找到
if (pos != s1.end())
{
cout << "找到了:值为:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
//count(key);//查找键key的元素个数 set而言 结果 0或者1
int num = s1.count(2);
cout << "2的个数为: " << num << endl;
//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
set<int>::iterator it = s1.lower_bound(3); // 10就是未找到
if (it!= s1.end())
{
cout << "找到了 lower_bound (3)的值为:" << *it << endl;
}
else
{
cout << "未找到" << endl;
}
// upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
set<int>::iterator it2 = s1.upper_bound(3);
if (it2 != s1.end())
{
cout << "找到了 upper_bound (3)的值为:" << *it2 << endl;
}
else
{
cout << "未找到" << endl;
}
//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
//上下限 就是lower_bound upper_bound
pair<set<int>::iterator, set<int>::iterator> ret = s1.equal_range(3);
//获取第一个值
if (ret.first != s1.end())
{
cout << "找到equal_range中 lower_bound 的值 :" << *(ret.first) << endl;
}
else
{
cout << "未找到" << endl;
}
//获取第二个值
if (ret.second != s1.end())
{
cout << "找到equal_range中 upper_bound 的值 :" << *(ret.second) << endl;
}
else
{
cout << "未找到" << endl;
}
}
//set容器 不允许插入重复的键值
void test03()
{
set<int> s1;
pair<set<int>::iterator,bool> ret = s1.insert(10);
if (ret.second)
{
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
ret = s1.insert(10);
if (ret.second)
{
cout << "第二次插入成功" << endl;
}
else
{
cout << "第二次插入失败" << endl;
}
printSet(s1);
//multiset允许插入重复值
multiset<int> mul;
mul.insert(10);
mul.insert(10);
}
//指定set排序规则 从大到小
//仿函数
class myCompare
{
public:
//重载 ()
bool operator()( int v1 ,int v2)
{
return v1 > v2;
}
};
//set容器排序
void test04()
{
set<int,myCompare>s1;
s1.insert(5);
s1.insert(1);
s1.insert(9);
s1.insert(3);
s1.insert(7);
//printSet(s1);
//从大到小排序
//在插入之前就指定排序规则
for (set<int, myCompare>::iterator it = s1.begin(); it != s1.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
//自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class myComparePerson
{
public:
bool operator()( const Person & p1, const Person & p2)
{
if (p1.m_Age > p2.m_Age) //降序
{
return true;
}
return false;
}
};
void test05()
{
set<Person, myComparePerson> s1;
Person p1("大娃", 100);
Person p2("二娃", 90);
Person p3("六娃", 10);
Person p4("爷爷", 1000);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
//插入自定义数据类型,上来就指定好排序规则
//显示
for (set<Person, myComparePerson>::iterator it = s1.begin(); it != s1.end();it++)
{
cout << "姓名:" << (*it).m_Name << " 年龄: " << it->m_Age << endl;
}
}
int main(){
//test01();
//test02();
//test03();
//test04();
test05();
system("pause");
return EXIT_SUCCESS;
}
06 pair对组.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
//创建对组
void test01()
{
//第一种
pair<string, int> p(string("Tom"), 100);
//取值
cout << "姓名:" << p.first << endl;
cout << "年龄: " << p.second << endl;
//第二种创建
pair<string, int> p2 = make_pair("Jerry", 200);
cout << "姓名:" << p2.first << endl;
cout << "年龄: " << p2.second << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
5. map容器
5.1每个元素 都是一个pair
5.2对于map而言 key是不可以重复
5.3multimap可以
5.44中插入方式
5.5count 统计 map 0 或1 multimap可能大于1
5.6排序规则自己指定
07 map容器.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <map>
using namespace std;
/*
map构造函数
map<T1, T2> mapTT;//map默认构造函数:
map(const map &mp);//拷贝构造函数
3.8.2.2 map赋值操作
map& operator=(const map &mp);//重载等号操作符
swap(mp);//交换两个集合容器
3.8.2.3 map大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
3.8.2.4 map插入数据元素操作
map.insert(...); //往容器插入元素,返回pair<iterator,bool>
map<int, string> mapStu;
// 第一种 通过pair的方式插入对象
mapStu.insert(pair<int, string>(3, "小张"));
// 第二种 通过pair的方式插入对象
mapStu.inset(make_pair(-1, "校长"));
// 第三种 通过value_type的方式插入对象
mapStu.insert(map<int, string>::value_type(1, "小李"));
// 第四种 通过数组的方式插入值
mapStu[3] = "小刘";
mapStu[5] = "小王";
*/
void test01()
{
map<int, int> m;
//插入值
// 4种方式
//第一种
m.insert(pair<int, int>(1, 10));
//第二种 推荐
m.insert(make_pair(2, 20));
//第三种 不推荐
m.insert(map<int, int>::value_type(3, 30));
//第四种 如果保证key存在 ,那么可以通过[]访问
m[4] = 40;
for (map<int, int>::iterator it = m.begin(); it != m.end();it++)
{
cout << "key = " << it->first << " value" << it->second << endl;
}
cout << m[5] <<endl;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value" << it->second << endl;
}
//cout << m[4] << endl;
if ( m.empty())
{
cout << "空" << endl;
}
else
{
cout << "size = " << m.size() << endl;
}
}
/*
map删除操作
clear();//删除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(keyElem);//删除容器中key为keyElem的对组。
3.8.2.6 map查找操作
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/
void test02()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
m.erase(1);
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value" << it->second << endl;
}
map<int,int>::iterator pos = m.find(2);
if (pos != m.end())
{
cout << "找到:key" << pos->first << " value:" << pos->second << endl;
}
else
{
cout << "未找到" << endl;
}
int num = m.count(3); //map的count 要么0 要么1
cout << "num = " << num << endl;
// lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
map<int,int>::iterator ret = m.lower_bound(3);
if (ret != m.end())
{
cout << "lower_bound 中key" << ret->first << " value: " << ret->second << endl;
}
else
{
cout << "未找到" << endl;
}
//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
ret = m.upper_bound(3);
if (ret != m.end())
{
cout << "upper_bound 中key" << ret->first << " value: " << ret->second << endl;
}
else
{
cout << "未找到" << endl;
}
//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
pair<map<int, int>::iterator, map<int, int>::iterator> ret2 = m.equal_range(3);
if (ret2.first != m.end())
{
cout << "找到了equal_range 中的lower_bound 的key " << ret2.first->first << " value: " << ret2.first->second << endl;
}
else
{
cout << "未找到" << endl;
}
if (ret2.second != m.end())
{
cout << "找到了equal_range 中的upper_bound 的key " << ret2.second->first << " value: " << ret2.second->second << endl;
}
else
{
cout << "未找到" << endl;
}
}
//指定排序规则
class myCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test03()
{
//从大到小排序
map<int, int, myCompare> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end();it++)
{
cout << "key: " << it->first << " value: " << it->second << endl;
}
}
int main(){
//test01();
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
来源:CSDN
作者:Mr.魏(魏先生)
链接:https://blog.csdn.net/weixin_43873349/article/details/104923180