Welcome back to the sixth part in our series on the C++ Standard Template Library. This time we will be looking at Algorithms and Iterators. If you are new to this series you might want to get started at the beginning by looking at an overview of the C++ Standard Template Library.
Just in case you are already familiar with the basics you can go ahead and read ahead in this article. If you missed it, in our previous tutorial, we talked about the Unordered Associative Containers which includes the unordered Set or Multiset and the Unordered Map or Multimap.
We Start off With Iterators
There are five main types of Iterators in the C++ Standard Template Library as Follows:
- Random Access Iterators: as seen earlier used in the array, vector, and deque
- Bidirectional Iterators: these have appeared in the list, set, multiset, map, and multimap
- Forward Iterators: which showed prominently in the forward_list
- Input Iterators: used mainly to read and process values while moving forward only
- Output Iterators: used mainly to output values while moving forward only
Each container in C++ STL provides an Iterator and a const Iterator. Const Iterators provide read-only access to the containers’ elements:
There is a special kind of Iterator which is a predefined iterator better known as Iterator Adaptor. There are four types of these predefined Iterators as follows.
1. Insert Iterator
This can be used to insert elements into the container at certain positions. The following is an example:
Other insert Iterators include back_insert_iterator which inserts at the back and front_insert_iterator which inserts at the front of a container.
2. Stream Iterator
This is used to iterate through the data that is originating from a stream. An example of this iterator is illustrated below.
3. Reverse Iterator
As its name suggests, this iterator is used to traverse a container in the reverse order as illustrated below. Notice the r prefix in the functions in the for the loop.
4. Move Iterator (as of C++11)
This iterator introduced in C++11 behaves exactly like the underlying input iterator. This iterator moves values rather than copy them. Take a look at this example.
Now we can Dive Into Algorithms
Algorithms in the C++ Standard Template Library are usually implemented as loops. This is interesting in that wherever you find a for loop or a while loop in your code, that becomes a likely candidate to replace with an algorithm from STL. Why we relegated algorithms to the back of this article is because the functions are too numerous to feature here. So we will just be mentioning a few that are applicable to almost all containers. We would then advise you to go to this link and read more about them.
These Algorithm functions cover a wide range of functions over a range of elements in a container. The range starts at the first element all the way past the last element. Meanwhile here are a few examples of algorithms at work even though we have used them extensively in previous articles in this series.
In the next article in this series on the C++ Standard Template Library, we will be talking about Templates. We have used them in all the previous articles in the series but have not really taken an in-depth look at them. So keep practicing with iterators and algorithms as you wait for the next article on C++ Templates.
Ref:
https://isocpp.org/
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.