stack容器
栈容器符合先进后出
栈不允许有便历行为
栈可以判断容器是空
栈可以返回元素个数
只有一个出口
遍历是非质变的过程,是不允许其中的东西有改动的。
stack常用的接口
构造函数:
stackstk; //stack采用模版类实现,stack对象的默认构造形式
stack(const stack &stk); //拷贝构造函数
赋值操作:
stack& operator=(const stack &stk); //重载等号操作符
数据存取:
push(elem); // 向栈顶添加元素
pop( ); //从栈顶移除第一个元素
top( ); //返回栈顶元素
大小操作:
empty( ); //判断堆栈是否为空;
size( ); //返回栈的大小;
#include<iostream>
using namespace std;
#include<stack>
int main() {
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
cout << "栈stack的大小为"<<s.size() << endl;
for (int i = 0; i < 5; i++)
{
cout << "栈顶元素为" << s.top() << endl;
s.pop();
}
cout << "栈stack的大小为" << s.size() << endl;
}
queue容器
队列符合先进先出,只有队头队尾能被外界访问,因此不允许有便历行为。
#include<iostream>
using namespace std;
#include<queue>
#include<string>
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
public:
string m_Name;
int m_Age;
};
void test01() {
//创建队列
queue<Person>q;
//准备数据
Person p1("lqd1", 30);
Person p2("lqd2", 20);
Person p3("lqd3", 19);
Person p4("lqd4", 18);
Person p5("lqd5", 17);
//向队列中添加元素
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
q.push(p5);
while (!q.empty())
{
cout << "队头姓名是" << q.front().m_Name << "年龄是" << q.front().m_Age << endl;
cout << "队尾姓名是" << q.back().m_Name << "年龄是" << q.back().m_Age << endl;
cout << endl;
q.pop();
}
cout << "队列大小是" << q.size() << endl;
}
int main() {
test01();
}
list容器构造函数
STL中List和vector是两个最常被使用的容器,各有优缺点
功能:将数据进行链式存储
链表(list):是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。
链表的组成:链表由一系列结点组成
结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
STL中的链表是一个双向循环链表
链表的优点:可以对任意位置进行快速插入和删除操作
链表的便历速度没有数组快,占用空间比数组大。
链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。
list优点:
1、采用动态存储分配,不会造成内存浪费和溢出
2、链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list缺点:
1、链表灵活,但是空间和时间额外耗费较大
list一个重要的性质,插入操作和删除操作不会造成原有的list迭代器的失效,这在vector是不成立的。
list构造方式同其他几个STL常用容器
#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;
}
int main() {
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);
printList(L2);
list<int>L3(L2.begin(), L2.end());
printList(L3);
list<int>L4(10, 999);
printList(L4);
}
list赋值和交换
assign(beg,end); //将[beg,end]区间中的数据拷贝赋值给本身
assign(n,elem); //将n个elem拷贝赋值给本身
list& operator = (const list &list); //重载等号操作符
swap(list); //将list与本身的元素互换
#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 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(L1);
printList(L2);
list<int>L3(L2.begin(), L2.end());
printList(L3);
list<int>L4(10, 999);
printList(L4);
}
void test02() {
list<int>l1;
l1.push_back(90);
l1.push_back(80);
l1.push_back(70);
l1.push_back(60);
printList(l1);
list<int>l2 = l1;
printList(l2);
list<int>l3(2, 21);
printList(l3);
cout << "交换前" << endl;
printList(l2);
l2.swap(l3);
cout << "交换后" << endl;
printList(l2);
}
int main() {
test02();
}
list容器大小操作
#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 test01() {
list<int>L1;
L1.push_back(14);
L1.push_back(28);
if (L1.empty())
{
cout << "此列表为空" << endl;
}
else {
cout << "此列表非空,且元素为" << " ";
printList(L1);
cout << "列表大小为" << L1.size() << endl;
}
//重新制定大小
L1.resize(10, 9);
cout << "重新指定L1的大小后列表为" << " ";
printList(L1);
L1.resize(1);
printList(L1);
}
int main()
{
test01();
}
list插入和删除
#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 test01() {
list<int>L1;
//头插
L1.push_front(10);
L1.push_front(90);
//尾插
L1.push_back(200);
L1.push_back(300);
printList(L1);
//头删
L1.pop_front();
cout << "头删后的结果是" << endl;
printList(L1);
L1.pop_back();
cout << "尾删后的结果是" << endl;
printList(L1);
L1.insert(L1.begin(), 1000);
printList(L1);
//在头部插入1000后
list<int>::iterator it = L1.begin();
L1.insert(it, 2000);
cout << "在头部插入2000后" << endl;
printList(L1);
//删除
it = L1.begin();
L1.erase(++it);
cout << "删除列表第二个元素后" << endl;
printList(L1);
//L1.clear();
L1.remove(200);
cout << "删除列表元素200后" << endl;
printList(L1);
}
int main() {
test01();
}
数据存取
对list容器中数据进行存取
front( ); //返回第一个元素
back( ); //返回最后一个元素
list 链表不是连续的内存空间,因此不能用中括号或者是at的方式便历其中的数据。
迭代器也不支持随机访问;list容器的迭代器是双向迭代器,只能前移和后移,不支持跳跃式访问。
#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 test01() {
list<int>L1;
//头插
L1.push_front(10);
L1.push_front(90);
//尾插
L1.push_back(200);
L1.push_back(300);
printList(L1);
//头删
cout << "返回容器中第一个元素:" << L1.front() << endl;
cout << "返回容器最后一个元素" <<L1.back()<< endl;
}
int main() {
test01();
}
反转和排序
所有不支持随机访问迭代器的容器,不可用标准算法
不支持随机访问迭代器的容器,内部会提供对应的一些算法。
排序案例
#include<iostream>
using namespace std;
#include<list>
#include<string>
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("关羽", 37, 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);
cout << "------------------------" << endl;
cout << "排序前的顺序是" << endl;
for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << "姓名是" << it->m_Name << " " << "年龄是" << it->m_Age << " " << "身高是" << it->m_Height << " " << endl;;
}
L1.sort(comparePerson);
cout << "------------------------" << endl;
cout << "按照年龄排序后的顺序是" << endl;
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();
}
-----------------------------------------------------------------------------------------------------------------------------------------