C++
  Home arrow C++ arrow Brief Introduction to the STL Containers
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Brief Introduction to the STL Containers
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-07-01

    Table of Contents:
  • Brief Introduction to the STL Containers
  • Sequence Containers
  • Associative Containers
  • Closing Thoughts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Brief Introduction to the STL Containers


    (Page 1 of 4 )

    STL stands for Standard Template Library. It is probably one of the most important contributions to the standard library. It has brought the most crucial improvements to the C++ language and redefined the way we perceive the data structures and the way we are coding right now. If you just know the basics of C++, then this is definitely an article that will help you gather knowledge and progress.

    Throughout this article, I will try to present each existing type briefly, starting with their basic usage, how they are represented, and the way you should work with them. I'll give the pros and cons, when and where to use it, and I'll add data structures that are either related to it or are the same, but just an individualized version.

    Before we begin, please make sure that you've read the previous part of this multi-part article series, Introduction to the STL. In it, I covered a few key points that are taken for granted because it's assumed that you already know them. A quick search for the name of the article should reveal its location.

    To start, STL containers can be divided into two parts: associative and sequence type. Sequence is probably familiar because you can use it in C too. An array of char would be a good example (char[100]). This is a succession of elements-one after another. Associative containers, on the other hand, are more elegant. The idea behind them is to provide a data structure from which elements can be accessed in an efficient way.

    For example, let's say we have 10 elements. Now if we put them in an array of size 10, and we want to access the last item, we would have to start from the first item and iterate through the array to the tenth element. This would result in a linear complexity of 10.

    However, if the order of elements in the array is negligible, or a sorted array is preferred, we can find any item in it using the binary search algorithm (which is a logarithmic algorithm) in O(logN) time. This is quite an improvement for a structure containing many elements.

    We can also add the vector, list, deque, and string (the slist, xstring, queue and stack as individualized types) to the sequence containers. As far as the associative containers, we can add the set and map (the multiset and multimap as individualized types). There are also the hash-related containers for the associative ones, however, I won't present these because they are more complex and are rarely used.

    Before we go any further, I'm going to show you how to make any of these containers available for use. First, you need to include the header for the container type. To make this even simpler for each of the containers this is exactly the same as its own name and because it's included in the standards you can forget the .h extension.

    The .h versions are also present for some backwards compatibility, so if you type them simply out of habit, they will work too. STL is included in the standard namespace, so you may have two ways to use it, such as presenting it on a vector:

    #include<vector>

    And:

    std::vector

    //or

    using namespace std;

    ...

    Vector

    The using namespaces keyword notifies the compiler which namespace to include the vector. Now that we are through with this, let's take a closer look at each item.

    More C++ Articles
    More By Gabor Bernat


       · This is the second part to the sequel. I hope by reading it you got a got grasp of...
     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT