C++primer U11 读书笔记 泛型算法
2014-05-27 11:27
225 查看
#include <algorithm>
vector<int> vec; // empty vector
// ok: back_inserter creates an insert iterator that adds elements to vec
fill_n (back_inserter(vec), 10, 0); // appends 10 elements to vec ,fill_n本身不会增加容器大小,back_inserter会调用push_back
删除重复元素
// sort words alphabetically so we can find the duplicates
sort(words.begin(), words.end());
/* eliminate duplicate words:
* unique reorders words so that each word appears once in the
* front portion of words and returns an iterator one past the
unique range;
* erase uses a vector operation to remove the nonunique elements
*/
vector<string>::iterator end_unique =
unique(words.begin(), words.end());
words.erase(end_unique, words.end());
插入迭代器
• back_inserter,创建使用 push_back 实现插入的迭代器。
• front_inserter,使用 push_front 实现插入。
• inserter,使用 insert 实现插入操作。除了所关联的容器外,inserter还带有第二实参:指向插入起始位置的迭代器。
iostream 迭代器
istream_iterator<T> in(strm); 创建从输入流 strm 中读取 T 类型对象的istream_iterator 对象
istream_iterator<T>in; istream_iterator 对象的超出末端迭代器
ostream_iterator<T> in(strm); 创建将 T 类型的对象写到输出流 strm 的ostream_iterator 对象
ostream_iterator<T> in(strm, delim);创建将 T 类型的对象写到输出流 strm 的ostream_iterator 对象,在写入过程中使用 delim作为元素的分隔符。delim 是以空字符结束的字符数组
流迭代器只定义了最基本的迭代器操作:自增、解引用和赋值。此外,可比较两个 istream 迭代器是否相等(或不等)。而 ostream 迭代器则不提供比较运算
算法的形参模式
alg (beg, end, other parms);
alg (beg, end, dest, other parms);
alg (beg, end, beg2, other parms);
alg (beg, end, beg2, end2, other parms);
vector<int> vec; // empty vector
// ok: back_inserter creates an insert iterator that adds elements to vec
fill_n (back_inserter(vec), 10, 0); // appends 10 elements to vec ,fill_n本身不会增加容器大小,back_inserter会调用push_back
删除重复元素
// sort words alphabetically so we can find the duplicates
sort(words.begin(), words.end());
/* eliminate duplicate words:
* unique reorders words so that each word appears once in the
* front portion of words and returns an iterator one past the
unique range;
* erase uses a vector operation to remove the nonunique elements
*/
vector<string>::iterator end_unique =
unique(words.begin(), words.end());
words.erase(end_unique, words.end());
插入迭代器
• back_inserter,创建使用 push_back 实现插入的迭代器。
• front_inserter,使用 push_front 实现插入。
• inserter,使用 insert 实现插入操作。除了所关联的容器外,inserter还带有第二实参:指向插入起始位置的迭代器。
iostream 迭代器
istream_iterator<T> in(strm); 创建从输入流 strm 中读取 T 类型对象的istream_iterator 对象
istream_iterator<T>in; istream_iterator 对象的超出末端迭代器
ostream_iterator<T> in(strm); 创建将 T 类型的对象写到输出流 strm 的ostream_iterator 对象
ostream_iterator<T> in(strm, delim);创建将 T 类型的对象写到输出流 strm 的ostream_iterator 对象,在写入过程中使用 delim作为元素的分隔符。delim 是以空字符结束的字符数组
流迭代器只定义了最基本的迭代器操作:自增、解引用和赋值。此外,可比较两个 istream 迭代器是否相等(或不等)。而 ostream 迭代器则不提供比较运算
算法的形参模式
alg (beg, end, other parms);
alg (beg, end, dest, other parms);
alg (beg, end, beg2, other parms);
alg (beg, end, beg2, end2, other parms);
相关文章推荐
- C++ Primer 读书笔记 Charpter 11 泛型算法
- C++ Primer 读书笔记:第11章 泛型算法
- c++ primer 读书笔记之第十章 泛型算法
- 读书笔记:C++ primer 5th edition--chapter10.泛型算法
- 《C++Primer》读书笔记——第10章 泛型算法
- final关键字的使用(Thinking In Java 读书笔记)
- 《.NET4.0 面向对象编程漫谈》读书笔记 ——第14章 对象间的协作与信息交换
- 《重构》读书笔记(十)——第十章 简化函数调用
- 《你的灯亮着吗》读书笔记3
- 《GPU编程与CG语言之阳春白雪下里巴人》 读书笔记3 漫反射与Lambert光照模型
- 读书笔记——深入理解java虚拟机
- 重构(Refactoring)技巧读书笔记 之一
- 【面向对象分析与设计】读书笔记3-Classification
- 创建Win32应用程序步骤――《VC++深入详解》读书笔记
- 【c++ primer读书笔记】【第10章】泛型算法
- 《把时间当作朋友》读书笔记
- 【读书笔记】TCP/IP网络编程 第4章 回声
- 读书笔记 (3) (转^漂泊的猪^)
- linux 读书笔记
- 《重构》读书笔记(十二)——第十二章 大型重构