Associative Containers in C++














































Associative Containers in C++



Associative Containers

In associative containers elements are not arranged in sequence. Instead they are arranged in more complex way that makes it much faster to find a given item.  

Searching  is done using a key, which is usually a single value like a number or a string. This value is an attribute of the objects in the container, or it may be the entire object. 


The two main categories of associative containers in the  STL are

1. Sets

2. Maps


SETS:

Sets are a type containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.  


Some basic function associated with sets are 



1. begin() : returns an iterator to the first element of the set.


2. end() : returns an iterator to the last element of the set 


3. size() : returns the number of elements in the set.


4. max_size() : returns the maximum number of elements that the set can hold.



C++ program to demonstrate SETS:

#include<iostream>
#include <set>
#include <string>
using namespace std;
int main ()
{

set<int> s;
// inserting elements in random order .
s.insert(
60 ) ;
s.insert(
10 ) ;
s.insert(
20 ) ;
s.insert(
20 ) ;
s.insert(
40 ) ;
s.insert(
50 ) ;


set<int >::iterator it ;
cout << "The element of set s are : \n";
for (it = s.begin() ; it != s.end() ; it++ )
{
cout << *it<<" ";
}
cout << endl;

cout<<"Size of set is : "<<s.size();

return 0;

}


Output:
    The element of set s are : 
         10 20 40 50 60 
    Size of set is : 5

MAPS

A maps stores pair, where the first part of the pair is an object containing a key and the second part is an object containing a value. No two mapped value can have same keys.


Some basic function of maps are :


1. begin() : returns an iterator to the first element of the map.


2. end() : returns an iterator to the theoretical element that follow last element of the map.


3. size() :  returns the number of elements in the map.


4. max_size() : returns the maximum number of elements that the map can hold.


5. pair insert( keyvalue , mapvalue) : insert new element to the map.


6. erase( const c) : remove the key value 'c' from the map.


7. erase (iterator position) : remove the element at the position pointed by the iterator.


C++ Program to demonstrate MAPS :

#include<iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
map< char, int> m;
map< char, int> :: iterator it;

m.insert( pair<
char ,int> ('a' , 10));
m.insert( pair<
char ,int> ('b' , 14));
m.insert( pair<
char ,int> ('c' , 17));
m.insert( pair<
char ,int> ('d' , 11));
m.insert( pair<
char ,int> ('e' , 19));
m.insert( pair<
char ,int> ('f' , 12));
m.insert( pair<
char ,int> ('g' , 15));

cout<<"Size of map : "<<m.size()<<"\n";

// iterating over map
for(it= m.begin() ; it != m.end() ; it++)
{
cout<<(*it).first<<" -> "<<(*it).second<<"\n";
}

//deleting element from maps
it=m.find(
'e');
m.erase(it);

cout<<"After deleting size of map = "<<m.size()<<"\n";
return 0;
}


Output:

Size of map : 7
a -> 10
b -> 14
c -> 17
d -> 11
e -> 19
f -> 12
g -> 15
After deleting size of map = 6


Comments