Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add std::pair with + operator

Tags:

c++

stl

Is there a simple way to make a+b work in the following example:

#include <utility>
#include <iostream>

int main ()
{
    std::pair<int, int> a=std::make_pair(1,2);
    std::pair<int, int> b=std::make_pair(3,3);
    std::pair<int, int> c = a+b;

    return 0;
}
like image 311
titus Avatar asked Feb 22 '14 15:02

titus


People also ask

Can you add pairs in C++?

Right now it supports adding pairs where the first and second are different types, but the two pairs and the return must have the same type. In c++14 you might be able to get away with auto instead of the trailing return type, if you explicitly return a pair.

How do you input a pair in C++?

Syntax: Pair_name = make_pair (value1,value2); CPP.

How does std :: pair work?

std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements. If neither T1 nor T2 is a possibly cv-qualified class type with non-trivial destructor, or array thereof, the destructor of pair is trivial.

How do you create an array of pairs in C++?

The first element of pair Arr1 is sorted with the pair elements of pair “Arr2”. In the main function, we have initialized the values for pair array “Arr1” and pair array “Arr2”. These sorted arrays and the original pairs array will be displayed by using the cout command.


2 Answers

template <typename T,typename U>                                                   
std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {   
    return {l.first+r.first,l.second+r.second};                                    
}                                                                                  
int main ()                                                                        
{                                                                                  
    std::pair<int, int> a=std::make_pair(1,2);                                     
    std::pair<int, int> b=std::make_pair(3,3);                                     
    std::pair<int, int> c = a+b;                                                   

    return 0;                                                                      
}  

You can also do this with more template types to support adding two different types. Right now it supports adding pairs where the first and second are different types, but the two pairs and the return must have the same type.

If you want to make the function really versatile you could do this

template <typename T,typename U, typename V,typename W>                            
auto operator+(const std::pair<T,U> & l,const std::pair<V,W> & r)                  
-> std::pair<decltype(l.first+r.first),decltype(l.second+r.second)>                
{                                                                                  
    return {l.first+r.first,l.second+r.second};                                    
} 

In c++14 you might be able to get away with auto instead of the trailing return type, if you explicitly return a pair.

like image 77
aaronman Avatar answered Oct 16 '22 19:10

aaronman


You can define an override for the binary + operator, specialized for pair<int, int> parameters:

std::pair<int, int> operator +(const std::pair<int, int>& x, const std::pair<int, int>& y) {
    return std::make_pair(x.first + y.first, x.second + y.second);
}
like image 38
buc Avatar answered Oct 16 '22 18:10

buc