Essential C++(3)泛型编程
来源:互联网 发布:宛然如生,不亦善乎翻译 编辑:程序博客网 时间:2024/06/03 17:18
STL:
容器:vector , list , set, map……
算法:find , sort , replace , merge……
假设给定一个存储整数的vector,以及一个整数值,如果此值存在于vector内,
我们必须返回一个指针指向该值,反之返回0,表示此值并不在vector内,以下
便是find.1.0
int* find (const vector<int> &vec, int value){ for (int ix = 0;ix < vec.size(); ++ix) if (vec[ ix ] == value) return &vec[ix]; return 0;}
find.2.0 想办法让这个函数不仅可以处理整数,也可以处理任何型别(该型别有
equality(相等)运算符)
template <typename elemType>elemType* find(const vector<elemType> &vec, const elemType &value ){ for (int ix = 0; ix < vec.size(); ++ix) if (vec[ ix ] == value) return &vec[ix]; return 0;}
find.3.0让这个函数可以同时处理vector与array内的任意类型元素
//增加一个参数,用来表示array的终点template <typename elemType>elemType* find( elemType *first ,const elemType *last, const elemType &value){//不可以写 const elemType* first,因为下面回事const elemType*返回elemType*//书中这里有错//也可以将返回值改为const elemType* if (! first || ! last) return 0; for (; first !=last; ++first ) if (*first == value ) return first; return 0;}
//处理vector,vector可以为空,array不可以,所以我们要先判断vector是否为零template <typename elemType>inline const elemType* begin(const vector<elemType> &vec){ return vec.empty() ? : 0 :&vec[0]; }//通常将取地址这种操作封装为函数,这样子,就可以同时处理vector和array了find (begin( svec),end (svec),search_value );
泛型指针(iterator)
使用const_iterator来遍历操作,允许我们读取vector元素,但不允许任何写入
vector<string>::const_iterator iter = ce_vec.begin();
其他用法基本等同于普通指针
exp
//重新设计find(),使其可以支持指针与某种容器的iteratorstemplate <typename IteratorType, typename elemType >IteratorTypefind( IteratorType first,IteratorType last, const elemType &value ){ for (; first != last; ++first) if (value == *first) return first; return last;}
继续努力的方向是将现有的find()版本演化为泛型算法。
标准库中有超过60个泛型算法,以下列出一部分
搜索算法:find( ),count( ),adjacent_find( ),find_if( ),count_if(),binary_search(),find_first_of
排序:
复制,删除,替换
关系
生成,质变
数值
集合
所有容器的共通操作
==!==empty()size()clear()begin(),end()insert(),erase()
序列式容器
vector
list
deque:前端和末端元素的安插删除效率最高
push_back( ):在末尾安插一个元素
pop_back( ):在末尾删除一个元素
list和deque还提供 push_front,pop_front()
读取前端和末端的元素值需要front()和back()
四种 insert函数变形
iterator insert(iterator position,elemType value )
将value插在pos前void insert( iterator position,int count,elemType value)
在pos前插入count个valuevoid insert(iterator1 position,iterator2 first,iterator2 last)
在pos前插入first和last的所有元素iterator insert(iterator position )
可在position之前插入元素,初值为起类型的默认值
两种erase( )函数的变形
iterator erase (iterator posit)
可以抹除posit所指元素iterator erase (iterator first ,iterator last)
可以抹除first,last范围内的元素,first是第一个被删除的,last是最后一个被删除的下一位
- Essential C++(3)泛型编程
- 《Essential C++》泛型编程
- Essential c++,泛型编程风格读书笔记
- 《Essential C++》笔记三(2)、泛型编程风格
- 《Essential C++》笔记一、C++编程基础
- Essential C++学习笔记-----第三章 泛型编程
- Essential C++学习第三章 泛型编程风格 STL
- 《Essential C++》笔记三(1)、使用泛型算法
- 《Essential C++》笔记二、面向过程的编程风格
- 《Essential C++》笔记五、基于对象的编程风格
- 【essential c++】模板函数编程练习2_5
- 《Essential C++》泛型之 模板函数 Template Functions 与 重载函数 Overloaded Functions
- 快速阅读《Essential C++》
- Essential C++
- 重读《Essential C++》心得
- 垃圾《Essential C++》
- 《Essential C++》笔记1
- 读《essential c++》有感
- c++中的浅拷贝和深拷贝问题
- linux ----- head
- 转Linux查找文件(find命令)
- swift函数
- java的动态代理机制详解
- Essential C++(3)泛型编程
- HTML<meta>标签
- Cisco IOS 特性集
- hbuilder autoprefixer配置
- andorid的不常见异常以及原因(持续更新中)
- Java :计算机基础设施及原理
- 将普通sql查询的将结果集转换指定的对象,然后对象存在list中并返回
- ctype.h头文件中的tolower和toupper以及cctype其他函数的应用
- 排座位(并查集)