#include <iostream>
#include <list>
#include <queue>
using namespace std;
//遍历
void printL(list<int> &l)
{
list<int>::iterator it = l.begin();
while(it != l.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void func1()
{
list<int> l;
l.push_back(1);//尾部插入
l.push_back(2);
l.push_back(3);
l.push_back(4);
l.push_back(5);
l.push_back(6);
l.push_back(-1);
l.push_back(-2);
l.push_back(-3);
printL(l);
l.erase(l.begin());//删除单个字符
printL(l);
list<int>::iterator it = l.begin();
it++;
it++;
l.erase(l.begin(),it);//区间删除
printL(l);
//删除某个值
l.remove(6);//所有6值全部删除
printL(l);
//链表的逆序:链表逆序函数
l.reverse();
printL(l);
}
//优先级队列:排列好大小顺序
void func2()
{
//priority_queue<int> q;//默认从大到小
//priority_queue<int,vector<int>,less<int>> q;//从大到小
priority_queue<int,vector<int>,greater<int>> q;//从小到大
q.push(1);//往堆栈中压入元素
q.push(2);
q.push(13);
q.push(41);
q.push(6);
q.push(22);
while(!q.empty())
{
cout << q.top() << endl;//查看栈的最上端元素
q.pop();//从栈中弹出元素
}
}
int main()
{
func1();
return 0;
}
#include <list>
#include <queue>
using namespace std;
//遍历
void printL(list<int> &l)
{
list<int>::iterator it = l.begin();
while(it != l.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void func1()
{
list<int> l;
l.push_back(1);//尾部插入
l.push_back(2);
l.push_back(3);
l.push_back(4);
l.push_back(5);
l.push_back(6);
l.push_back(-1);
l.push_back(-2);
l.push_back(-3);
printL(l);
l.erase(l.begin());//删除单个字符
printL(l);
list<int>::iterator it = l.begin();
it++;
it++;
l.erase(l.begin(),it);//区间删除
printL(l);
//删除某个值
l.remove(6);//所有6值全部删除
printL(l);
//链表的逆序:链表逆序函数
l.reverse();
printL(l);
}
//优先级队列:排列好大小顺序
void func2()
{
//priority_queue<int> q;//默认从大到小
//priority_queue<int,vector<int>,less<int>> q;//从大到小
priority_queue<int,vector<int>,greater<int>> q;//从小到大
q.push(1);//往堆栈中压入元素
q.push(2);
q.push(13);
q.push(41);
q.push(6);
q.push(22);
while(!q.empty())
{
cout << q.top() << endl;//查看栈的最上端元素
q.pop();//从栈中弹出元素
}
}
int main()
{
func1();
return 0;
}