Home arrow C++ arrow Page 4 - The Standard Template Library
C++

The Standard Template Library


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.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 24, 2008
TABLE OF CONTENTS:
  1. · The Standard Template Library
  2. · History Class
  3. · Looking Inside the Library
  4. · Iterators

print this article
SEARCH DEVARTICLES

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_n function. 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.

blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials