标准模板库STL中List使用参考手册
2012-10-05 21:29
537 查看
List 就是一双向链表,可高效地进行插入删除元素。包括构造、方法等。内存空间可以是不连续的,通过指针来进行数据的访问
优: 1)插入/删除效率高
缺: 1)不支持随机存取,查询效率较低
在使用list必须包括头文件#include <list>
构造:
成员函数:
中文列表:
使用:
1)、如何定义一个list对象
2)、使用list的成员函数push_back和push_front插入一个元素到list中
3)、使用list的成员函数empty()判断list是否为空
4)、用list< char >::iterator得到指向list的指针
说明:cList.Begin()和cList.end()函数返回指向list< char >::iterator的指针,由于list采用链表结构,因此它不支持随机存取,因此不能用cList.begin()+3来指向list中的第四个对象,vector和deque支持随机存取。
5)、用STL的通用算法count()来统计list中的元素个数
说明:在使用count()函数之前必须加入#include <algorithm>
6)、用STL的通用算法count_if ()来统计list中的元素个数
说明:count_if() 带一个函数对象的参数,函数对象是一个至少带有一个operator()方法的类函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会说的更清楚些。count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被记数。
7)、使用STL通用算法find()在list中查找对象
说明:如果没有找到指定的对象,就会返回cList.end()的值,找到了就返回一个指向对象iterator的指针。
8)、使用STL通用算法find_if()在list中查找对象
说明:如果没有找到指定的对象,就会返回cList.end()的值,找到了就返回一个指向对象iterator的指针。
9)、使用list的成员函数sort()排序
cList.sort();
10)、使用list的成员函数insert插入一个对象到list中
说明:insert()函数把一个或多个元素插入到指出的iterator位置。元素将出现在 iterator指出的位置以前。
11)、如何在list中删除元素
用list实现堆栈:
优: 1)插入/删除效率高
缺: 1)不支持随机存取,查询效率较低
在使用list必须包括头文件#include <list>
构造:
list<int> l1; //创建一个没有任何元素的list list<int> l2(10); //创建一个有10个元素的list,每个元素值为默认 list<double> l3(10, 9.3); //创建具有10个元素的list,每个元素的初始值为9.3 list<double> l4(l3); //通过拷贝一个list对象的元素,创建一个新的list对象 int iArray[] ={3, 10, 19}; list<int> l5(iArray, iArray + 3);//将另一个list对象的迭代器区间[first, last)所指的元素,拷贝到新创建的list对象中
成员函数:
assign | assign elements to a list |
back | returns a reference to last element of a list |
begin | returns an iterator to the beginning of the list |
clear | removes all elements from the list |
empty | true if the list has no elements |
end | returns an iterator just past the last element of a list |
erase | removes elements from a list |
front | returns a reference to the first element of a list |
insert | inserts elements into the list |
max_size | returns the maximum number of elements that the list can hold |
merge | merge two lists |
pop_back | removes the last element of a list |
pop_front | removes the first element of the list |
push_back | add an element to the end of the list |
push_front | add an element to the front of the list |
rbegin | returns a reverse_iterator to the end of the list |
remove | removes elements from a list |
remove_if | removes elements conditionally |
rend | returns a reverse_iterator to the beginning of the list |
resize | change the size of the list |
reverse | reverse the list |
size | returns the number of items in the list |
sort | sorts a list into ascending order |
splice | merge two lists in constant time |
swap | swap the contents of this list with another |
unique | removes consecutive duplicate elements |
assign() //分配值,有两个重载: c1.assign(++c2.begin(), c2.end()) //c1现在为(50,60)。 c1.assign(7,4) //c1中现在为7个4,c1(4,4,4,4,4,4,4)。 back() //返回最后一元素的引用: begin() //返回第一个元素的指针(iterator) clear() //删除所有元素 empty() //判断是否链表为空 end() //返回最后一个元素的下一位置的指针(list为空时end()=begin()) erase() //删除一个元素或一个区域的元素(两个重载) front() //返回第一个元素的引用: insert() //在指定位置插入一个或多个元素(三个重载): max_size() //返回链表最大可能长度(size_type就是int型): merge() //合并两个链表并使之默认升序(也可改): pop_back() //删除链表尾的一个元素 pop_front() //删除链表头的一元素 push_back() //增加一元素到链表尾 push_front() //增加一元素到链表头 rbegin() //返回链表最后一元素的后向指针(reverse_iterator or const) rend() //返回链表第一元素的下一位置的后向指针 remove() //删除链表中匹配值的元素(匹配元素全部删除) remove_if() //删除条件满足的元素(会遍历一遍链表) resize() //重新定义链表长度(两重载): reverse() //反转链表: size() //返回链表中元素个数 sort() //对链表排序,默认升序(可自定义) splice() //对两个链表进行结合(三个重载) swap() //交换两个链表(两个重载) unique() //删除相邻重复元素(断言已经排序,因为它不会删除不相邻的相同元素)
使用:
1)、如何定义一个list对象
#include <list> int main (void) { list<char > cList; //声明了list<char>模板类 的一个实例 }
2)、使用list的成员函数push_back和push_front插入一个元素到list中
cList. push_back('a'); //把一个对象放到一个list的后面 cList. push_front ('b'); //把一个对象放到一个list的前面
3)、使用list的成员函数empty()判断list是否为空
if (cList.empty()) { printf("this list is empty"); }
4)、用list< char >::iterator得到指向list的指针
list< char>::iterator charIterator; for(cIterator = cList.Begin();cIterator != cList.end();cIterator++) { printf("%c", *cIterator); } //输出list中的所有对象
说明:cList.Begin()和cList.end()函数返回指向list< char >::iterator的指针,由于list采用链表结构,因此它不支持随机存取,因此不能用cList.begin()+3来指向list中的第四个对象,vector和deque支持随机存取。
5)、用STL的通用算法count()来统计list中的元素个数
int cNum; char ch = 'b'; cNum = count(cList.Begin(), cList.end(), ch); //统计list中的字符b的个数
说明:在使用count()函数之前必须加入#include <algorithm>
6)、用STL的通用算法count_if ()来统计list中的元素个数
const char c('c'); class IsC { public: bool operator() ( char& ch ) { return ch== c; } }; int numC; numC = count_if (cList.begin(), cList.end(),IsC());//统计c的数量;
说明:count_if() 带一个函数对象的参数,函数对象是一个至少带有一个operator()方法的类函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会说的更清楚些。count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被记数。
7)、使用STL通用算法find()在list中查找对象
list<char >::iterator FindIterator; FindIterator = find(cList.begin(), cList.end(), 'c'); If (FindIterator == cList.end()) { printf("not find the char 'c'!"); } else { printf("%c", * FindIterator); }
说明:如果没有找到指定的对象,就会返回cList.end()的值,找到了就返回一个指向对象iterator的指针。
8)、使用STL通用算法find_if()在list中查找对象
const char c('c'); class IsC { public: bool operator() ( char& ch ) { return ch== c; } }; list<char>::iterator FindIterator FindIterator = find_if (cList.begin(), cList.end(),IsC());//查找字符串c;
说明:如果没有找到指定的对象,就会返回cList.end()的值,找到了就返回一个指向对象iterator的指针。
9)、使用list的成员函数sort()排序
cList.sort();
10)、使用list的成员函数insert插入一个对象到list中
cList.insert(cLiset.end, 'c'); ///在list末尾插入字符'c' char ch[3] ={'a', 'b', 'c'}; cList.insert(cList.end, &ch[0], & ch[3] ); //插入三个字符到list中
说明:insert()函数把一个或多个元素插入到指出的iterator位置。元素将出现在 iterator指出的位置以前。
11)、如何在list中删除元素
cList.pop_front(); //删除第一个元素 cList.pop_back(); //删除最后一个元素 cList. Erase(cList.begin()); //使用iterator删除第一个元素; cList. Erase(cList.begin(), cList.End()); //使用iterator删除所有元素; cList.remove('c'); //使用remove函数删除指定的对象; list<char>::iterator newEnd; //删除所有的'c' ,并返回指向新的list的结尾的iterator newEnd = cList.remove(cList.begin(), cList.end(), 'c');
用list实现堆栈:
#ifndef LL_STACK #define LL_STACK #include <list> template<class T> class LLStack { public: LLStack()//构造函数 { } void clear()//清空栈 { lst.clear();//清空容器 } bool isEmpty() const//判断栈是否为空 { return lst.empty();//容器是否为空 } T& topEl()//取栈顶元素 { return lst.back();//取容器最后一个元素 } T pop()//出栈 { T el = lst.back();//取容器最后一个元素 lst.pop_back();//删除最后一个元素 return el; } void push(const T& el)//进栈 { lst.push_back(el);//在容器的最末尾增加一个元素 } private: list<T> lst; }; #endif
相关文章推荐
- STL 标准模板库 vector queue list map multimap的使用基础
- 标准模板库STL中Stack参考手册
- 标准模板库STL中Queue参考手册
- 标准模板库STL中优先队列Priority Queues使用手册
- 【c++——STL】标准模板库STL中优先队列Priority Queues使用手册
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
- 标准模板库使用举例(STL - (LIST,VECTOR))
- C++ 标准模板库STL 双向链表 list 使用方法与应用介绍(一)
- C++ 标准模板库STL 双向链表 list 使用方法与应用介绍(一)
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
- C++标准模板库(STL)学习精要,看此文再配合参考手册轻松成高手!
- STL容器使用DEMO-list
- C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用
- 使用STL通用算法find_if()在list中搜索对象
- C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用
- 【语言-C++】STL std::list的使用
- STL中list,vector,deque,map,set区别、联系和使用场景
- STL 中 list 的使用
- C++中STL STD::list使用说明
- STL之list使用简介