How to Iterate Over a Vector of Pairs in C++? Last Updated : 02 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vectors are dynamic arrays that allow users to store data in a contiguous memory location while pairs are data structures that allow the users to store two heterogeneous values together in the key-value pair. In this article, we are to learn how we can iterate over a vector of pairs in C++. For Example, Input:myVector = <{1: "Geek"}, {2:" for"}, {3:" Geeks"} >Output:Vector Elements:Key: 1, Value: GeekKey: 2, Value: forKey: 3, Value: GeeksIterate Over a Vector of Pairs in C++In C++, we can use the range-based for loop to iterate over each element of the vector of pairs. We can then access the first and second data members of each of the pair containers. C++ Program to Iterator Over a Vector of Pairs C++ // C++ Program to Iterate through a Vector of Pairs #include <iostream> #include <utility> #include <vector> using namespace std; int main() { // Declare a vector of pairs vector<pair<int, string> > vec; // Insert pairs inside the vector vec.push_back({ 1, "Geek" }); vec.push_back({ 2, "for" }); vec.push_back({ 3, "Geeks" }); // Iterate through the vector of pairs using range-based // for loop cout << "Vector Elements:" << endl; for (auto pair : vec) { cout << "Key: " << pair.first << ", Value: " << pair.second << endl; } return 0; } OutputVector Elements: Key: 1, Value: Geek Key: 2, Value: for Key: 3, Value: Geeks Time Complexity: O(N) where N is the number of elements present in the vector.Auxilary Space : O(1) Comment More infoAdvertise with us Next Article How to Iterate Over a Vector of Pairs in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-vector cpp-pair CPP Examples +1 More Practice Tags : CPP Similar Reads How to Iterate Over a Set of Pairs in C++? In C++, a set is a container that stores unique values in some specified order while pairs are data structures that store key-value pairs. In this article, we will look at how we can iterate through a set of pairs in C++. For Example, Input: mySet = { {100: "Geek"}, {200:" for"}, {300:" Geeks"} } Ou 2 min read How to Reverse Iterate a Vector in C++? In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai 2 min read How to Iterate Through a Vector in C++? Iterating or traversing a vector means accessing each element of the vector sequentially. In this article, we will learn different methods to iterate over a vector in C++.The simplest way to iterate a vector is by using a range based for loop. Let's take a look at a simple example that iterate the v 3 min read How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that 3 min read How to Create a Vector of Pairs in C++? 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 5 min read Like