list(双向链表)

1)

*  :包含头文件list

**:不支持随机存取;增删元素时间是常数,只需要修改指针

2)成员函数

*  :vector的成员函数list基本都有

**:以下是部分独有成员函数

sort()算法需要随机访问,故list不支持,所以引入一个成员函数sort()

3)list示例

*

//常用成员函数示例
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
class A{
private:
int n;
public:
A(int n_){
n=n_;
}
friend bool operator<(const A & a,const A & a2);
friend bool operator==(const A & a,const A & a2);
friend ostream & operator<<(ostream & o, const A & a2);
};
bool operator<(const A & a,const A & a2){
return a.n<a2.n;
}
bool operator==(const A & a,const A & a2){
return a.n==a2.n;
}
ostream & operator<<(ostream & o,const A & a){
o<<a.n;
return o;
} template<class T>
void Print(T first,T last){
for(;first!=last;++first)
cout<<*first<<" ";
cout<<endl;
} int main(){ A a[]={,,,,};
A b[]={,,,,,,};
list<A>lst1(a,a+),lst2(b,b+);
lst1.sort(); //sort()此处是成员函数,不是算法
cout<<"1)"; Print(lst1.begin(),lst1.end());
lst1.remove(); //删除和2相等的参数
cout<<"2)";Print(lst1.begin(),lst1.end());
lst2.pop_front(); //删除第一个元素
cout<<"3)"; Print(lst2.begin(),lst2.end());
lst2.unique(); //删除和前一个相等的元素
cout<<"4)"; Print(lst2.begin(),lst2.end());
lst2.sort();
lst1.merge(lst2);//合并lst2到lst1,并删除lst2
cout<<"5)"; Print(lst1.begin(),lst1.end());
cout<<"6)";Print(lst2.begin(),lst2.end()) ;
lst1.reverse(); //前后颠倒
cout<<"7)"; Print(lst1.begin(),lst1.end());
lst2.insert(lst2.begin(),a+,a+);
list<A>::iterator p1,p2,p3;
p1=find(lst1.begin(),lst1.end(),);
p2=find(lst2.begin(),lst2.end(),);
p3=find(lst2.begin(),lst2.end(),);
lst1.splice(p1,lst2,p2,p3);//将[p2,p3)插入p1之前,并从lst2中删除
cout<<"8)";Print(lst1.begin(),lst1.end());
cout<<"9)";Print(lst2.begin(),lst2.end()); return ;
}

**

//list 的约瑟夫问题
#include<iostream>
#include<list>
using namespace std;
int main(){
list<int>monkeys;
int n,m;
while(true){
cin>>n>>m;
if(n==&&m==) break;
monkeys.clear();
for(int i=;i<=n;++i)
monkeys.push_back(i);
list<int>::iterator it=monkeys.begin();
while(monkeys.size()>){
for(int i=;i<m;++i){
++it;
if(it==monkeys.end())
it=monkeys.begin();
}
it=monkeys.erase(it);
if(it==monkeys.end())
it=monkeys.begin();
}
cout<<*it<<endl;
}
return ;
}

此例用vector也可以,因为vector的erase操作牵涉元素的移动,不是常数时间完成,n很大时在速度上有明显差别。

2.2 顺序容器-list的更多相关文章

  1. C++ 顺序容器基础知识总结

    0.前言 本文简单地总结了STL的顺序容器的知识点.文中并不涉及具体的实现技巧,对于细节的东西也没有提及.一来不同的标准库有着不同的实现,二来关于具体实现<STL源码剖析>已经展示得全面细 ...

  2. c++ 顺序容器学习

    所谓容器,就是一个装东西的盒子,在c++中,我们把装的东西叫做“元素” 而顺序容器,就是说这些东西是有顺序的,你装进去是什么顺序,它们在里面就是什么顺序. c++中的顺序容器一共有这么几种: vect ...

  3. C++ Primer 第九章 顺序容器

    由于书籍上写的已经很经典了,故大部分用图片的形式来阐述概念,代码纯手打进行验证. 1.顺序容器类型:vector.deque.list.forword_list.array.string. 2.顺序容 ...

  4. C++学习基础四——顺序容器和关联容器

    —顺序容器:vector,list,queue1.顺序容器的常见用法: #include <vector> #include <list> #include <queue ...

  5. C++ 顺序容器

    <C++ Primer 4th>读书笔记 顺序容器内的元素按其位置存储和访问.容器类共享公共的接口,每种容器类型提供一组不同的时间和功能折衷方案.通常不需要修改代码,只需改变类型声明,用一 ...

  6. C++ Primer : 第九章 : 顺序容器的操作以及迭代器失效问题

    顺序容器的添加.访问.删除操作以及forward_list的特殊操作,还有迭代器失效问题. 一.向容器添加元素 // array不支持这些操作 // forward_list有自己撰于的版本的inse ...

  7. C++ Primer : 第九章 : 顺序容器的定义、迭代器以及赋值与swap

    顺序容器属于C++ STL的一部分,也是非常重要的一部分. 顺序容器包括: std::vector,包含在头文件<vector>中 std::string, 包含在头文件<strin ...

  8. 顺序容器:vector,deque,list

    1.顺序容器:vector,deque,list 容器类共享公共接口,只要学会其中一种类型就能运用另一种类型.每种容器提供一组不同的时间和功能这种方案,通常不需要修改代码,秩序改变类型声明,每一种容器 ...

  9. C++ Primer 随笔 Chapter 9 顺序容器

     参考:http://www.cnblogs.com/kurtwang/archive/2010/08/19/1802912.html 1..顺序容器:vector(快速随机访问):list(快速插入 ...

  10. C++ Primer 5th 第9章 顺序容器

    练习9.1:对于下面的程序任务,vector.deque和list哪种容器最为适合?解释你的选择的理由.如果没有哪一种容器优于其他容器,也请解释理由.(a) 读取固定数量的单词,将它们按字典序插入到容 ...

随机推荐

  1. CSS 继承深度解析

    FROM ME: 之前在研究前端性能优化的时候,就有学习关于CSS中“善用CSS中的继承”. 原文:CSS Inheritance, The Cascade And Global Scope: You ...

  2. 随机添加一个Class,Class提前写好

    $("").hover(function(){ var ary = ["red","green","blue",]; v ...

  3. OpenCV图像轮廓检测

    轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1  cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...

  4. Python 列表元素排重uniq

    # -*- coding: gbk -*- def uniq(ls): lsCopy=[e for e in ls] for i in xrange(1,len(ls)): for j in xran ...

  5. ITIL与ITSM的联系与区别

    1.ITIL(IT Infrastructure Library)是CCTA(英国国家计算机和电信局)于20世纪80年代末开发的一套IT服务管理标准库,它把英国各个行业在IT管理方面的最佳实践归纳起来 ...

  6. c#.netGr idView1在div不局中

    <div style="margin:0 auto;text-align:center;" >//可以用GridView剧中 <asp:GridView ID=& ...

  7. Unity3d 换装Avatar系统

    原理就是用新造的部件和角色的骨骼进行重新对接. demo的使用方法: PartIdx设置要换那个部件[0,4],一共5个部件 EquipIdx设置要更换部件的装备索引[0,1],具体看我的Change ...

  8. 配置ubuntu虚拟机备忘

    #1配置minicom sudo minicom -s sudo minicom -w #1.配置网络,嵌入机的ip地址 ifconfig eth0 10.5.52.202 #2.挂载文件,把虚拟主机 ...

  9. 16. javacript高级程序设计-HTML5脚本编程

    1. HTML5脚本编程 l 跨文档消息传递API能够让我们在不降低同源策略安全性的前提下,在来至不同的域的文档间传递消息 l 原生拖放功能可以方便的指定某个元素是否可以拖动,并在放置时做出响应.还可 ...

  10. nyoj130 相同的雪花

    相同的雪花 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 You may have heard that no two snowflakes are alike. ...