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在linux上执行外部命令,system(),exec(),shell_exec()
目录:一.PHP中调用外部命令介绍二.关于安全问题三.关于超时问题四.关于PHP运行linux环境中命令出现的问题 一.PHP中调用外部命令介绍 在PHP中调用外部命令,有三种方法: 1. 调用专门函 ...
- 【Java面试题】25 同步和异步有何异同,在什么情况下分别使用他们?举例说明。
如果数据将在线程间共享.例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据就是共享数据,必须进行同步存取. 当应用程序在对象上调用了一个需要花费很长时间 ...
- js中数组作为参数传递的定义
下面的函数实现了一个我们想要的最基本的图片预加载效果 function preloadimages(arr){ var newimages=[] var arr=(typeof arr!= ...
- javascript测试框架 Mocha 实例教程
http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html
- ubuntu网页无法看视频
sudo apt-get install flashplugin-nonfree sudo apt-get install aptitude sudo aptitude install ubuntu- ...
- oracle 触发器 pragma autonomous_transaction
from:http://blog.csdn.net/ruru7989/article/details/30712987一般情况下在触发器中是不能使用DDL语句的,使用自治事务可以实现 可以在触发器中加 ...
- Strut2------源码下载
转载: http://download.csdn.net/detail/dingkui/6858009
- Python 使用正则表达式匹配URL网址
使用正则表达式匹配以 .com 或 .cn 为域名后缀的URL地址 In [1]: import re In [2]: str = "http://www.baidu.com/" ...
- Qt监控后台服务运行状态
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMa ...
- php遍历文件夹下的所有文件及文件夹
//第一种 遍历放入数据中 function my_scandir($dir) { $files = array(); if ( $handle = opendir($dir) ) { while ( ...