Home arrow C++ arrow Page 3 - Brief Introduction to the STL Containers
C++

Brief Introduction to the STL Containers


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.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
July 01, 2008
TABLE OF CONTENTS:
  1. · Brief Introduction to the STL Containers
  2. · Sequence Containers
  3. · Associative Containers
  4. · Closing Thoughts

print this article
SEARCH DEVARTICLES

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.


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