insert in pair c++ code example

Example 1: pair c++

// make_pair example
#include <utility>      // std::pair
#include <iostream>     // std::cout

int main () {
  std::pair <int,int> foo;
  std::pair <int,int> bar;

  foo = std::make_pair (10,20);
  bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>

  std::cout << "foo: " << foo.first << ", " << foo.second << '\n';
  std::cout << "bar: " << bar.first << ", " << bar.second << '\n';

  return 0;
}

Example 2: insert a value in pair in c++

#include<bits/stdc++.h>
using  namespace std;
int main(){
  //Declaring
  pair<string,int> p;
  
  //Initializing
  p.first = "Empty String";
  p.second = 100;
  
  //Output
  cout<<p.first;
  cout<<endl;
  cout<<p.second;
  
  return 0;
}

Tags:

Cpp Example