Use push_back for vector pair in C++

A vector is a dynamic array that can resize according to the elements inserted or deleted inside the container. In this tutorial, we will be learning how to use push_back for vector pairs in C++.

C++ program for push back in Vector pair

Vector pair is multiple numbers of pairs that can store two values mapped to each other. We can insert a pair in vector using vector “make_pair” function  (pair functions are inside #include<algorithm>).


  • Making a vector pair
    Vector<pair<datatype1, datatype2>> object;
  • Push back in vector pair
    object. push_back (make_pair (key1, key2));


To insert a vector pair first we declare the vector pair using the given syntax then we create a character array containing names of fruits and an integer array containing the price of each fruit respectively.

Then by using Push back in vector pair (syntax given above) we insert all the values in vector pair.

At last, we print the whole vector array using a loop. We can access the first element by v[i].first and the second element by v[i].second.Where v is the name of vector pair and i is the position in vector array.

using namespace std; 
int main() 
    //declaring vector of pairs 
    vector< pair <string,int> > v; 
    // initialising key values
    string arr[] = {"Apple","Mango","Watermelon","litchi" }; 
    int arr1[] = {150, 120, 50, 110}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    //Inserting value in vector pair
    for (int i=0; i<n; i++) 
        v.push_back( make_pair(arr[i],arr1[i]) ); 
    // Printing the vector 
    for (int i=0; i<n; i++) 
        cout << v[i].first << " " << v[i].second << endl; 
    return 0; 




Apple 150
Mango 120
Watermelon 50
litchi 110

Leave a Reply

Your email address will not be published. Required fields are marked *