Maps are the container in STL which is used to store the elements in the form of key-value pair. Internally, the elements in a map are always sorted by its key. Maps are mainly implemented as binary search trees.
map::at()
at() function is used to return the reference to the element associated with the key k.
Syntax:
map1.at(k) Parameters: k is the Key value of the element whose associated value is accessed.
Return: It return a reference to the associated value of the element whose key value is equivalent to k.
Examples:
Input: map1 = {
{1, 'a'},
{2, 'b'},
{3, 'c'},
{4, 'd'}
}
map1.at(2);
Output: b
Input: map2 = {
{'w', 1},
{'x', 2},
{'y', 3}
}
map2.at('w');
Output: 1
// CPP program to illustrate // Implementation of swap() function #include <bits/stdc++.h> using namespace std; int main() { // Take any two maps map<int, char> map1; map<char, int> map2; map1[1] = 'a'; map1[2] = 'b'; map1[3] = 'c'; map1[4] = 'd'; map2['w'] = 1; map2['y'] = 2; map2['z'] = 3; // Print the associated element cout << "Element at map1[2] = " << map1.at(2) << endl; cout << "Element at map2['w'] = " << map2.at('w') << endl; return 0; } |
Output:
Element at map1[2] = b Element at map2['w'] = 1
map::swap()
swap() function is used to exchange the contents of two maps but the maps must be of same type, although sizes may differ.
Syntax:
map1.swap(map2)
OR
swap(map1, map2)
Parameters:
map1 is the first map object.
map2 is the second map object.
Return value: None
Examples:
Input : map1 = {
{1, 'a'},
{2, 'b'},
{3, 'c'},
{4, 'd'}
}
map2 = {
{5, 'w'},
{6, 'x'},
{7, 'y'}
}
swap(map1, map2)
Output : map1 = {
{5, 'w'},
{6, 'x'},
{7, 'y'}
}
map2 = {
{1, 'a'},
{2, 'b'},
{3, 'c'},
{4, 'd'}
}
// CPP program to illustrate // Implementation of swap() function #include <bits/stdc++.h> using namespace std; int main() { // Take any two maps map<int, char> map1, map2; map1[1] = 'a'; map1[2] = 'b'; map1[3] = 'c'; map1[4] = 'd'; map2[5] = 'w'; map2[6] = 'x'; map2[7] = 'y'; // Swap elements of queues swap(map1, map2); // Print the elements of maps cout << "map1:\n" << "\tKEY\tELEMENT\n"; for (auto it = map1.begin(); it != map1.end(); it++) cout << "\t" << it->first << "\t" << it->second << '\n'; cout << "map2:\n" << "\tKEY\tELEMENT\n"; for (auto it = map2.begin(); it != map2.end(); it++) cout << "\t" << it->first << "\t" << it->second << '\n'; return 0; } |
Output:
map1:
KEY ELEMENT
5 w
6 x
7 y
map2:
KEY ELEMENT
1 a
2 b
3 c
4 d
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- fill() and fill_n() functions in C++ STL
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- accumulate() and partial_sum() in C++ STL : numeric header
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Descending order in Map and Multimap of C++ STL
- Insertion and Deletion in STL Set C++
- std::minmax() and std::minmax_element() in C++ STL
- deque::at() and deque::swap() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::empty() and list::size() in C++ STL
- list::remove() and list::remove_if() in C++ STL
- queue::empty() and queue::size() in C++ STL
- queue::push() and queue::pop() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- stack empty() and stack size() in C++ STL
- deque::front() and deque::back() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

