# PS，1880后程序员

## 11.2 初窥算法

#include <algorithm>

#include <numeric>

### 只读算法

#### accumulate

 int sum = accumulate(vec.begin(), vec.end(), 42);

 string sum = accumulate(v.begin(), v.end(), string(""));

#### find_first_of

 size_t cnt = 0; list::iterator it = roster1.begin(); // look in roster1 for any name also in roster2 while   ((it = find_first_of(it, roster1.end(),              roster2.begin(), roster2.end()))                 != roster1.end()) {    ++cnt;    // we got a match, increment it to look in the rest of roster1    ++it; }

roster1listroster2可以是dequevector，只要我们可以用==操作比较这两个序列中的元素。尤其是：如果 roster1 list<string> 对象，则 roster2 可以是 vector<char*> 对象，因为 string 标准库为 string 对象与 char* 对象定义了相等（==）操作符。

### 写容器算法

 vector vec; // empty vector // disaster: attempts to write to 10 (nonexistent) elements in vec fill_n(vec.begin(), 10, 0);

back_inserter

unique

sort

stable_sort

## 再次解读迭代器Iterators

insert iterators插入迭代器

iostream iterators

reverse iterators反向迭代器

### insert iterators

inserter是迭代器适配器，它和容器绑定，产生一个迭代器，这个迭代器向绑定的容器插入元素。当我们通过inserter迭代器赋值时，迭代器插入新的元素。（An inserter is an iterator adaptor that takes a container and yields an iterator that inserts elements into the specified container. When we assign through an insert iterator, the iterator inserts a new element.

• back_inserter：使用push_back创建一个迭代器
• front_inserter: 使用push_front创建一个迭代器
• inserter：在指定的位置后创建迭代器

 list ilst, ilst2, ilst3;     // empty lists // after this loop ilst contains: 3 2 1 0 for (list::size_type i = 0; i != 4; ++i)      ilst.push_front(i); // after copy ilst2 contains: 0 1 2 3 copy (ilst.begin(), ilst.end(), front_inserter(ilst2)); // after copy, ilst3 contains: 3 2 1 0 copy (ilst.begin(), ilst.end(), inserter (ilst3, ilst3.begin()));

### iostream iterators

istream_iterator:input stream

ostream_iterator:output stream

#### 定义流迭代器stream Iterators

 istream_iterator cin_it(cin);    // reads ints1 from cin istream_iterator end_of_stream; // end iterator value while(cin_it != end_of_stream) {    //do something } // writes Sales_items from the ofstream named outfile // each element is followed by a space ofstream outfile; ostream_iterator output(outfile, " ");

#### istream_iterator上的操作

++it & it++

 // write one string per line to the standard output ostream_iterator out_iter(cout, ""n"); // read strings from standard input and the end iterator istream_iterator in_iter(cin), eof; // read until eof and write what was read to the standard output while (in_iter != eof)     // write value of in_iter to standard output     // and then increment the iterator to get the next value from cin    *out_iter++ = *in_iter++;

#### 流迭代器的限制

1.      不能从ostream_iterator中读入数据，同时也不能向istream_iterator写入数据（It is not possible to read from an ostream_iterator, and it is not possible to write to an istream_iterator.

2.      一旦给ostream_iterator赋值，这个写入的操作就提交了。一旦赋值，在后续的操作中就不能修改。此外，每个ostream_iterator只能用作输出一次。（Once we assign a value to an ostream_iterator, the write is committed. There is no way to subsequently change a value once it is assigned. Moreover, each distinct value of an ostream_iterator is expected to be used for output exactly once.

3.      ostream_iterator里没有->操作。

#### 基于流迭代器使用算法

 istream_iterator cin_it(cin);    // reads ints from cin istream_iterator end_of_stream; // end iterator value // initialize vec from the standard input: vector vec(cin_it, end_of_stream); sort(vec.begin(), vec.end()); // writes ints to cout using " " as the delimiter ostream_iterator output(cout, " "); // write only the unique elements in vec to the standard output unique_copy(vec.begin(), vec.end(), output);

### reverse iterators

#### 利用迭代器处理string

 // find first element in a comma-separated list string::iterator comma = find(line.begin(), line.end(), ','); cout << string(line.begin(), comma) << endl;

 // find last element in a comma-separated list string::reverse_iterator rcomma = find(line.rbegin(), line.rend(), ','); // wrong: will generate the word in reverse order cout << string(rcomma.base(), line.end()) << endl;

Nothing to say

## 容器特有算法

posted on 2009-06-04 11:22 amenglai 阅读(475) 评论(1)  编辑  收藏 所属分类: C++ Primer 之 读书笔记

## #re: C++ Primer 之 读书笔记 第十一章回复更多评论

back_inserter

2009-06-28 17:11 | zwyd34@sina.com

 只有注册用户登录后才能发表评论。 网站导航: 相关文章: