Add std :: pair with + operator

Is there an easy way to make + 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;
}
+3
source share
4 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 the addition of 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 be of the same type.

If you want to make the function truly universal, you can do it

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 can get away from auto instead of returning return type if you explicitly return a pair.

+9
source

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

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);
}
+2
source

Try this code:

#include <utility>
#include <iostream>
namespace
{
    std::pair<int,int> operator+(const std::pair<int,int> &a ,const  std::pair<int,int> &b )
    {
        return std::make_pair(a.first+b.first,a.second+b.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;

    std::cout<<c.first<<"  "<<c.second;

    return 0;
}
+1
source

yes, you can overload operator + as follows:

#include <utility>
#include <iostream>

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);
}

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;

    std::cout << "c= ("<<c.first<<", "<<c.second<<")"<<std::endl;

    return 0;
}

will provide you with the following:

./a.out 
c= (4, 5)
+1
source

All Articles