list的简单应用

来源:互联网 发布:java邮箱验证基本思路 编辑:程序博客网 时间:2024/05/17 08:23

Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.

assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素

例noi:http://noi.openjudge.cn/ch0309/3339/
描述
写一个程序完成以下命令:
new id ——新建一个指定编号为id的序列(id<10000)
add id num——向编号为id的序列加入整数num
merge id1 id2——合并序列id1和id2中的数,并将id2清空
unique id——去掉序列id中重复的元素
out id ——从小到大输出编号为id的序列中的元素,以空格隔开

输入
第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。
输出
按题目要求输出。
样例输入
16
new 1
new 2
add 1 1
add 1 2
add 1 3
add 2 1
add 2 2
add 2 3
add 2 4
out 1
out 2
merge 1 2
out 1
out 2
unique 1
out 1
样例输出
1 2 3
1 2 3 4
1 1 2 2 3 3 4

1 2 3 4

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <map>#include <set>#include <list>#include <queue>#include <algorithm>#define inf 0x3f3f3f3f#define LL long longusing namespace std;list<int> a[10005];int main(){    int n;    string op;    cin>>n;    while(n--)    {        cin>>op;        int x,y;        if(op=="new")        {            cin>>x;        }        else if(op=="add")        {            cin>>x>>y;            a[x].push_back(y);        }        else if(op=="merge")        {            cin>>x>>y;            a[x].merge(a[y]);        }        else if(op=="unique")        {            cin>>x;            a[x].sort();            a[x].unique();        }        else if(op=="out")        {            cin>>x;            if(a[x].empty())            {                cout<<endl;            }            else            {                a[x].sort();                list<int> :: iterator it;                for(it=a[x].begin();it!=a[x].end();it++)                {                    cout<<*it<<" ";                }                cout<<endl;            }        }    }    return 0;}
原创粉丝点击