map operator and swap non-member function


The map container has some non-member functions,which are also a template functions.These functions can be helpful in some ways.They provide an easy way to compare the elements of the two map containers as a whole.

The non-member functions of the map container is provided below;note they are all template.

1 bool operator==
2 bool operator<
3 bool operator!=
4 bool operator>
5 bool operator<=
6 bool operator>=
7 void swap

Click on the link to navigate to the function.



template<typename Key, typename T, typename Comp, typename Alloc>
inline bool operator==(
const map<Key, T, Comp, Alloc>& x,
const map<Key, T, Comp , Alloc>& y);

The operator== compares all the elements of the two map containers.This function returns true if all the elements of the two map containers are equal and there should be equal number of elements.

Code example

map<int,string>mp{{12 , “twelve”} , {34 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}} ;

cout<< (mp==mp1) << endl
<< (mp==mp2) ;

Output

0
1



template<typename Key, typename T, typename Comp, typename Alloc>
inline bool operator<(
const map<Key, T, Comp, Alloc>& x,
const map<Key, T, Comp , Alloc>& y);

Compare the elements of the two map containers.This function returns true only if all the key of the first map container elements is smaller than the key of the second map container elements.

Code example

map<int,string>mp{{12 , “twelve”} , {34 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}} ;

cout<< (mp<mp1) << endl
<< (mp<mp2) ;

Output

1
0


 


Stop wasting time,earn money($$$) from your website-Join Now!


template<typename Key, typename T, typename Comp, typename Alloc>
inline bool operator!=(
const map<Key, T, Comp, Alloc>& x,
const map<Key, T, Comp , Alloc>& y);

The operator!= returns true if the two map containers is not equal.
key should not be same
value should not be same and
number of elements should not be same
Only under this condition the function returns true.

Code example
Code example

map<int,string>mp{{12 , “twelve”} , {34 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}};

cout<< (mp!=mp1) << endl
<< (mp!=mp2) ;

Output

1
0



template<typename Key, typename T, typename Comp, typename Alloc>
inline bool operator>(
const map<Key, T, Comp, Alloc>& x,
const map<Key, T, Comp , Alloc>& y);

This function returns true only under the condition mentioned below
the key should be larger
number of elements does not matter.

Code example

map<int,string>mp{{122 , “twelve”} , {324 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}};

cout<< (mp>mp1) << endl
<< (mp>mp2) ;

Output

1
1






template<typename Key, typename T, typename Comp, typename Alloc>
inline bool operator<=(
const map<Key, T, Comp, Alloc>& x,
const map<Key, T, Comp , Alloc>& y);

The opeator<= returns true if
All the keys of the first container is smaller than the all keys of the second container.In this case number of elements does not matter.
key of both the container’s elements are equal ans also the number of elements must be equal.

Code example

map<int,string>mp{{122 , “twelve”} , {324 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}};

cout<< (mp<=mp1) << endl
<< (mp<=mp2) ;

Output

1
1



template<typename Key, typename T, typename Comp, typename Alloc>
inline bool operator=>(
const map<Key, T, Comp, Alloc>& x,
const map<Key, T, Comp , Alloc>& y);

Compares the elements of the two map containers for greater than or equal to.The function returns true if:
All the keys of the first container is larger than the elements of the second container.The number of elements does not matter.
All the key of the two containers are equal.

Code example

map<int,string>mp{{122 , “twelve”} , {324 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}};

cout<< (mp>=mp1) << endl
<< (mp>=mp2) ;

Output

0
1


 


Stop wasting time,earn money($$$) from your website-Join Now!


template<typename Key, typename T, typename Comp, typename Alloc>
inline void swap(map<Key, T , Comp , Alloc>& x,
map<Key, T , Comp , Alloc>& y);

This swap function is a non-member version.This function actually calls the member version and so it take a constant time.This means this function is also fast.

Link: constant time

Code example

map<int,string>mp{{122 , “twelve”} , {324 , “Thrity”}} ,
mp1{{34 , “Thrity-four”} , {122 , “Twelve”} , {90 , “ninety”}} ,
mp2{{12 , “twelve”} , {34 , “Thrity”}};

swap(mp,mp1);

for(auto elem:mp)
{
cout<< elem.first << ” ” << elem.second << endl ;
}
cout<< endl ;
for(auto elem:mp1)
{
cout<< elem.first << ” ” << elem.second << endl ;
}

Output

34 Thrity-four
90 ninety
122 Twelve

12 twelve
34 Thrity