
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
map::at() and map::swap() in C++ STL
In this article we will be discussing the working, syntax and examples of map::at() and map::swap() functions in C++ STL.
What is a Map in C++ STL?
Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.
What is a map::at()?
map::at() function is an inbuilt function in C++ STL, which is defined in
If there is a case when the key is not matching to any key of the map container then, the function throws an out_of_range exception.
Syntax
map_name.at(key& k);
Parameters
The function accepts one parameter i.e.
- k − This is the key where we want to refer to.
Return value
This function returns a reference to the value associated with key k which we are looking for.
Example
Input
std::map<int> mymap; mymap.insert({‘a’, 10}); mymap.insert({‘b, 20}); mymap.insert({‘c, 30}); mymap.at(‘b’);
Output
b:20
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_1; map<int, int> TP_2; TP_1[1] = 10; TP_1[2] = 20; TP_1[3] = 30; TP_1[4] = 40; TP_2[5] = 50; TP_2[6] = 60; TP_2[7] = 70; cout<<"Elements at TP_1[1] = "<< TP_1.at(1) << endl; cout<<"Elements at TP_1[2] = "<< TP_1.at(2) << endl; cout<<"Elements at TP_1[3] = "<< TP_1.at(3) << endl; cout<<"\nElements at TP_2[7] = "<< TP_2.at(7) << endl; cout<<"Elements at TP_2[5] = "<< TP_2.at(5) << endl; return 0; }
Output
Elements at TP_1[1] = 10 Elements at TP_1[2] = 20 Elements at TP_1[3] = 30 Elements at TP_1[7] = 70 Elements at TP_1[5] = 50
What is a map::swap()?
map::swap() function is an inbuilt function in C++ STL, which is defined in <map> header file. swap() is used to swap the content of the two map containers. This function swaps the values of two map containers irrespective to the size of both the map containers.
When this function gets called it takes the parameter which is another map container and swap the contents with the associated container.
Syntax
map_name.swap(map& map_name2);
Parameters
The function accepts one parameter i.e.
- map_name2 − This is the other map container’s object whose data we want to swap with the associated map container.
Return value
This function returns nothing.
Example
Input
std::map<int> odd, eve; odd.insert({‘a’, 1}); odd.insert({‘b’, 3}); odd.insert({‘c’, 5}); eve.insert({‘d’, 2}); eve.insert({‘e’, 4}); eve.insert({‘f’, 6}); odd.swap(eve);
Output
Odd: d: 2 e:4 f:6 Eve: a:1 b:3 c:5
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_1; map<int, int> TP_2; TP_1[1] = 10; TP_1[2] = 20; TP_1[3] = 30; TP_1[4] = 40; TP_2[5] = 50; TP_2[6] = 60; TP_2[7] = 70; swap(TP_1, TP_2); cout<<"Elements of TP_1 after swap:\n"<< "\tKEY\tELEMENT\n"; for (auto i = TP_1.begin(); i!= TP_1.end(); i++) { cout << "\t" << i->first << "\t" << i->second << '\n'; } cout << "Elements of TP_2 after swap:\n"<< "\tKEY\tELEMENT\n"; for (auto i = TP_2.begin(); i!= TP_2.end(); i++) { cout << "\t" << i->first << "\t" << i->second << '\n'; } return 0; }
Output
Elements of TP_1 after swap: KEY ELEMENT 5 50 6 60 7 70 Elements of TP_2 after swap: KEY ELEMENT 1 10 2 20 3 30 4 40
- Related Articles
- map::at() in C++ STL
- map equal_range() in C++ STL
- map emplace() in C++ STL
- map get_allocator in C++ STL
- map value_comp() in C++ STL
- map::clear() in C++ STL
- map insert() in C++ STL
- map operator= in C++ STL
- map::empty() in C++ STL
- map::size() in C++ STL
- map max_size() in C++ STL
- map::begin() and end() in C++ STL
- Set vs Map in C++ STL
- map emplace_hint() function in C++ STL
- map erase() function in C++ STL
