![]() |
VOOZH | about |
In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in C++.
Table of Content
A vector of pairs is a std::vector container in which each element is of std::pair type. In C++, we can create a vector of pairs by passing std::pair of the desired type as the template parameter when declaring std::vector.
vector<pair<key_type, val_type>> v;
where,
1 Geeks 2 Geeksfor 3 GeeksforGeeks
We cannot initialize a vector element by directly passing key type and value type separated by a comma to any vector insertion function like vector::push_back().
v.push_back("Geeks", 1); // Incorrect
The compiler read this format as two separate arguments rather than a pair, leading to a "no matching function call" error. That is why we have to initialize the vector of pair elements by using these two methods:
In this method, we enclose the key value pair inside braces { }. The compiler then treats it as a single argument and pass it to the std::pair constructor to create a pair with given key and value.
Example
v.push_back( {"Geeks", 1} );
The std::make_pair() is a standard library function that is specifically designed only to create a std::pair from the given key and value provided as arguments. We can use this function to first create a pair and then pass it to the vector insertion function.
Example
v.push_back( std::make_pair("Geeks", 1) );
For insertion, it is recommended to use vector::emplace_back() as it avoids creating the extra copy.
We can use any of the vector access method such as access [] operator, vector::at(), vector::back() and vector::front() to access the pair at any given index. This will give us the pair that is present at this index. Then we can access the key of this pair by using .first and value using .second.
We can also assign them a new value using = assignment operator.
Example
v[2].first // Accessing key
v[2].second // Accessing valuev[2].first= 12 // Assigning key
v[2].second = "GfG" // Assigning value
The behaviour of the STL algorithms with vector of pairs is same as that of vector of other types. But there will be some cases in which we will have to define the working of that algorithm with pairs. This is because the operation that a given algorithm performs is not defined for the std::pair by default.
For example, the std::accumulate algorithm doesn't work for vector of pairs unless we define a custom operation because the compiler doesn't know how to perform its default operation (+) addition for std::pair.
On the other hand, std::sort works find for it as its default operation < greater than is defined for std::pair.
To know which STL algorithm will work on vector of pairs, we need to know the operation of that algorithm and check if that operation is defined for std::pair or not.
The following examples demonstrate the use of vector of pairs in C++.
1 Geeks 2 Geeksfor 3 GeeksforGeeks
6: GeeksGeeksforGeeksforGeeks