STL stands for Standard Template Library. It has been one of the most important parts of the standard library since 1999. bringing the most crucial improvements to the C++ language. It redefined the way we perceive data structures and the way we are coding right now. If you've just learned the basics of C++, then this is definitely an article that will help you gather more knowledge and progress.
The Standard Template Library - Iterators (Page 4 of 4 )
As you may have observed by now, each private/protected member is preceded by _ mark. The pop_back function shouldn’t be so hard to decrypt, so I leave that to you. Instead, take a look at the _Assign_nfunction. It’s a function for internal usage and you can see the begin() function in it. You should know that begin() returns an iterator to the first element in the array. But what exactly is an iterator?
Well, iterators can be perceived as pointers; a smarter pointer, more like a generalization of pointers, that have extended capabilities aside from pointing to a concrete member. They can be used to iterate through a data structure. This can be achieved because C++ provides operator overriding for classes. This way we can step (iterate) further into the container by just using the ++operator, for example.
The need for iterators occurs when you try to make some generic algorithms that do not depend on the container type. This way all generic algorithms (like the ones implemented in the algorithm’s header) can resolve most of the problems, regardless of the container type, by just getting two iterators that determine where to perform the algorithm.
Of course, iterators can be more complex than that. There exist many types depending on the level of access that they give to the data structure. Input Iterator, Output Iterator, Forward Iterator, Bidirectional Iterator, and Random Access Iterator are all used in algorithms, while a sixth concept, Trivial Iterator, is introduced only to clarify the definitions of the other iterator concepts. Debating over each of them is beyond the purpose of this article, but feel free to read about them in the official SGI documentation.
A very important note about iterators: always—and I mean always—use the ++iterator form instead of iterator++ if you can. It is more recommended purely for reasons related to performance. The second one creates a local variable in order to increase the iterator, and in an iteration, the time needed to create that local variable can add up significantly. But the first one just does what it’s asked: increase the iterator directly without creating a local variable.
Conclusions
Here we are at the end of part one. I really hope that you understood some basic ideas about how STL was created. This first part only introduced you to the way it looks at the internal level. Next time, I shall present the existing data structures and some basic ways to use them. By the time you reach the end of that segment, you should have a good bit of knowledge about STL.
Until then, please try to pick up the main ideas included in this article. Don’t forget to ask any question about what was described here in the blog. Don’t hesitate to do so because procrastination leads nowhere.
Also, if your goal is to ask for help from a community, then feel free to join one of our friendly forums. They are an online place where you’ll meet people that try to answer every little question you may have. Until we meet again, have some great moments and try to widen your knowledge on a consistent basis.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.