What is Map in C++ STL? Different operation on Map with example
Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value.
1 Unique key values: no two elements in the map have keys that compare equal to each other. For a similar associative container allowing for multiple elements with equivalent keys, see multimap.
2 Each element is composed of a key and a mapped value. For a simpler associative container where the element value itself is its key, see set.
3 Elements follow a strict weak ordering at all times. Unordered associative arrays, like unordered_map, are available in implementations following TR1.
template < class Key, class T, class Compare = less
class Allocator = allocator
Where the template parameters have the following meanings:
1 Key: Type of the key values. Each element in a map is uniquely identified by its key value.
2 T: Type of the mapped value. Each element in a map is used to store some data as its mapped value.
3 Compare: Comparison class: A class that takes two arguments of the key type and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are key values, shall return true if a is to be placed at an earlier position than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to less
4 Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Operation on Map
1. insert Insert element (public member function)
// map::insert
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
pair<map<char,int>::iterator,bool> ret;
// first insert function version (single parameter):
mymap.insert ( pair<char,int>('a',100) );
mymap.insert ( pair<char,int>('z',200) );
ret=mymap.insert (pair<char,int>('z',500) );
if (ret.second==false)
{
cout << "element 'z' already existed";
cout << " with a value of " << ret.first->second << endl;
}
// second insert function version (with hint position):
it=mymap.begin();
mymap.insert (it, pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, pair<char,int>('c',400)); // no max efficiency inserting
// third insert function version (range insertion):
map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
// showing contents:
cout << "mymap contains:\n";
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
cout << "anothermap contains:\n";
for ( it=anothermap.begin() ; it != anothermap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}
Output:
element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300
2. erase Erase elements (public member function)
mymap.erase (it); // erasing by iterator
mymap.erase ('c'); // erasing by key
mymap.erase ( it, mymap.end() ); // erasing by range
3. swap Swap content (public member function)
foo.swap(bar);
4. clear Clear content (public member function)
All the elements in the container are dropped: their destructors are called, and then they are removed from the container, leaving it with a size of 0.
What is set in C++ STL ? List the various operation on set with examples
Set: Sets are a kind of associative containers that stores unique elements, and in which the elements themselves are the keys.
Therefore, the main characteristics of set as an associative container are:
1. Unique element values: no two elements in the set can compare equal to each other. For a similar associative container allowing for multiple equivalent elements, see multiset.
2. The element value is the key itself. For a similar associative container where elements are accessed using a key, but map to a value different than this key, see map.
3. Elements follow a strict weak ordering at all times. Unordered associative arrays, like unordered_set, are available in implementations following TR1.
Creation of set take three parameter
template < class Key, class Compare = less
class Allocator = allocator
Where the template parameters have the following meanings:
1 Key: Key type: type of the elements contained in the container. Each elements in a set is also its key.
2 Compare: Comparison class: A class that takes two arguments of the same type as the container elements and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are elements of the container, shall return true if a is to be placed at an earlier position than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to less
3 Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template for type Key is used, which defines the simplest memory allocation model and is value-independent
Operations:
1. insert:: Insert element (public member function) / set::insert #include#include using namespace std; int main () { set myset; set ::iterator it; pair ::iterator,bool> ret; // set some initial values: for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50 ret = myset.insert(20); // no new element inserted if (ret.second==false) it=ret.first; // "it" now points to element 20 myset.insert (it,25); // max efficiency inserting myset.insert (it,24); // max efficiency inserting myset.insert (it,26); // no max efficiency inserting int myints[]= {5,10,15}; // 10 already in set, not inserted myset.insert (myints,myints+3); cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); it++) cout << " " << *it; cout << endl; return 0; } Output: myset contains: 5 10 15 20 24 25 26 30 40 50 2. erase:: Erase elements (public member function) myset.erase (it); myset.erase (40); it=myset.find (60); myset.erase ( it, myset.end() ); 3. swap:: Swap content (public member function) int myints[]={12,75,10,32,20,25}; set first (myints,myints+3); // 10,12,75 set second (myints+3,myints+6); // 20,25,32 set ::iterator it; first.swap(second); 4. clear All the elements in the set container are dropped: their destructors are called, and then they are removed from the container, leaving it with a size of 0. myset.clear();
What are the different modifier and operation on the list STL
1.assign:: Assign vector content (public member function)
2.push_back:: Add element at the end (public member function)
3.pop_back:: Delete last element (public member function)
4.insert::Insert elements (public member function)
5.erase::Erase elements (public member function)
6.swap::Swap content (public member function)
7.clear::Clear content (public member function)
8. push_front
9. pop_front
Operation:
1. splice:: Move elements from list to list (public member function) [Moves elements from list x into the list container at the specified position, effectively inserting the specified elements into the container and removing them from x.]
list mylist1, mylist2;
list::iterator it;
// set some initial values:
for (int i=1; i<=4; i++)
mylist1.push_back(i); // mylist1: 1 2 3 4
for (int i=1; i<=3; i++)
mylist2.push_back(i*10); // mylist2: 10 20 30
it = mylist1.begin();
++it; // points to 2
mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)
mylist2.splice (mylist2.begin(),mylist1, it);
// mylist1: 1 10 20 30 3 4
// mylist2: 2
// "it" is now invalid.
it = mylist1.begin();
advance(it,3); // "it" points now to 30
mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
// mylist1: 30 3 4 1 10 20
2. remove::Remove elements with specific value (public member function)
mylist.remove(89);
3. remove_if::Remove elements fulfilling condition (public member function template)
// list::remove_if
#include
#include
using namespace std;
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
class is_odd
{
public:
bool operator() (const int& value) {return (value%2)==1; }
};
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
list mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
cout << "mylist contains:";
for (list::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
4. unique::Remove duplicate values (member function)
mylist.unique();
5. merge:: Merge sorted lists (public member function)
// list::merge
#include
#include
using namespace std;
// this compares equal two doubles if
// their interger equivalents are equal
bool mycomparison (double first, double second)
{ return ( int(first)
int main ()
{
list first, second;
first.push_back (3.1);
first.push_back (2.2);
first.push_back (2.9);
second.push_back (3.7);
second.push_back (7.1);
second.push_back (1.4);
first.sort();
second.sort();
first.merge(second);
second.push_back (2.1);
first.merge(second,mycomparison);
cout << "first contains:";
for (list::iterator it=first.begin(); it!=first.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
sort:: Sort elements in container (public member function)
mylist.sort();
mylist.sort(compare_nocase);
reverse::Reverse the order of elements (public member function)
mylist.reverse();
What are the different modifier on vector STL
1.assign:: Assign vector content (public member function)
2.push_back:: Add element at the end (public member function)
3.pop_back:: Delete last element (public member function)
4.insert::Insert elements (public member function)
5.erase::Erase elements (public member function)
6.swap::Swap content (public member function)
7.clear::Clear content (public member function)
vector<int> first;
vector<int> second;
vector<int> third;
first.assign (7,100); //A repetitive 7 times of value 100
vector<int>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
first.swap(second); //Swap the containers
first.clear(); //delete all the element leaving the size of the container to 0
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
// inserting into a vector
#include
#include
using namespace std;
int main ()
{
vector myvector (3,100);
vector::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
vector anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
cout << "myvector contains:";
for (it=myvector.begin(); it
cout << " " << *it;
cout << endl;
return 0;
}
