
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Utility::make_pair() Function
The C++ std::utility::make_pair() function is used to create a pair object, which is a simple container for storing two heterogeneous values. It allows you to combine the two values into a single object without explicitly specifying the types.
Syntax
Following is the syntax for std::utility::make_pair() function.
pair<V1,V2> make_pair (T1&& x, T2&& y);
Parameters
- x, y − It indicates the values for the members first and second.
Return Value
It returns a pair object whose elements first and second are set to x and y respectivelly.
Exceptions
If the construction or assignment of type T throws.
Data races
If either (or both) T1 or T2 is an rvalue reference type of a type supporting move semantics, its corresponding argument is modified.
Example 1
In the following example, we are going to consider the basic usage of the make_pair() function.
#include <iostream> #include <utility> int main() { auto x = std::make_pair(1, "Welcome"); std::cout << "Result : " << x.first << ", " << x.second << std::endl; return 0; }
Output
Output of the above code is as follows −
Result : 1, Welcome
Example 2
Consider the following example, where we are going to use the vector for storing the values.
#include <iostream> #include <vector> #include <utility> int main() { std::vector < std::pair < int, char >> x; x.push_back(std::make_pair(11, 'A')); x.push_back(std::make_pair(13, 'B')); for (const auto & p: x) { std::cout << "Result : " << p.first << ", " << p.second << std::endl; } return 0; }
Output
Following is the output of the above code −
Result : 11, A Result : 13, B
Example 3
Let's look at the following example, where we are going to use the make_pair() in the map.
#include <iostream> #include <map> #include <utility> int main() { std::map < int, std::string > x; x.insert(std::make_pair(14, "Hi")); x.insert(std::make_pair(12, "Hello")); for (const auto & p: x) { std::cout << "Result : " << p.first << ", " << p.second << std::endl; } return 0; }
Output
If we run the above code it will generate the following output −
Result : 12, Hello Result : 14, Hi