Copying items from containers to other containers
The most important STL data structures have iterator support. This means that it is at least possible to get iterators via begin()
and end()
functions, which point to the data structure's underlying payload data and allow to iterate over that data. The iteration always looks the same, no matter what kind of data structure is iterated over.
We can get iterators from vectors, lists, deques, maps, and so on. Using iterator adaptors, we can even get iterators as an interface to files, standard input, and standard output. Moreover, as we saw in the previous chapter, we can even wrap iterator interfaces around algorithms. Now, where we can access everything with iterators, we can combine them with STL algorithms, which accept iterators as parameters.
A really nice way to show how iterators help abstract the nature of different data structures away is the std::copy
algorithm, which just copies items from one set of iterators to an output iterator...