C++
  Home arrow C++ arrow Page 3 - 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 - Associative Containers


    (Page 3 of 4 )

    1)  Set:

    Declaration:

     

    Header: set<Key, Compare, Alloc>

    In Code:

    set<int> aSet;

    Representation:

    A set can be most easily comprehended as a sorted sequence of items. Items added to it are always added so that a sorted property of the set remains intact. The set is sorted in increasing order. The compare is handled by default by the >= operator when defined for the type, or you can pass a function as a second parameter at creation time.

    The last parameter in the template declaration is the usual allocation-type chooser. For example, the code below will print 2 into the console for the size and 0 and 1 as members. This is because no multiple existence is allowed in a set. So, only one 1 is added from the vector.

    int item = 0;

    std::vector<int> aVector;

     

    aVector.reserve(10); // Reserve memory for 10 items

    aVector.resize(10,1); // Resize the vector with 10 1

    aVector.push_back(item); // add another item / a resize will be called

    std::set<int> aSet;

    aSet.insert(aVector.begin(), aVector.end());

    cout << aSet.size() << endl;

    copy(aSet.begin(), aSet.end(), ostream_iterator<int>(cout, " "));

    Cons/Pros:

    Inserting an item into a set is a fast operation. Inserting a range is also fast (linear time if the inserted range is already sorted). Iterators aren't deemed invalid after modifying the structure of the set unless the iterator was pointing to the modified item. Searching for an item is very fast.

    Related types:

    multiset -> In this version, multiple presence of the same item is allowed. For example, you may have two "2" numbers within a set, despite the fact that this isn't correct mathematically.

    2)  Map:

    Declaration:

    In Header: map<Key, Data, Compare, Alloc>

    In Code:

    map< string, int> aMap;

    Representation:

     

    A map is similar to a set, but, as you can see on the image above, we can also assign data for the key. For the containers above, the [] operator was just returning the nth item when available. However, for the map, this gets a new functionality. Check out the code below:

    string text("Avril");

    aMap[text] = 9;

    aMap[text] = 10;

    The first call of the operator inserts the text "Avril" into the container and sets the value pointed by it to 9. The second one just resets the value to 10. You see, the [] operator searches for the key and if it manages to find one, it returns a reference to the data member that was pointed to by the key. However, if it fails to find it, it inserts it, and right after the insertion, it returns the reference to the data member.

    Just as for the set, here is a sorted container with the corresponding advantages. Also, an iterator that is returned is now a pair, since it holds the key and the data for it. You can refer to the key as first and the data as -> second. Take a look at the example following:

    map<string, int>::iterator it = aMap.begin();

    it->first; // will return the key, in our case the text //"Avril"

    it->second; // return the second item, the 10

    string text_2(" Lavigne");

    aMap[text_2] = 11;

    it = aMap.find(text_2 + "None");

    if( it == aMap.end())

    cout << " The searched item not found";

    Related types:

    multimap -> In this version, multiple presence of the same item is allowed. For example, you may have a "green",1 pair and a "green",2 at the same time.

    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
    For more Enterprise Application Development news, visit eWeek