make_pair() in C++ STL Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, make_pair() is a standard library function used to construct a key-value pair from the given arguments. The type of the pair constructed is deduced automatically from the type of arguments. In this article, we will learn about make_pair() function in C++.Let’s take a quick look at a simple example that illustrates std::make_pair(): C++ #include <bits/stdc++.h> using namespace std; int main() { // Creating a pair auto p = make_pair('A', 11); cout << p.first << " " << p.second; return 0; } OutputA 11 This article covers the syntax, usage, and common examples of make_pair() function in C++:Table of ContentSyntax of make_pair()Examples of make_pair()Creating a Pair of IntegersCreating Pair of Integer and StringInserting Pairs in a Mapmake_pair() Function in C++ – FAQsSyntax of make_pair()The make_pair() function defined inside <utility> header file.make_pair(key, val);Parameterskey: Represents the key for the pair object i.e. first value.val: Represents the value for the pair object i.e. second value.Return ValueReturns an object of std::pair having first and second members as key and val that were passed.Examples of make_pair()The following examples demonstrates the use of make_pair() function in different scenarios and for different purposes:Creating a Pair of Integers C++ #include <bits/stdc++.h> using namespace std; int main() { // Creating a pair of integers pair<int, int> p = make_pair(10, 20); cout << p.first << ": " << p.second; return 0; } Output10: 20Creating Pair of Integer and String C++ #include <bits/stdc++.h> using namespace std; int main() { // Make pair of integer and string auto p1 = make_pair(1, "Geeks"); cout << p1.first << ": " << p1.second; return 0; } Output1: GeeksInserting Pairs in a Map C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m; // Inserting pairs into the map m.insert(make_pair(1, "Geeks")); m.insert(make_pair(2, "Gfg")); for (const auto& pair : m) { cout << pair.first << ": " << pair.second << endl; } return 0; } OutputAlice Geeks Bob Geeks for Geeks Comment More infoAdvertise with us Next Article make_pair() in C++ STL S satwiksuman Follow Improve Article Tags : C++ STL cpp-pair CPP Examples Practice Tags : CPPSTL Similar Reads Pair in C++ STL In C++, pair is used to combine together two values that may be of different data types or same data types as a single unit. The first element is stored as a data member with name 'first' and the second element as 'second'.Example:CPP#include <bits/stdc++.h> using namespace std; int main() { / 5 min read Map of pairs in STL Map in STL is used to hash key and value. We generally see map being used for standard data types. We can also use map for pairs. For example consider a simple problem, given a matrix and positions visited, print which positions are not visited. CPP // C++ program to demonstrate use of map // for pa 2 min read multimap::operator= in C++ STL multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as p 2 min read multimap rbegin in C++ STL multimap::rbegin() is a built-in-function in C++ STL which returns an iterator pointing to the last element of the container. Syntax: multimap_name.rbegiin() Parameters: The function does not take any parameter. Return Value: The function returns a reverse iterator pointing to the last element of th 2 min read multimap key_comp in C++ STL This is the part of Standard Template Library(STL) of C++. To use this STL, use Namespace: std and include âmapâ header file in the program. It returns the function object or comparison object or ordering delegate that compares the keys, which is a copy of this container's constructor argument. It i 2 min read multimap key_comp() in C++ STL The std::multimap::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container.By default, this is a less object, which returns the same as operator â<'.It is a function pointer or a function object which takes two arguments of the same ty 2 min read set::swap() in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::swap() This func 2 min read forward_list::operator= in C++ STL Forward list in STL implements singly linked list. Introduced from C++11, forward lists are useful than other containers for insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from the list by the fact that forward list keeps tr 2 min read Sorting 2D Vector of Pairs in C++ A 2D vector also known as vector of vectors is a vector in which each element is a vector on its own. In other words, It is a matrix implemented with the help of vectors. What is a 2D vector of pairs? A 2D vector of pairs is a vector in which each element is a vector of pairs on its own. In other wo 15+ min read Multiset of Pairs in C++ with Examples What is Multiset? A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element. Some of the functions associated with a multiset: begin(): Returns an iterator to the first element in the m 4 min read Like