3.5 stack容器
3.5.1 stack 基本概念
概念:stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口
栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为
栈中进入数据称为 — 入栈 push
栈中弹出数据称为 — 出栈 pop
可以判断是否为空
可以返回元素个数
3.5.2 stack 常用接口
功能描述:栈容器常用的对外接口
构造函数:
stack<T> stk;
//stack采用模板类实现, stack对象的默认构造形式stack(const stack &stk);
//拷贝构造函数
赋值操作:
stack& operator=(const stack &stk);
//重载等号操作符
数据存取:
push(elem);
//向栈顶添加元素pop();
//从栈顶移除第一个元素top();
//返回栈顶元素
大小操作:
empty();
//判断堆栈是否为空size();
//返回栈的大小
示例:
#include<iostream>
using namespace std;
#include<stack>
//栈stack容器
void test1()
{
//特点,符合先进后出原则
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()
{
test1();
system("pause");
return 0;
}
总结:
- 入栈 — push
- 出栈 — pop
- 返回栈顶 — top
- 判断栈是否为空 — empty
- 返回栈大小 — size
3.6 queue 容器
3.6.1 queue 基本概念
概念:Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口
队列容器允许从一端新增元素,从另一端移除元素
队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
队列中进数据称为 — 入队 push
队列中出数据称为 — 出队 pop
3.6.2 queue 常用接口
功能描述:栈容器常用的对外接口
构造函数:
queue<T> que;
//queue采用模板类实现,queue对象的默认构造形式queue(const queue &que);
//拷贝构造函数
赋值操作:
queue& operator=(const queue &que);
//重载等号操作符
数据存取:
push(elem);
//往队尾添加元素pop();
//从队头移除第一个元素back();
//返回最后一个元素front();
//返回第一个元素
大小操作:
empty();
//判断堆栈是否为空size();
//返回栈的大小
示例:
#include<iostream>
using namespace std;
#include<queue>
#include<string>
//queue队列
class person
{
public:
person(string name,int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
};
void test1()
{
//创建队列
queue<person>q;
//准备队列
person p1("唐僧", 30);
person p2("孙悟空", 300);
person p3("猪八戒", 200);
person p4("沙僧", 100);
//入队
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout << "队列大小: " << q.size() << endl;
//只要对列不为空,查看对头和队尾,出队
//队列不提供迭代器,更不支持随机访问
while (!q.empty())
{
cout << "对头元素--姓名: " << q.front().name << " 年龄: " << q.front().age << endl;
cout << "对尾元素--姓名: " << q.back().name << " 年龄: " << q.back().age << endl;
q.pop();
}
cout << "队列大小: " << q.size() << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- 入队 — push
- 出队 — pop
- 返回队头元素 — front
- 返回队尾元素 — back
- 判断队是否为空 — empty
- 返回队列大小 — size
3.7 list容器
3.7.1 list基本概念
功能: 将数据进行链式存储
链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表的组成:链表由一系列结点组成
结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
STL中的链表是一个双向循环链表
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点:
- 采用动态存储分配,不会造成内存浪费和溢出
- 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list的缺点:
- 链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大,遍历速度没有数组快。
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。
总结:STL中List和vector是两个最常被使用的容器,各有优缺点
3.7.2 list构造函数
功能描述:
- 创建list容器
函数原型:
list<T> lst;
//list采用采用模板类实现,对象的默认构造形式:list(beg,end);
//构造函数将[beg, end)区间中的元素拷贝给本身。list(n,elem);
//构造函数将n个elem拷贝给本身。list(const list &lst);
//拷贝构造函数。
示例:
#include<iostream>
using namespace std;
#include<list>
//list容器构造函数
void printlist(const list<int>l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_back(50);
//遍历容器
printlist(l1);
//区间方式构造
list<int>l2(l1.begin(), l1.end());
printlist(l2);
//拷贝构造函数方式
list<int>l3(l2);
printlist(l3);
//n个elem
list<int>l4(10, 1000);
printlist(l4);
}
int main()
{
test1();
system("pause");
return 0;
}
总结:list构造方式同其他几个STL常用容器,熟练掌握即可
3.7.3 list 赋值和交换
功能描述:
- 给list容器进行赋值,以及交换list容器
函数原型:
assign(beg, end);
//将[beg, end)区间中的数据拷贝赋值给本身。assign(n, elem);
//将n个elem拷贝赋值给本身。list& operator=(const list &lst);
//重载等号操作符swap(lst);
//将lst与本身的元素互换。
示例:
#include<iostream>
using namespace std;
#include<list>
//list容器赋值操作
void printlist(const list<int>l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_back(50);
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, 1000);
printlist(l4);
}
//交换
void test2()
{
list<int>l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_back(50);
list<int>l2;
l2.assign(10, 1000);
cout << "交换前" << endl;
printlist(l1);
printlist(l2);
l1.swap(l2);
cout << "交换后" << endl;
printlist(l1);
printlist(l2);
}
int main()
{
test1();
test2();
system("pause");
return 0;
}
总结:list赋值和交换操作能够灵活运用即可
3.7.4 list 大小操作
功能描述:
- 对list容器的大小进行操作
函数原型:
-
size();
//返回容器中元素的个数 -
empty();
//判断容器是否为空 -
resize(num);
//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。 //如果容器变短,则末尾超出容器长度的元素被删除。
-
resize(num, elem);
//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。
示例:
#include<iostream>
using namespace std;
#include<list>
//list容器赋值操作
void printlist(const list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
list<int>l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_back(50);
printlist(l1);
if (l1.empty())
{
cout << "l1为空" << endl;
}
else
{
cout << "l1不为空" << endl;
cout << "l1元素个数: " << l1.size() << endl;
}
//重新指定大小
/*l1.resize(10);
printlist(l1);*/
l1.resize(10, 100);
printlist(l1);
l1.resize(2);
printlist(l1);
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- 判断是否为空 — empty
- 返回元素个数 — size
- 重新指定个数 — resize
3.7.5 list 插入和删除
功能描述:
- 对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值匹配的元素。
示例:
#include<iostream>
using namespace std;
#include<list>
void printlist(const list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
list<int>l1;
//尾插
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
//头插
l1.push_front(100);
l1.push_front(200);
l1.push_front(300);
printlist(l1);
//尾删
l1.pop_back();
printlist(l1);
//头删
l1.pop_front();
printlist(l1);
//插入
l1.insert(l1.begin(), 1000);
printlist(l1);
//删除
l1.erase(l1.begin());
printlist(l1);
//移除
l1.push_back(10000);
l1.push_back(10000);
l1.push_back(10000);
printlist(l1);
l1.remove(10000);
printlist(l1);
//清空
l1.clear();
printlist(l1);
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- 尾插 — push_back
- 尾删 — pop_back
- 头插 — push_front
- 头删 — pop_front
- 插入 — insert
- 删除 — erase
- 移除 — remove
- 清空 — clear
3.7.6 list 数据存取
功能描述:
- 对list容器中数据进行存取
函数原型:
front();
//返回第一个元素。back();
//返回最后一个元素。
示例:
#include<iostream>
using namespace std;
#include<list>
void test1()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//cout << L1.at(0) << endl;//错误 不支持at访问数据
//cout << L1[0] << endl; //错误 不支持[]方式访问数据
//原因:list是个链表,不是连续线性空间存储结构,迭代器也是不支持随机访问
cout << "第一个元素为: " << L1.front() << endl;
cout << "最后一个元素为: " << L1.back() << endl;
//list容器的迭代器是双向迭代器,不支持随机访问
list<int>::iterator it = L1.begin();
it++;//只能一个一个访问
//it = it + 1;//错误,不可以跳跃访问,即使是+1,如果可以话+2+3都可以就是跳跃性了
it--;//支持双向
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
- list容器中不可以通过[]或者at方式访问数据
- 返回第一个元素 — front
- 返回最后一个元素 — back
3.7.7 list 反转和排序
功能描述:
- 将容器中的元素反转,以及将容器中的数据进行排序
函数原型:
reverse();
//反转链表sort();
//链表排序
示例:
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
void printlist(const list<int>l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
list<int>L1;
L1.push_back(50);
L1.push_back(30);
L1.push_back(20);
L1.push_back(40);
L1.push_back(10);
cout << "反转前" << endl;
printlist(L1);
L1.reverse();
cout << "反转后" << endl;
printlist(L1);
}
bool mycompare(int v1, int v2)
{
//降序 就让第一个数大于第二个数
return v1 > v2;
}
void test2()
{
list<int>L1;
L1.push_back(50);
L1.push_back(30);
L1.push_back(20);
L1.push_back(40);
L1.push_back(10);
cout << "排序前" << endl;
printlist(L1);
//所有不支持随机访问的迭代器的容器,不可以用标准算法
//sort(L1.begin(), L1.end());
//不支持随机访问迭代器的容器,内部会提供对应的算法
L1.sort();
cout << "排序后" << endl;
printlist(L1);
//降序
L1.sort(mycompare);
printlist(L1);
}
int main()
{
test1();
test2();
system("pause");
return 0;
}
总结:
- 反转 — reverse
- 排序 — sort (成员函数)
3.7.8 排序案例
案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
示例:
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
#include<string>
class person
{
public:
person(string name,int age,int height)
{
this->age = age;
this->height = height;
this->name = name;
}
string name;
int age;
int height;
};
bool mycompare(person &p1,person &p2)
{
if (p1.age == p2.age)
{
return p1.height > p2.height;
}
else
{
return p1.age < p2.age;
}
}
void test1()
{
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->name << " 年龄: " << it->age
<< " 身高: " << it->height << endl;
}
cout << "---------------------------------" << endl;
cout << "排序后" << endl;
L1.sort(mycompare);
for (list<person>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << "姓名: " << it->name << " 年龄: " << it->age
<< " 身高: " << it->height << endl;
}
}
int main()
{
test1();
system("pause");
return 0;
}
总结:
-
对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序
-
高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂