2.2 顺序容器-list
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的更多相关文章
- C++ 顺序容器基础知识总结
0.前言 本文简单地总结了STL的顺序容器的知识点.文中并不涉及具体的实现技巧,对于细节的东西也没有提及.一来不同的标准库有着不同的实现,二来关于具体实现<STL源码剖析>已经展示得全面细 ...
- c++ 顺序容器学习
所谓容器,就是一个装东西的盒子,在c++中,我们把装的东西叫做“元素” 而顺序容器,就是说这些东西是有顺序的,你装进去是什么顺序,它们在里面就是什么顺序. c++中的顺序容器一共有这么几种: vect ...
- C++ Primer 第九章 顺序容器
由于书籍上写的已经很经典了,故大部分用图片的形式来阐述概念,代码纯手打进行验证. 1.顺序容器类型:vector.deque.list.forword_list.array.string. 2.顺序容 ...
- C++学习基础四——顺序容器和关联容器
—顺序容器:vector,list,queue1.顺序容器的常见用法: #include <vector> #include <list> #include <queue ...
- C++ 顺序容器
<C++ Primer 4th>读书笔记 顺序容器内的元素按其位置存储和访问.容器类共享公共的接口,每种容器类型提供一组不同的时间和功能折衷方案.通常不需要修改代码,只需改变类型声明,用一 ...
- C++ Primer : 第九章 : 顺序容器的操作以及迭代器失效问题
顺序容器的添加.访问.删除操作以及forward_list的特殊操作,还有迭代器失效问题. 一.向容器添加元素 // array不支持这些操作 // forward_list有自己撰于的版本的inse ...
- C++ Primer : 第九章 : 顺序容器的定义、迭代器以及赋值与swap
顺序容器属于C++ STL的一部分,也是非常重要的一部分. 顺序容器包括: std::vector,包含在头文件<vector>中 std::string, 包含在头文件<strin ...
- 顺序容器:vector,deque,list
1.顺序容器:vector,deque,list 容器类共享公共接口,只要学会其中一种类型就能运用另一种类型.每种容器提供一组不同的时间和功能这种方案,通常不需要修改代码,秩序改变类型声明,每一种容器 ...
- C++ Primer 随笔 Chapter 9 顺序容器
参考:http://www.cnblogs.com/kurtwang/archive/2010/08/19/1802912.html 1..顺序容器:vector(快速随机访问):list(快速插入 ...
- C++ Primer 5th 第9章 顺序容器
练习9.1:对于下面的程序任务,vector.deque和list哪种容器最为适合?解释你的选择的理由.如果没有哪一种容器优于其他容器,也请解释理由.(a) 读取固定数量的单词,将它们按字典序插入到容 ...
随机推荐
- PHPCMS几个有用的全局函数
1.$site_setting = get_site_setting($siteid); 这个get_site_setting()函数读取的是多站点中$siteid站点的相关配置,具体位置在网站根 ...
- CSS Reset样式重置
为了让页面在各不同浏览器之间显示效果一致,CSS样式清除和重置是前端开发必需要做的事情,结合前车之鉴,整理了份CSS重置样式代码供参考. @charset "utf-8"; /* ...
- MAC地址,使用java获取IP地址和MAC地址。
MAC地址,通常在http连接的项目中,来区分唯一客户端. MAC:六组十六进制字符组成. 如:E0-3F-49-AB-DB-EB IP:四组八位的二进制字符组成. 如:10.6.62.244 /** ...
- [POJ1328]Radar Installation
[POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...
- (转载)XML解析之-XStream解析
转载来源:http://hwy584624785.iteye.com/blog/1168680 本例使用XStream生成一个xml文件,再发序列化xml文件内容. XStream是一个简单的类库,可 ...
- MikroTik RouterOS防火墙与过滤详解
MikroTik RouterOS能对包状态过滤:P2P协议过滤:源和目标NAT:对源MAC.IP地址.端口.IP协议.协议(ICMP.TCP.MSS等).接口.对内部的数据包和连接作标记.ToS 字 ...
- mysql 数据库字符集的指定
create database mydb default character set utf8 default collate utf8_general_ci;
- faad解码aac
// faad2.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <cassert> #include <io ...
- ubuntu下配置apache2多域名(apache2.4.6)
Ubuntu 在 Linux 各发行版中, 个人用户数量最多的. 很多人在本机和虚拟机中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 设置方法不相同. 1. 打开目录 /et ...
- Redis Sentinel机制与用法(一)
Sentinel spring 集群配置: 概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕 ...