#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
void Print(int &item)
{
cout<<item<<" ";
}
int main()
{
list<int> listintegers;
list<int>::iterator listiter; //引入迭代器 //------------头插法插入元素-------------
listintegers.push_front(5);
listintegers.push_front(3);
listintegers.push_front(1);
listintegers.push_front(2);
listintegers.push_front(4); //----------尾插法插入元素----------
listintegers.push_back(6);
listintegers.push_back(8);
listintegers.push_back(7); //--------使用list的成员函数insert()插入元素到链表中
listintegers.insert(listintegers.end(),10);
listintegers.insert(listintegers.end(),9); //----------利用迭代器输出链表-----------
/* 我们在每一个算法中都使用一个或多个iterator。 我们使用它们来存取容器中的对象。
要存取一个给定的对象,我们把一个iterator指向它,然后间接引用这个iterator */
cout<<"链表为:";
for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++)
{
cout<<*listiter<<" ";
}
cout<<endl; //-------利用STL通用算法for_each()输出链表---------------
/* Print是个函数,实现对象的输出功能 */
cout<<"链表为:";
std::for_each(listintegers.begin(),listintegers.end(),Print);
cout<<endl; //------利用STL通用算法find()推断链表中是否存在某元素----------
listiter=find(listintegers.begin(),listintegers.end(),6);
if(listiter==listintegers.end())
{
cout<<"6 is not in list"<<endl;
}
else
{
cout<<"6 is in list"<<endl;
} //-------利用STL通用算法search()推断链表中是否存在某个序列------- list<int> targetlist;
targetlist.push_front(2);
targetlist.push_front(1); //定义该序列为12
listiter=search(listintegers.begin(),listintegers.end(),targetlist.begin(),targetlist.end());
if(listiter==listintegers.end())
{
cout<<"序列12 is not in list"<<endl;
}
else
{
cout<<"序列12 is in list"<<endl;
} //使用list的成员函数sort()对链表进行排序
cout<<"排序后,链表为:";
listintegers.sort();
for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++)
{
cout<<*listiter<<" ";
}
cout<<endl; //使用list的成员函数remove()删除链表元素
listintegers.remove(8);
cout<<"删除8后,链表为:";
for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++)
{
cout<<*listiter<<" ";
}
cout<<endl; //----------使用list成员函数pop_front删除链首元素----------
listintegers.pop_front();
cout<<"删除链首元素后。链表为:";
for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++)
{
cout<<*listiter<<" ";
}
cout<<endl; //----------使用list成员函数pop_back删除链尾元素----------
listintegers.pop_back();
cout<<"删除链尾元素后,链表为:";
for(listiter=listintegers.begin();listiter!=listintegers.end();listiter++)
{
cout<<*listiter<<" ";
}
cout<<endl; system("pause");
return 0; }

使用STL中的list容器实现单链表的操作的更多相关文章

  1. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  2. 【转】 STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  3. STL中的set容器的一点总结(转)

    STL中的set容器的一点总结 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂 ...

  4. C++ STL中的常用容器浅谈

    STL是C/C++开发中一个非常重要的模板,而其中定义的各种容器也是非常方便我们大家使用.下面,我们就浅谈某些常用的容器.这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中 ...

  5. stl中顺序性容器,关联容器两者粗略解释

    什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器.很简单,容器就是保存其它对象的对象 ...

  6. 深入解析C++ STL中的常用容器

    转载:http://blog.csdn.net/u013443618/article/details/49964299 这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中的 ...

  7. STL中的set容器的一点总结2

    http://blog.csdn.net/sunshinewave/article/details/8068326 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像 ...

  8. 【转】STL中的set容器的一点总结

    转自 http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html 1.关于set C++ STL 之所以得到广泛的赞誉,也 ...

  9. java实现单链表常见操作

    一.概述: 本文主要总结单链表常见操作的实现,包括链表结点添加.删除:链表正向遍历和反向遍历.链表排序.判断链表是否有环.是否相交.获取某一结点等. 二.概念: 链表: 一种重要的数据结构,HashM ...

随机推荐

  1. 大数据系列之分布式数据库HBase-1.2.4+Zookeeper 安装及增删改查实践

    之前介绍过关于HBase 0.9.8版本的部署及使用,本篇介绍下最新版本HBase1.2.4的部署及使用,有部分区别,详见如下: 1. 环境准备: 1.需要在Hadoop[hadoop-2.7.3]  ...

  2. 20行js代码制作网页刮刮乐

    分享一段用canvas和JS制作刮刮乐的代码,JS部分去掉注释不到20行代码效果如下 盖伦.jpg 刮刮乐.gif HTML部分 <body> ![](img/gailun.jpg) &l ...

  3. Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常

    Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...

  4. Django 1.10文档中文版Part4

    2.10 高级教程:如何编写可重用的apps 2.10.1 重用的概念 The Python Package Index (PyPI)有大量的现成可用的Python库.https://www.djan ...

  5. 7.Python3标准库--文件系统

    ''' Python的标准库中包含大量工具,可以处理文件系统中的文件,构造和解析文件名,还可以检查文件内容. 处理文件的第一步是要确定处理的文件的名字.Python将文件名表示为简单的字符串,另外还提 ...

  6. c++输出保留固定小数位数

    cout<<setprecision(6)<<fixed<<ans<<endl;

  7. 转:在CentOS 7.3使用yum安装 MySql5.6.24

    按照CentOS 6.5的方法在CentOS 7上安装会失败,需要按照下文进行安装. 1.安装rpm包 Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无 ...

  8. python爬取漫画

    抓取漫画的网址是:sf互动传媒 抓取漫画的由来也是看了知乎上有人说用爬取漫画,然后自己也玩玩 首页中每个漫画的url是类似这样存储的: <tr> <td height="3 ...

  9. 强大的PHP一句话后门

    强悍的PHP一句话后门  这类后门让网站.服务器管理员很是头疼,经常要换着方法进行各种检测,而很多新出现的编写技术,用普通的检测方法是没法发现并处理的. 今天我们细数一些有意思的PHP一句话木马. 1 ...

  10. 洛谷P1094纪念品分组 题解

    题目传送门 首先的思路就是贪心.先将所有的纪念品按照价格从低到高进行排序.在分别从左到右.从右到左合并纪念品.如果两端纪念品价格超过了上上限,那么就将较大的那一个纪念品独自放入.否则将两个纪念品一起放 ...