std::pair

suggest change

Creating a pair and accessing the elements

Pair allows us to treat two objects as one object. Pairs can be easily constructed with the help of template function std::make_pair.

Alternative way is to create pair and assign its elements (first and second) later.

#include <iostream>
#include <utility>

int main()
{
  std::pair<int,int> p = std::make_pair(1,2); //Creating the pair
  std::cout << p.first << " " << p.second << std::endl; //Accessing the elements
	// We can also create a pair and assign the elements later
	std::pair<int,int> p1;
	p1.first = 3;
	p1.second = 4;
	std::cout << p1.first << " " << p1.second << std::endl;
	
	//We can also create a pair using a constructor
	std::pair<int,int> p2 = std::pair<int,int>(5, 6);
	std::cout << p2.first << " " << p2.second << std::endl;
	
	return 0;
	}

Compare operators

Parameters of these operators are lhs and rhs

std::pair<int, int> p1 = std::make_pair(1, 2);
std::pair<int, int> p2 = std::make_pair(2, 2);

if (p1 == p2) {
    std::cout << "equals";
} else {
    std::cout << "not equal"; // statement will show this, because they are not identical
}

Another example with containers of pairs. It uses operator< because it needs to sort container.

#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
 
int main()
{
    std::vector<std::pair<int, std::string>> v = { {2, "baz"},
                                                   {2, "bar"},
                                                   {1, "foo"} };
    std::sort(v.begin(), v.end());
 
    for (const auto& p: v) {
        std::cout << "(" << p.first << "," << p.second << ") ";
        //output: (1,foo) (2,bar) (2,baz)
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


std::pair:

Table Of Contents
8 Arrays
11 Loops
25 std::pair
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype