学习目标:
学习内容:
1. Stack容器相关知识
1.1 stack的结构说明
1.2 stack的常用接口
//stack容器
#include<iostream>
#include<string>
using namespace std;
#include<stack>
void test()
{
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
cout << "栈的大小为:" << s.size() << endl;
while (!s.empty())
{
cout << "栈顶的元素为:" << s.top() << endl;
s.pop();
}
cout << "栈的大小为:" << s.size() << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2. queue容器相关知识
2.1 queue常用接口
#include<queue>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
string m_Name;
int m_Age;
};
void test()
{
queue<Person>q;
Person p1("唐僧", 18);
Person p2("孙悟空",100);
Person p3("猪八戒", 150);
Person p4("沙僧", 200);
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.front().m_Age << endl;
q.pop();
}
cout << "结束时队列的大小:" << q.size() << endl;
}
int main()
{
test();
system("pause");
return 0;
}
3. list容器相关知识
3.1 list的概念
3.2 list的构造函数
//list构造函数
#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << (*it) << " ";
}
cout << endl;
}
void test()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
printList(L);
//区间构造
list<int>L1(L.begin(), L.end());
printList(L1);
//拷贝构造
list<int>L2;
L2 = L;
printList(L2);
list<int>L3(4, 5);
printList(L3);
}
int main()
{
test();
system("pause");
return 0;
}
3.3 list的赋值和交换
//list赋值与交换
#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it!= L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.push_back(50);
printList(L);
//等号赋值
list<int>L1;
L1 = L;
printList(L1);
//assign赋值
list<int>L2;
L2.assign(L.begin(), L.end());
printList(L2);
//assign赋值
list<int>L3;
L3.assign(5, 100);
printList(L3);
//交换操作
cout << "交换操作:" << endl;
L.swap(L3);
printList(L);
printList(L3);
}
int main()
{
test();
system("pause");
return 0;
}
3.4 list的大小操作
//list容器大小操作
#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.push_back(50);
printList(L);
if(L.empty())
{
cout << "L为空!!!" << endl;
}
else
{
cout << "L不为空!!!" << endl;
cout << "L的元素大小为:" << L.size() << endl;
}
L.resize(10);
printList(L);
}
int main()
{
test();
system("pause");
return 0;
}
3.5 list的插入和删除
#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test()
{
list<int>L;
//尾插
L.push_back(10);
L.push_back(20);
L.push_back(30);
//头插
L.push_front(300);
L.push_front(200);
L.push_front(100);
cout << "original_List:" ;
printList(L);
//尾删
L.pop_back();
cout << "pop-back_List:" ;
printList(L);
//头删
L.pop_front();
cout << "pop-front_List:" ;
printList(L);
//插入
L.insert(L.begin(), 99);
cout << "Insert_List:" ;
printList(L);
//偏移
list<int>::iterator it = L.begin();
L.insert(++it, 500);
cout << "偏移:";
printList(L);
//删除
it = L.begin();
L.erase(++it);
cout << "删除:";
printList(L);
//移除
L.push_back(20);
L.push_back(20);
L.push_back(20);
cout << "移除前:";
printList(L);
L.remove(20); //直接移除元素
cout << "移除后:";
printList(L);
//清空
L.clear();
cout << "清空后:";
printList(L);
}
int main()
{
test();
system("pause");
return 0;
}
3.6 list的数据存取
list数据存取不支持中括号[]和@的方式,原因:list的本质是链表,不是用连续线性空间存储数据。
//数据存取
#include<iostream>
#include<list>
using namespace std;
void test()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.push_back(50);
//在list容器中,不支持中括号[]和at的访问方式
//list的本质是链表,不是用连续线性空间来存储数据的
cout << "list中第一个元素为:" << L.front() << endl;
cout << "list中最后一个元素为:" << L.back() << endl;
}
int main()
{
test();
system("pause");
return 0;
}
3.7 list的反转和排序
//反转和排序
#include<iostream>
#include<list>
using namespace std;
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << (*it) << " ";
}
cout << endl;
}
bool myCompare(int a, int b)
{
return a > b;
}
void test()
{
list<int>L;
L.push_back(50);
L.push_back(20);
L.push_back(10);
L.push_back(40);
L.push_back(30);
printList(L);
L.reverse();
cout << "反转:";
printList(L);
//排序
//sort(L.begin(),L.end())
//printList(L)
/*以上两行无法运行:
所有不支持随机访问迭代器的容器都不可以用标准算法;
不支持随机访问迭代器的容器,内部一般都会提供一些对应的算法来解决;*/
L.sort();
cout << "升序:"; //默认为升序
printList(L);
//降序
L.sort(myCompare);
cout << "降序:";
printList(L);
}
int main()
{
test();
system("pause");
return 0;
}
3.8 list的反转和排序
//案例
#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;
};
void printList(const list<Person>& L)
{
for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout <<"姓名:"<< (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;
}
}
bool myCompare(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 test()
{
list<Person>L;
Person p1("刘备", 30, 175);
Person p2("关羽", 29, 185);
Person p3("张飞", 28, 184);
Person p4("曹操", 35, 175);
Person p5("孙权", 30, 180);
Person p6("赵云", 28, 185);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
cout << "排序前:" << endl;
printList(L);
cout << "----------------------------" << endl;
cout << "排序后:" << endl;
L.sort(myCompare);
printList(L);
}
int main()
{
test();
system("pause");
return 0;
}