c++标准库中,含有链表的类list

Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.STL中 end()指向的总是无效值,取值都用迭代器,用法跟指针差不多。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中重复的元素附List用法实例:#include <iostream>#include <list>#include <numeric>#include <algorithm>using namespace std;//创建一个list容器的实例LISTINT

typedef list<int> LISTINT;//创建一个list容器的实例LISTCHAR

typedef list<char> LISTCHAR;void main(void){//--------------------------//用list容器处理整型数据//--------------------------//用LISTINT创建一个名为listOne的list对象
LISTINT listOne;//声明i为迭代器
LISTINT::iterator i;//从前面向listOne容器中添加数据
listOne.push_front (2);listOne.push_front (1);//从后面向listOne容器中添加数据
listOne.push_back (3);listOne.push_back (4);//从前向后显示listOne中的数据
cout<<"listOne.begin()--- listOne.end():"<<endl;for (i = listOne.begin(); i != listOne.end(); ++i)cout << *i << " ";cout << endl;//输出为 1 2 3 4//从后向后显示listOne中的数据

LISTINT::reverse_iterator ir;cout<<"listOne.rbegin()---listOne.rend():"<<endl;for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {cout << *ir << " ";}cout << endl;//输出为 4 3 2 1//使用STL的accumulate(累加)算法int result = accumulate(listOne.begin(), listOne.end(),0);cout<<"Sum="<<result<<endl;cout<<"------------------"<<endl;//输出为 Sum=10//--------------------------//用list容器处理字符型数据//--------------------------//用LISTCHAR创建一个名为listOne的list对象
LISTCHAR listTwo;//声明i为迭代器
LISTCHAR::iterator j;//从前面向listTwo容器中添加数据
listTwo.push_front ('A');listTwo.push_front ('B');//从后面向listTwo容器中添加数据
listTwo.push_back ('x');listTwo.push_back ('y');//从前向后显示listTwo中的数据
cout<<"listTwo.begin()---listTwo.end():"<<endl;for (j = listTwo.begin(); j != listTwo.end(); ++j)cout << char(*j) << " ";cout << endl;//输出为 B A x y//使用STL的max_element算法求listTwo中的最大元素并显示
j=max_element(listTwo.begin(),listTwo.end());cout << "The maximum element in listTwo is: "<<char(*j)<<endl;}//输出为: The maximum element in listTwo is: y

#include <iostream>#include <list>using namespace std;typedef list<int> INTLIST;//从前向后显示list队列的全部元素void put_list(INTLIST list, char *name){INTLIST::iterator plist;cout << "The contents of " << name << " : ";for(plist = list.begin(); plist != list.end(); plist++)cout << *plist << " ";cout<<endl;}//测试list容器的功能void main(void){//list1对象初始为空
INTLIST list1;   //list2对象最初有10个值为6的元素 
INTLIST list2(10,6); //list3对象最初有9个值为6的元素 
INTLIST list3(list2.begin(),--list2.end()); //声明一个名为i的双向迭代器
INTLIST::iterator i;//从前向后显示各list对象的元素
put_list(list1,"list1");put_list(list2,"list2");put_list(list3,"list3");// 输出: 空行一行;10个6一行;9个6一行。//从list1序列后面添加两个元素

list1.push_back(2);list1.push_back(4);cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;put_list(list1,"list1");//输出 2 4//从list1序列前面添加两个元素

list1.push_front(5);list1.push_front(7);cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;put_list(list1,"list1");//输出 7 5 2 4//在list1序列中间插入数据

list1.insert(++list1.begin(),3,9);cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;put_list(list1,"list1");//输出 7 9 9 9 5 2 4//测试引用类函数

cout<<"list1.front()="<<list1.front()<<endl;   //输出 7

cout<<"list1.back()="<<list1.back()<<endl;  //输出 4//从list1序列的前后各移去一个元素

list1.pop_front();list1.pop_back();cout<<"list1.pop_front() and list1.pop_back():"<<endl;put_list(list1,"list1");//输出 9 9 9 5 2//清除list1中的第2个元素

list1.erase(++list1.begin());cout<<"list1.erase(++list1.begin()):"<<endl;put_list(list1,"list1");//输出 9 9 5 2//对list2赋值并显示

list2.assign(8,1);cout<<"list2.assign(8,1):"<<endl;put_list(list2,"list2");//输出 1 1 1 1 1 1 1 1 【八个1】//显示序列的状态信息

cout<<"list1.max_size(): "<<list1.max_size()<<endl;   //输出 1073741823

cout<<"list1.size(): "<<list1.size()<<endl;       //输出 4

cout<<"list1.empty(): "<<list1.empty()<<endl;   //输出 0//list序列容器的运算
put_list(list1,"list1");  //输出 9 9 5 2
put_list(list3,"list3");  //输出  9 9 9 9 9 9 9 9 9 【9个9】

cout<<"list1>list3: "<<(list1>list3)<<endl;  //输出 1

cout<<"list1<list3: "<<(list1<list3)<<endl;  //输出 0//对list1容器排序

list1.sort();put_list(list1,"list1");  //输出 2 5 9 9//结合处理

list1.splice(++list1.begin(), list3);put_list(list1,"list1");  //输出 2 6 6 6 6 6 6 6 6 6 5 9 9【在2后面插入list3】
put_list(list3,"list3");  //输出 空行

} 补充:STL标准函数find进行vector 、list链表查找#include <vector>#include <algorithm>#include <iostream>using namespace std;class example{public:example(int val){i = val;}bool operator==(example const & rhs){return (i == rhs.i) ? true : false;}private:int i;};int main(void){vector<example> ve;ve.push_back(1);  //若这里为压入2,则程序运行就会奔溃,因为迭代器指针it未指向任何有 //效的地址!!!
vector<example>::iterator it;example elem(1);    //定义类对象elem
it = find(ve.begin(), ve.end(), elem);cout<<boolalpha<<(*it == elem);  //输出 true

}#include <list>#include <vector>#include <algorithm>#include <iostream>using namespace std;typedef list<int> LISTINT;int main(void){int a[5] = {1,5,3,5,6};LISTINT ls1;ls1.assign(a,a+5);LISTINT::iterator it;for( it=ls1.begin(); it!=ls1.end(); it++)cout<<*it<<" ";cout<<endl;//输出 1 5 3 5 6
ls1.insert( ls1.end(), 4 );for( it=ls1.begin(); it!=ls1.end(); it++)cout<<*it<<" ";cout<<endl;//输出 1 5 3 5 6 4
ls1.remove( 5 );for( it=ls1.begin(); it!=ls1.end(); it++)cout<<*it<<" ";cout<<endl;}//输出 1 3 6 4 【5元素全部被删除了】/* 输出如下1 5 3 5 61 5 3 5 6 41 3 6 4*/

 

转载于:https://www.cnblogs.com/renyuan/archive/2013/05/21/3091524.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/295478.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

读懂路由表

教你读懂路由表源码:--------------------------------------------------------------------------------Active Routes: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 192.16…

C# 用IrisSkin4.dll美化你的WinForm

1. 将IrisSkin4.dll动态文件导入当前项目引用中。具体操作为&#xff1a;解决方案资源管理器->当前项目->引用->右键->添加引用&#xff0c;找到IrisSkin4.dll文件&#xff0c;然后加入即可。建议&#xff0c;最好把IrisSkin4.dll文件放在当前项目\bin\Debug文件中…

SpringMVC中的文件上传

这是用的是SpringMVC-3.1.1、commons-fileupload-1.2.2和io-2.0.1 web.xml文件 <?xml version"1.0" encoding"UTF-8"?> <web-app version"2.5" xmlns"http://java.sun.com/xml/ns/javaee" xmlns:xsi"http://www.w3.o…

JavaScript对象、JSON对象、JSON字符串的区别

一、首先看下什么是JSON JSON&#xff1a;JavaScript Object Natation&#xff0c;JavaScript对象的表现形式&#xff0c;已经发展成一种轻量级的数据交换格式。 JavaScript对象的表现形式&#xff0c;指定义JS对象的一种方式。数据交换格式&#xff0c;即用于交换的数据格式。…

全球最大油田、金矿、煤矿、铁矿、铜矿,究竟哪个最值钱?

全世界只有3.14 % 的人关注了爆炸吧知识1世界上最大油田&#xff1a;沙特加瓦尔油田&#xff08;Ghawar Oil field&#xff09;加瓦尔油田位于沙特阿拉伯东部&#xff0c;首都利雅得以东约500km处&#xff0c;它探明的石油可采储量为114.8亿吨&#xff0c;天然气储量9240亿立方…

JavaScript自动设置IFrame高度(兼容各主流浏览器)

调用方式如下 <iframe id"ifm" name"ifm" οnlοad"SetIFrameHeight(ifm)" src"http://www.qq.com" /> function SetIFrameHeight(down) {var Sys {};var ua navigator.userAgent.toLowerCase();var s;(s ua.match(/msie ([\…

MVC3快速搭建Web应用(二)

easyui与mvc的结合 上一篇文章发布后&#xff0c;自己又仔细读了数遍&#xff0c;感觉一是文笔太差&#xff0c;二是描述逻辑比较混乱&#xff0c;客观原因是涉及到东西其实蛮多的&#xff0c;那三个步骤不可能在一篇短短的文章中就可以描述清楚。此篇笔者将尽量更加详尽一些。…

如何通过 .NETCore 获取 Linux,Mac 的硬件信息?

咨询区 Pascal Jackson&#xff1a;请问我如何通过 .NET Core 去识别 Linux/Mac 电脑上的硬件信息&#xff1f;在 windows 上我可以通过 System.Management 下 WMI Query 很容易获取&#xff0c;比如下面的代码&#xff1a;ManagementObjectSearcher searcher new ManagementO…

KVM 虚拟机自动克隆脚本

#!/bin/sh#----------------------------------## virt-clone auto script[虚拟机自动克隆脚本]# create by xuekun# date 2015-12-26# 开源人#----------------------------------## Source function library.. /etc/rc.d/init.d/functions#SOUR_SERVER$1DIRE_SERVER$2IP_ADDR…

SpringMVC整合Shiro

这里用的是SpringMVC-3.2.4和Shiro-1.2.2&#xff0c;示例代码如下 首先是web.xml <?xml version"1.0" encoding"UTF-8"?> <web-app version"2.5"xmlns"http://java.sun.com/xml/ns/javaee"xmlns:xsi"http://www.w3.o…

这个发热鞋垫厉害了,有它冬天再也不怕脚冷

▲ 点击查看冬天一到&#xff0c;小爆发现身边的“抖友”又开始上线了&#xff01;至于为什么会抖脚&#xff1f;有盆友说&#xff0c;当然不是真的想抖&#xff0c;而是因为脚太冷冷冷了&#xff01;有时候穿了棉袜厚鞋&#xff0c;脚都是冷冰冰的&#xff0c;感觉就像踩在冰窟…

.NET 6新特性试用 | 热重载

前言在以前的开发模式下&#xff0c;我们修改代码后必须重新编译、重新运行才能看到效果。而热重载提供了这样一种特性&#xff0c;它允许你在项目正在运行时修改代码&#xff0c;并将代码更改立即应用于正在运行的应用程序上。热重载的目的是尽可能节省编辑之间的应用重启次数…

加速你的Hibernate引擎(上)

为什么80%的码农都做不了架构师&#xff1f;>>> 1.引言 Hibernate是最流行的对象关系映射&#xff08;ORM&#xff09;引擎之一&#xff0c;它提供了数据持久化和查询服务。 在你的项目中引入Hibernate并让它跑起来是很容易的。但是&#xff0c;要让它跑得好却是需…

WSUS服务器的建立以及客户端发布

http://yuelei.blog.51cto.com/202879/81676转载于:https://blog.51cto.com/439810/909642

Spring MVC 中 HandlerInterceptorAdapter过滤器的使用

一般情况下&#xff0c;对来自浏览器的请求的拦截&#xff0c;是利用Filter实现的&#xff0c;这种方式可以实现Bean预处理、后处理。 Spring MVC的拦截器不仅可实现Filter的所有功能&#xff0c;还可以更精确的控制拦截精度。 Spring为我们提供了org.springframework.web.s…

7部必看的纪录片,每一部都堪称经典,让人叹为观止!

全世界只有3.14 % 的人关注了爆炸吧知识纪录片的一大重要意义&#xff0c;就在于它能将我们的视野和脚步&#xff0c;引向我们无法企及的地方和领域&#xff0c;又能让那些我们曾经到过的地方、经历过的人事&#xff0c;变得更有深意。今天&#xff0c;就给大家分享7部顶级纪录…

通过SQL Server操作MySQL的步骤和方法

在多种数据库环境下&#xff0c;经常会遇见在不同数据库之间转换数据和互相进行操作的情况。以下简要介绍下用SQL Server操作MySQL的步骤和方法。 1 操作前的准备 1.1 安装MySQL驱动 想要在SQL Server中操作MySQL&#xff0c;首先要在SQL Server所在的服务器上安装MySQL的驱动。…

ubuntu 新增mysql用户_Ubuntu中给mysql添加新用户并分配权限

一.Ubuntu下启动mysql方法&#xff1a;/etc/init.d/sudo mysqld二.用户添加bingt;mysql -u rootmysqlgt; grant 权限1,权限2,...权限n on一.Ubuntu下启动mysql方法&#xff1a;/etc/init.d/sudo mysqld二.用户添加bin>mysql -u rootmysql> grant 权限1,权限2,...权限n on…

ABP Framework 5.0 RC.1 新特性和变更说明

.Net 6.0 发布之后&#xff0c;ABP Framework 也在第一时间进行了升级&#xff0c;并在一个多星期后&#xff08;2021-11-16&#xff09;发布了 5.0 RC.1 &#xff0c;新功能和重要变更基本已经确定。5.0版本新特性5.0版本新特性列表&#xff1a;•静态 C# 和 JavaScript 客户端…

技术成长的困扰

学习知识的来源都是微信公众号、微博、博客&#xff0c;太碎片化&#xff0c;造成的结果是没有自己的知识体系&#xff0c;不能从整个知识结构层面去看待问题。转载于:https://www.cnblogs.com/samniu/p/5147191.html