C++容器之list
来源:互联网 发布:ubuntu 麒麟下载 编辑:程序博客网 时间:2024/06/17 13:07
简介
List是一种可在常数时间内在任何位置执行插入和删除操作的顺序容器。list是双向链表,其迭代器是双向的。与其他顺序容器(array, vector, deque)相比,list容器在任意位置执行插入、提取、和移动元素的操作更高效,但它不能通过在容器中的位置直接获取元素。
成员函数
复制控制
list::list()
构造函数:构造一个新的list对象,根据参数初始化list容器的内容。
list::~list()
析构函数:销毁以list对象。
list::operator=
为容器分配新的内容,代替当前的内容,随之修改其大小。
示例代码
#include<iostream>#include<list> using namespace std; voidprint(list<int> l){ for(list<int>::iterator it = l.begin(); it != l.end(); ++it) { cout << *it << ""; } cout << endl;} intmain(void){ // list::list() list<int> first; list<int> second(5, 10); list<int> third(second.begin(), second.end()); list<int> fourth(third); int arr[] = {1, 2, 3, 4, 5}; list<int> fifth(arr, arr + sizeof(arr)/sizeof(int)); print(second); print(third); print(fourth); print(fifth); // list::operator= first = fifth; print(first); return(0);}
Iterator
list::begin()
返回一个迭代器,指向list的第一个元素。返回值类型:iterator/const_iterator。
list::end()
返回一个迭代器,指向list的最后一个元素的下一个位置。返回值类型:iterator/const_iterator。
list::rbegin()
返回一个反转迭代器,指向list的最后一个元素。返回值类型:reverse_iterator/reverse_const_iterator。
list::rend()
返回一个反转迭代器,指向list的第一个元素的前一个位置。返回值类型:reverse_iterator/reverse_const_iterator。
list::cbegin()
begin()的const版本。
list::cend()
end()的const版本
list::crbegin()
rbegin()的cosnt版本。
list::crend()
rend()的const版本。
示例代码
#include <iostream>#include <list>using namespace std;intmain(void){int arr[] = {1, 2, 3, 4, 5};list<int> l(arr, arr + sizeof(arr)/sizeof(int));// list::begin endfor(list<int>::iterator it = l.begin(); it != l.end(); ++it){cout << *it << " ";}cout << endl;// list::rbegin rendfor(list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it){cout << *it << " ";}cout << endl;// list::cbegin cendfor(list<int>::const_iterator it = l.cbegin(); it != l.cend(); ++it){cout << *it << " ";}cout << endl;// list::cbegin cendfor(list<int>::const_reverse_iterator it = l.crbegin(); it != l.crend(); ++it){cout << *it << " ";}cout << endl;return 0;}
Capacity
list::empty()
测试list是否为空,空返回true,否则返回false。
list::size()
返回list中元素个数。返回值类型list::size_type。
list::max_size()
返回list能够容纳元素的最大个数。返回值类型list::size_type。
示例代码
#include<iostream>#include<list> using namespace std; intmain(void){ list<int> l(10, 20); // list::empty if(l.empty()){ cout << "Listis empty" << endl; }else{ cout << "Listis not empty" << endl; } cout << "Listsize : " << l.size() << endl; cout << "Listmax size : " << l.max_size() << endl; return(0);}
Element access
list::front()
返回list第一个元素的引用。返回值类型reference/const_reference。
list::back()
返回list最后一个元素的引用。返回值类型reference/const_reference。
示例代码
#include<iostream>#include<list> using namespace std; intmain(void){ int arr[] = {1, 2, 3, 4, 5}; list<int> l(arr, arr + sizeof(arr)/sizeof(int)); // list::front cout << "Thefirst element is : " << l.front() << endl; // list::back cout << "Thesecond element is : " << l.back() << endl; return(0);}
Modifiers
list::assign()
为list分配新的内容,以代替当前内容,并随之改变其大小。
list::emplace_front()
在list的前端插入一个元素。插入的元素由其构造函数创建。
list::push_front()
在list的前端插入一个元素。与emplace_front()不同,它的插入方式是将一个已存在的元素复制或移动到list前端。
list::pop_front()
删除list的第一个元素。
list::emplace_back()
在list的末端插入一个元素。插入的元素由其构造函数创建。
list::push_back()
在list的末端插入一个元素。与emplace_back()不同,它的插入方式是将一个已存在的元素复制或移动到list末端。
list::pop_back()
删除list的最后一个元素。
list::emplace()
在指定位置插入一个元素。插入的元素由其构造函数创建。
list::insert()
在指定位置插入一个或多个元素。对于插入大量元素来说是非常高效的。
list::erase()
从list中删除指定位置的一个或一定范围的元素。
list::swap()
交换两个list中的元素。两个list类型必须相同,大小可以不同。
list::resize()
重新分配list的大小,使其能容纳指定数量的元素。
list::clear
删除list中的所有元素。
示例代码
#include<iostream>#include<list>#include<vector> using namespace std; voidprint(list<int> l){ for(list<int>::iterator it = l.begin(); it != l.end(); ++it) { cout << *it << ""; } cout << endl;} intmain(void){ list<int> first; list<int> second; list<int> third; int arr[] = {1, 2, 3, 4, 5}; // list::assign first.assign(5, 10); second.assign(first.begin(), first.end()); third.assign(arr, arr + sizeof(arr)/sizeof(int)); print(first); print(second); print(third); // list::emplace emplace_front emplace_back list<pair<int, char>> pl; pl.emplace_front(1, 'a'); pl.emplace_front(2, 'b'); pl.emplace_back(3, 'c'); pl.emplace(pl.end(), 4, 'd'); for(pair<int, char> x :pl) { cout<< x.first << "" << x.second << endl; } //list::push_front push_back third.push_front(10); third.push_back(20); print(third); // list::insert first.insert(first.begin(), 20); first.insert(first.end(), 2, 30); vector<int> vec(2, 40); first.insert(first.end(), vec.begin(), vec.end()); print(first); // list::erase second.erase(second.begin()); print(second); second.erase(++second.begin(), second.end()); print(second); // list::swap second.swap(first); print(first); print(second); // list::resize third.resize(5); print(third); third.resize(10); print(third); third.resize(15, 100); print(third); // list::clear third.clear(); cout << "Thesize of third is : " << third.size() << endl; return(0);}
Operations
list::splice()
将一个list A中的元素转移到list B的指定位置,并将A中被转移的元素删除。
list::remove()
将list中指定的元素删除。
list::remove_if()
根据判断条件删除list的元素,如果条件为真则删除该元素。
list::unique()
删除list中具有相同值的元素,只保留第一个。也可以根据条件删除具有相同条件的元素,只保留第一个。
list::merge()
合并两个list,在合并之前两个list应该先排序,合并之后的list依然有序。也可以自定义排序的条件。
list::sort()
对list中的元素进行排序,变更它们在容器中的位置。sort()还可以按给定条件进行排序。
list::reverse()
改变list中元素的顺序。
示例程序
#include<iostream>#include<cmath>#include<list> using namespace std; voidprint(list<int> l){ for(list<int>::iterator it = l.begin(); it != l.end(); ++it) { cout << *it << ""; } cout << endl;} boolsingle_dight(const int &val){ return val > 10;} boolis_near(int first, int second){ return(fabs(first - second) < 5);} boolreverse(int first, int second){ return((first -second) > 0);} intmain(void){ list<int> first(2, 10); list<int> second(2, 20); list<int>::iteratorit; // list::splice it = first.begin(); first.splice(it, second); print(first); if(second.empty()) { cout << "Secondis empty" << endl; } else { cout << "Secondis not empty" << endl; } // it still point to 10(the 3th element) cout << *it << endl; second.splice(second.begin(), first, it); print(second); it = first.begin(); advance(it, 2); print(first); first.splice(first.begin(), first, it, first.end()); print(first); // list::remove cout << "Beforeremove : "; print(first); first.remove(10); cout << "Afterremove : "; print(first); // list::remove_if cout << "Beforeremove_if : "; first.push_back(10); print(first); first.remove_if(single_dight); cout << "Afterremove_if : "; print(first); // list::unique first.push_back(20); first.push_back(20); first.push_back(21); cout << "Beforecall unique() : "; print(first); first.unique(); cout << "Aftercall unique() : "; print(first); cout << "Beforecall unique() : "; print(first); first.unique(is_near); cout << "Aftercall unique() : "; print(first); // list::merge first.push_back(5); first.push_back(12); first.sort(); print(first); second.push_back(9); second.push_back(17); second.sort(); print(second); first.merge(second); print(first); // list::sort first.sort(reverse); print(first); // list::reverse first.reverse(); print(first); return(0);}
- C++STL之list容器
- C++STL之list双向链表容器
- 【C++】STL常用容器总结之四:链表list
- c++STL之顺序容器(vector、deque、list)分析
- STL容器之list
- STL容器之list
- 顺序容器之list
- STL容器之list
- C++容器之list
- STL容器之list
- JAVA容器之List
- STL容器之list
- STL之list容器
- C++之List容器
- 顺序容器之list
- STL容器之list
- STL容器 之 list
- STL容器之list
- abstract class 和Interface有什么区别?
- 用vs2012编译doxygen-1.8.7
- iOS自动化测试相关
- 通过经纬度计算距离
- 动手学Android之十一——界面太丑我抗议
- C++容器之list
- 批量替换Excel中空单元格或者别的单元格
- Windows 下安装NPM
- 怎么查看自己电脑主板是否集成显卡
- WebRTC学习笔记
- ZOJ-3777-Problem Arrangement
- 程序员的11个阶段
- time_base of ffmpeg transcode
- 怎样在Word 2010文档中选择纵向或横向