一步一步学STL标准模板库
来源:互联网 发布:java读取parquet文件 编辑:程序博客网 时间:2024/05/12 17:02
list 的使用
在使用list必须包括头文件#include <list>,
1)、如何定义一个list对象
5)、用STL的通用算法count()来统计list中的元素个数
6)、用STL的通用算法count_if ()来统计list中的元素个数
7)、使用STL通用算法find()在list中查找对象
8)、使用STL通用算法find_if()在list中查找对象
9)、使用list的成员函数sort()排序
11)、如何在list中删除元素
在使用list必须包括头文件#include <list>,
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 c{public:bool operator() ( char& ch ){return ch== c;}};list<char>::iterator FindIteratorFindIterator = 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的结尾的iteratornewEnd = cList.remove(cList.begin(), cList.end(), ‘c’);
- 一步一步学STL标准模板库
- 一步一步学STL标准模板库
- 一步一步学STL标准模板库
- 一步一步学STL标准模板库
- 一步一步学STL标准模板库
- [转]一步一步学习STL标准模板库
- 一步一步学习STL标准模板库
- 标准模板库(STL)
- STL标准模板库
- STL标准模板库
- STL标准模板库
- STL标准模板库
- STL 标准模板库
- 标准模板库 (STL)
- STL标准模板库
- 标准模板库-STL
- STL 标准模板库
- 标准模板库STL
- [软件升级]简单图片浏览器 v1.21
- 关于dos启动的一些知识
- 霍元甲是被谁毒死?
- 自勉1
- Unix命令执行顺序
- 一步一步学STL标准模板库
- 工作中常用的一些javascript的小技巧
- 企业信息化大路子是不是搞反了?
- 2005年显卡盘点
- 奋斗十年(南下打工仔的创业回忆)
- 工作中常用的一些javascript小技巧(二)
- 使用DirectShow驱动摄像头
- 工作中常用的一些javascript小技巧(三)
- Liner search vs. Binary search in Array