Effective STL: 4, iterators

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_30534935/article/details/102764825

At first glance, the concept seems to have STL iterators very simple, but take a closer look, you'll notice, STL standard containers actually provides four different iterator types: iterator, const_iterator, reverse_iterator and const_reverse_iterator . You will further notice, for a particular form of insert and erase functions, only one of four types of the iterator is acceptable container. The question is: Why do we need four different iterator it? What is the relationship between them? Are they interchangeable? Can you mix different types of iterators in STL algorithms and other tools that function? These iterations between the respective vessel and its member functions and then what to do with it?


This chapter aims to seek answers to the above questions, and will introduce a do not get enough attention in practice iterator type: istreambuf_iterator. If you prefer to use the STL, but then use istream_iterator character stream read performance is not very satisfactory, then istreambuf_iterator is the tool you need.



Article 26: iterator precedence over const_iterator, reverse_iterator, const_reverse_iterator


Some versions of insert and erase functions require the use of iterator. If you need to call these functions, you must use iterator. const iterators and reverse type can not meet the requirements of these functions.

To tell a const_iterator implicitly convert iterator is impossible, even using const_iterator converted into iterator technology is not widely used, but there is no guarantee efficiency.

From reverse_iterator converted from the iterator before use may need to be adjusted accordingly.



Article 27: Using the distance and the container advance const_iterator converted into iterator


To get distance calls successfully compile, need to exclude ambiguity. The easiest way is to explicitly specify the type parameter distance used, and prevents the compiler to infer the type parameter.



Article 28: the correct understanding of the base reverse_iterator () member function generated iterator usage


Through the base () function can be obtained with a reverse_iterator "corresponding" iterator is not accurate.

For the insertion operation, this relationship does exist. But for the delete operation, the situation is not so simple.



Article 29: For the input character by character, consider using istreambuf_iterator


If you need to read a character from the input stream one by one, then do not use formatted input; if you care about is spending time reading the stream, then the use of multiple-input istreambuf_iterator replace istream_iterator only three characters, it can achieve significant performance improvements. For non-formatted character by character input process, you should always consider using istreambuf_iterator.

Likewise, for non-character-by-formatted output process, you should consider using ostreambuf_iterator. It can avoid the additional burden brought about by the use of ostream_iterator, which has more superior performance.



Guess you like

Origin blog.csdn.net/qq_30534935/article/details/102764825