一、list 定义和初始化
只需要简单的 list<TYPE> my_list; 就可以完成对一个 list 的定义了。不需要 new。
初始化的话就要用到 list 的构造函数。
一个简单的例子是:
int myints[] = {75,23,65,42,13};
list<int> mylist (myints, myints+5);
二、list 函数介绍
迭代器
函数名 | 作用 |
begin | 将迭代器返回到开头(Return iterator to beginning) |
end | 将迭代器返回到最后(Return iterator to end) |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
容量
函数名 | 作用 |
empty | 检查容器是否为空 |
size | 返回当前容器内元素个数 |
max_size | 返回当前容器能容纳的最大元素数量 |
元素访问
函数名 | 作用 |
front | 访问第一个元素 |
back | 访问最后一个元素 |
更改 list
函数名 | 作用 |
assign | Assign new content to container |
push_front | 将元素插入到开头 |
pop_front | 删除第一个元素 |
push_back | 将元素插入到最后 |
pop_back | 删除最后一个元素 |
insert | 插入元素 |
erase | 删除元素 |
swap | 交换两个 list 内容 |
resize | 改变容器大小 |
clear | 删除容器所有内容 |
操作
函数名 | 作用 |
splice | 合并两个 list |
remove | 根据值删除元素 |
remove_if | 删除满足条件的元素 |
unique | 删除重复的值 |
merge | 合并排好序的 list |
sort | 对容器内的元素排序 |
reverse | 将元素反序 |
三、函数举例
assign()
#include <iostream>
#include <list>
using namespace std;
template <class T>
void print_list(list<T> my_list)
{
for (typename list<T>::iterator it = my_list.begin(); it != my_list.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
int main ()
{
list<int> first;
list<int> second;
first.assign (7, 100); // 7 ints with value 100
print_list(first);
// 100 100 100 100 100 100 100
second.assign (first.begin(),first.end()); // a copy of first
print_list(second);
// 100 100 100 100 100 100 100
int myints[]= {1776, 7, 4};
first.assign (myints, myints+3); // assigning from array
print_list(first);
// 1776 7 4
cout << "Size of first: " << int (first.size()) << '\n';
cout << "Size of second: " << int (second.size()) << '\n';
// Size of first: 3
// Size of second: 7
return 0
}
begin() —— 对 list 进行顺序遍历(同end())
#include <iostream>
#include <list>
using namespace std;
int main ()
{
int myints[] = {75,23,65,42,13};
list<int> mylist (myints,myints+5);
cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
cout << '\n';
// mylist contains: 75 23 65 42 13
return 0;
}
erase()——动态删除
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<int> mylist;
list<int>::iterator it1,it2;
// set some values:
for (int i=1; i<10; ++i) mylist.push_back(i*10);
// 10 20 30 40 50 60 70 80 90
it1 = it2 = mylist.begin(); // ^^
advance (it2,6); // ^ ^
++it1; // ^ ^
it1 = mylist.erase (it1); // 10 30 40 50 60 70 80 90
// ^ ^
it2 = mylist.erase (it2); // 10 30 40 50 60 80 90
// ^ ^
++it1; // ^ ^
--it2; // ^ ^
mylist.erase (it1,it2); // 10 30 60 80 90
// ^
cout << "mylist contains:";
for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
cout << ' ' << *it1;
cout << '\n';
return 0;
}
insert()——插入元素
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main ()
{
list<int> mylist;
list<int>::iterator it;
// set some initial values:
for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // it points now to number 2 ^
mylist.insert (it,10); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist.insert (it,2,20); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
vector<int> myvector (2,30);
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n';
// mylist contains: 1 10 20 30 30 20 2 3 4 5
return 0;
}
四、例题
https://www.luogu.com.cn/problem/P1160
#include <cstdio>
#include <list>
using namespace std;
using Iter = list<int>::iterator;
const int maxN = 1e5 + 10;
Iter pos[maxN];
list<int> queList;
bool erased[maxN];
int N;
void buildQueue()
{
queList.push_front(1);
pos[1] = queList.begin();
for (int i = 2; i <= N; i++)
{
int k, p;
scanf("%d%d", &k, &p);
if (p == 0)
{
pos[i] = queList.insert(pos[k], i); //left
}
else
{
auto nextIter = next(pos[k]);
pos[i] = queList.insert(nextIter, i); //right
}
}
int M;
scanf("%d", &M);
for (int x, i = 1; i <= M; i++)
{
scanf("%d", &x);
if (!erased[x])
{
queList.erase(pos[x]);
}
erased[x] = true;
}
}
int main()
{
scanf("%d", &N);
buildQueue();
bool first = true;
for (int x: queList)
{
if (!first)
putchar(' ');
first = false;
printf("%d", x);
}
putchar('\n');
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int L, R;
}a[100003];
int n, m;
inline void addRight(int x, int pos) { //插入右边
a[x].L = pos;
a[a[pos].R].L = x;
a[x].R = a[pos].R;
a[pos].R = x;
}
inline void addLeft(int x, int pos) { //插入左边
a[x].R = pos;
a[a[pos].L].R = x;
a[x].L = a[pos].L;
a[pos].L = x;
}
inline void del(int x) {
if(a[x].L == -1) return;
a[a[x].L].R = a[x].R;
a[a[x].R].L = a[x].L;
a[x].L = -1;
a[x].R = -1;
}
inline void go() {
int x = a[0].R;
while(1) {
cout<<x<<" ";
if(a[x].R == -1) break;
x = a[x].R;
}
}
inline void init() {
for(int i = 1; i <= n; ++i) a[i].L = a[i].R = -1;
a[1].R = -1; a[1].L = 0; a[0].R = 1;
}
int main() {
scanf("%d", &n);
int cmd1, cmd2;
init();
for(int i = 2; i <= n; ++i) {
scanf("%d %d", &cmd1, &cmd2);
if(!cmd2) addLeft(i, cmd1);
else addRight(i, cmd1);
}
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
scanf("%d", &cmd1);
del(cmd1);
}
go();
return 0;
}