#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. UVALive 3668 A Funny Stone Game

    题目链接:UVALive - 3668 题目大意为给定n堆石子,每次的操作是选择三个数i<j<=k,从i中拿一枚石子,在j和k中分别放入一枚石子.不能操作者输.求先手是否能赢,若可以,则输 ...

  2. 在Linux上安装pycharm

    1.首先在官网下载pycharm并进行提取,将提取的文件夹放在/usr下面(或者任意位置) 2.然后vi /etc/hosts 编辑 将0.0.0.0 account.jetbrains.com添加到 ...

  3. JVM内存分配及GC简述

    在阐述JVM的内存区域之前,先来看下计算机的存储单位.从小到大依次为Bit,Byte,KB,MB,GB,TB.相邻的单位相差2的10次方. 计算机运行中的存储元件主要分为寄存器(位于CPU)和内存,寄 ...

  4. Homestead在windows7 下的搭建

    遇到的问题有 1.Homestead 的版本问题,教程git版本是 v5,最新是v7的,如果用最新,就要求vagrant的版本是 2.0的: 2.启动homestead后,出现 No input fi ...

  5. [Linux: vim]vim自动生成html代码

    如果直接将vim编辑的文字复制粘贴到一些blog的编辑器中,这些代码将会是死板的白纸黑字.如果能加入关键字高亮功能就好了,这样代码阅读起来会很方便.一些blog的编辑器提供了这项功能,一些没有,一些支 ...

  6. P3960 列队

    这是NOIP 2017最后一道题 不知道这道题有没有人代码写的和我一样麻烦. Solution 30分暴力 维护每行每列的元素. 每次删除一个元素的时候, 需要修改一行一列 因此复杂度上界\(O(nm ...

  7. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  8. OPENSSL问题,使用fsockopen()函数提示错误

    环境配置 系统环境 CentOS7.2WDCP v3.2.2 lanmp PHP 多版本 指定使用5.6 OpenSSL 1.0.2h  3 May 2016 php.ini相关设置allow_url ...

  9. PhpStorm设置函数注释模板

    *设置位置:"Settings"->"file templates"; 如下图,设置头部注释.类注释以及函数注释,时间.用户名.文件名称等随机改变的属性, ...

  10. spring boot 笔记

    1.不能访问非启动类中的@RequestMapping 路径: 启动类注解 1.@Controller @EnableAutoConfiguration @ComponentScan 或 2.@Spr ...