c++ stl library 学习(7)

来源:互联网 发布:nba体测数据之最 编辑:程序博客网 时间:2024/06/05 01:59

Algorithms:searching, sorting, copying,reordering, modifying, and numeric processing.

Algorithms are not member functions of the container classes. Instead, they are global functions
that operate with iterators. This has an important advantage: Instead of each algorithm being
implemented for each container type, all are implemented only once for any container type. The
algorithm might even operate on elements of different container types. You can also use the
algorithms for user-defined container types. All in all, this concept reduces the amount of code
and increases the power and the flexibility of the library.//算法为全局函数,与迭代器一起配合使用。不是容器的成员函数。


迭代器只不过是容器中某个位置的抽象而已、、、


iterater pos;

pos = min_element(first,last)

pos = max_element(first,last)

sort(first,last);

pos = find( first, last, element)

reverse(first,last); //反转这个范围内的元素

copy(first, last, otherFirst);

unique_copy(first, last, otherFirst);


Iterator Adapters:

1. Insert iterators
2. Stream iterators
3. Reverse iterators


Insert Iterators:

back_inserter (container) Appends in the same order by using push_back()
front_inserter (container) Inserts at the front in reverse order by using push_front()
inserter (container ,pos) Inserts at pos (in the same order) by using insert()


Stream Iterators
Another very helpful kind of iterator adapter is a stream iterator. Stream iterators are iterators that
read from and write to a stream. [8] Thus, they provide an abstraction that lets the input from the
keyboard behave as a collection, from which you can read. Similarly you can redirect the output
of an algorithm directly into a file or onto the screen.


Reverse Iterators:

Reverse iterators operate in reverse. They switch the call of an increment operator internally into a call of the decrement
operator, and vice versa. All containers can create reverse iterators via their member functions rbegin() and rend()


vector<int>:: reverse_iterator viter;
viter = v.rend();
for (--viter; viter!=v.rbegin(); --viter)
{
cout<<*viter<<' ';
}


Manipulating Algorithms://可能会删除、重排、修改元素的算法remove(),resort(),modify()

Several algorithms modify destination ranges. In particular, they may remove elements.


eg:

remove();返回的为新的终点的iterator

distance(iterator1,iterator2)返回两个迭代器之间的距离。


Manipulating Algorithms and Associative Containers:

Manipulating Algorithms 不能和 关联容器很好的结合,而且还会产生错误。eg,remove()会改变容器中元素的顺序,但是关联容器的顺序不允许在既定的比较规则下改变。。。。关联容器必须有序。


Algorithms   VS Member Functions

成员函数永远被推荐优先使用,但是如果你想实现只改变容器的类型就将代码完全移植的话,使用全局的算法会让工作变的更简单一些,只是会付出一些性能上的代价。


用户定义的函数:

#include <iostream>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}

原创粉丝点击