C++ list容器系列功能函数详解
C++ list函数详解
首先说下eclipse工具下怎样debug:方法:你先要设置好断点,然后以Debug方式启动你的应用程序,不要用run的方式,当程序运行到你的断点位置时就会停住,也会提示你进入到Debug视图方式操作,
F5是进入到函数或语句块的内部
F6是单步运行,一行一行的走,
F7可以跳当前监听函数或语句块
F8 会直接跳到下个断点。
Ctrl+R运行到光标处。
下面进入主题:
一、构造、析构函数、= 运算符
1、功能:声明list容器。4种方式
list<int> first; // empty list of ints
list<int> second (4,100); // four ints with value 100。4个100
list<int> third (second.begin(),second.end()); // iterating through second
list<int> fourth (third); // a copy of third
2、功能:注销list。 ~list ( );
3、原型:list1 = list2;
功能:将list2赋值给list1,包括list的所有元素以及list2的size
返回值:this指针
二、返回迭代器类的函数
begin、end 、rbegin、rend
举例:
Begin指向第一个元素,黄色箭头。end是最后一个元素的后一个位置,黑色箭头。Begin和end一般一起使用,按正序输出list。rbegin指逆序的第一个元素,即最后一个元素,蓝色箭头。rend指逆序的最后一个元素的前一个位置,即第一个元素的前一个位置,红色箭头。
Rbegin和rend一般一起使用,用于逆序输出list。
三、list的容量相关的函数
1、empty
原型:bool empty ( ) const;
功能:判断lsit是否为空,即size是否为0
返回值:size为0,返回true,否则,返回false
2、size
原型:size_type size() const;
功能:返回lsit中元素的个数
返回值:size_type
3、Max_size
原型:size_type max_size () const;
功能:返回lsit的最大容量
返回值:
4、resize
原型:void resize ( size_type sz, T c = T());
功能:重新分配lsit的大小。如果sz小于目前的size就将多余的值删除;如果sz大于目前的size,就在增加容量,且用c填充。例如:
mylist.resize(5); //将size定为5
mylist.resize(8,100); //将size定为8,多出的用100填充
mylist.resize(12); //将size定为12
四、获取元素
1、front
原型: reference front ( );
const_reference front ( ) const;
功能:获取第一个元素
返回值:第一个元素的值
2、back
原型:reference back ( );
const_reference back ( ) const
功能:获取最后一个元素
返回值:最后一个元素
五、修改lsit的函数
1、assign
原型:void assign ( InputIterator first, InputIterator last );
void assign ( size_type n, const T& u)
功能:为list重新分配空间并赋值。将[first,last)范围内的值或者n次u值的拷贝赋给list
返回值:无
2、push_front:从头插入一个元素。pop_front:删除第一个元素
push_back:在尾部插入一个元素。 pop_back:删除最后一个元素
3、insert
原型:iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
void insert ( iterator position, InputIterator first, InputIterator last );
功能:插入元素
insert ( iterator position, const T& x ) :在position位置处插入元素x
insert ( iterator position, size_type n, const T& x ):在position位置处开始插入n个x
insert ( iterator position, InputIterator first, InputIterator last ):在position位置处开始插入
[first,last)范围内的元素。
返回值:只有第一个函数返回插入的元素所在位置
4、erase
原型:iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );
功能:清除链表中position 处或者[first,last)范围内的元素。会减少list的size值。
返回值:清除的最后一个元素的下一个位置(迭代器)
5、swap
原型:void swap ( list<T,Allocator>& lst)
功能:将两个lsit交换
6、clear
功能:清空list
六、操作类的函数
1、splice
原型:设list2调用了splice函数
void splice ( iterator position, list<T,Allocator>& x );将list x中的所有元素插入到调用该函数的list2的position处。List x会被清空。
void splice ( iterator position, list<T,Allocator>& x, iterator i );将x中指向i的位置处的元素插入到list2的position处。X会将i位置处的值删除。
void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last ); 将x中[first,last)位置处的元素插入到list2的position处。
功能:Move elements from list to list。将一个lsit中的值移动到另一个list
2、remove
原型:void remove ( const T& value );
功能:清除链表中特定的值value,lsit的size会相应减少。
返回值:无
3、remove_if
原型:template <class Predicate>
void remove_if ( Predicate pred );
功能:在满足Predicate pred返回true值时,移除元素。pred可以是一个返回bool类型的函数,还可以是一个重写operator函数的类。 例如:
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
class is_odd
{
public:
bool operator() (const int& value) {return (value%2)==1; }
};
返回值:无
4、unique
原型:void unique ( );
template <class BinaryPredicate>
void unique ( BinaryPredicate binary_pred );按照规则binary_pred消除重复值。例如:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }
// a binary predicate implemented as a class:
class is_near
{
public:
bool operator() (double first, double second)
{ return (fabs(first-second)<5.0); }
};
调用:mylist.unique (same_integral_part);
mylist.unique (is_near());
功能:消除list中的重复元素
返回值:
5、merge
原型:void merge ( list<T,Allocator>& x );
template <class Compare>
void merge ( list<T,Allocator>& x, Compare comp );
功能:合并两个已经有序(同时为升序或降序)的list。
merge()组合起两个排好序的表。如果一个表未排序,merge()仍然能产生出一个表,其中包含着原来两个表元素的并集。当然,对结果的排序就没有任何保证了。向splice()函数一样,merge()函数也不复制元素。
merge函数的作用是:将两个有序的序列合并为一个有序的序列。函数参数:merge(first1,last1,first2,last2,result,compare);//firs1t为第一个容器的首迭代器,last1为第一个容器的末迭代器,first2为第二个容器的首迭代器,last2为容器的末迭代器,result为存放结果的容器,comapre为比较函数(可略写,默认为合并为一个升序序列)。
返回值:
6、sort
原型:void sort ( );
template <class Compare>
void sort ( Compare comp );
功能:排序
返回值:
7、reverse
功能:将list中的元素逆置。
返回值:
C++ list容器系列功能函数详解的更多相关文章
- jQuery功能函数详解
jQuery通过$.browser对象获取浏览器信息. 属性 说明msie 如果是ie为true,否则为falsemozilla 如果是mozilla相关的浏览器为true,否则为falsesafar ...
- PHP输出缓存ob系列函数详解
PHP输出缓存ob系列函数详解 ob,输出缓冲区,是output buffering的简称,而不是output cache.ob用对了,是能对速度有一定的帮助,但是盲目的加上ob函数,只会增加CPU额 ...
- nginx高性能WEB服务器系列之四配置文件详解
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)
本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...
- STL之map与pair与unordered_map常用函数详解
STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...
- python基础之函数详解
Python基础之函数详解 目录 Python基础之函数详解 一.函数的定义 二.函数的调用 三.函数返回值 四.函数的参数 4.1 位置参数 4.2 关键字参数 实参:位置实参和关键字参数的混合使用 ...
- ThreeJS系列1_CinematicCameraJS插件详解
ThreeJS系列1_CinematicCameraJS插件详解 接着上篇 ThreeJS系列1_CinematicCameraJS插件介绍 看属性的来龙去脉 看方法作用 通过调整属性查看效果 总结 ...
- kzalloc 函数详解(转载)
用kzalloc申请内存的时候, 效果等同于先是用 kmalloc() 申请空间 , 然后用 memset() 来初始化 ,所有申请的元素都被初始化为 0. view plain /** * kzal ...
- memset函数详解
语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关 功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...
随机推荐
- php 连接数据库
连接数据库 <?php $con = mysql_connect("localhost","root","root"); if (!$ ...
- mssql 设置id自增 设置主键
主键自增长列在进行数据插入的时候,很有用的,如可以获取返回的自增ID值,接下来将介绍SQL Server如何设置主键自增长列,感兴趣的朋友可以了解下,希望本文对你有所帮助 1.新建一数据表,里 ...
- openfire数据库mysql配置
<?php return array( //'配置项'=>'配置值' //'USERNAME'=>'admin', //赋值 //数据库配置信息 'DB_TYPE' => 'm ...
- 解决App can’t be opened because it is from an unidentified developer
关闭设置 打开终端 输入sudo spctl --master-disable
- 【转】【Linux】linux awk命令详解
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- MindManager篇
MindManager:新建脑图 MindManager:大纲视图(批阅文档结构) MindManager:导出为其他格式 MindManager:插入基本插入主题.备注,标记等) MindManag ...
- erlang -- ios apns provider -- erlang 实现
os apns-apple notification server 与第三方provider的通信原理网上已有很多介绍,这里不再介绍,有想了解的大家可以去IOS官网https://developer. ...
- CentOS7设置开机自启动命令大全
任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.service 使某服务不自 ...
- [渗透技巧] Windows命令行下载
certutil简介 用于证书管理 支持环境: XP - Windows 10 全系统 更多:https://technet.microsoft.com/zh-cn/library/cc75534 ...
- iOS 图文混排 (Swift版)
// 0> 图片附件 let attachment = NSTextAttachment() attachment.image = #imageLiteral(resourceName: &qu ...