Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ overloading operator[] for std::pair

I work a lot with pairs of values: std::pair<int, int> my_pair. Sometimes I need to perform the same operation on both my_pair.first and my_pair.second.

My code would be much smoother if I could do my_pair[j] and loop over j=0,1. (I am avoiding using arrays because I don't want to bother with allocating memory, and I use pair extensively with other things).

Thus, I would like to define operator[] for std::pair<int, int>.

And I can't get it to work, (I'm not very good with templates and such)...

#include <utility>
#include <stdlib.h>

template <class T1> T1& std::pair<T1, T1>::operator[](const uint &indx) const
{
  if (indx == 0)
    return first;
  else
    return second;
};

int main()
{
// ....
return 0;
}

fails to compile. Other variations fail as well.

As far as I can tell, I am following the Stack Overflow operator overloading FAQ, but I guess I am missing something...

like image 543
cmo Avatar asked Dec 22 '22 01:12

cmo


2 Answers

  1. you cannot overload operator[] as a non-member
  2. you cannot define a member function which has not been declared in the class definition
  3. you cannot modify the class definition of std::pair

Here's a non-member implementation:

/// @return the nth element in the pair. n must be 0 or 1.
template <class T>
const T& pair_at(const std::pair<T, T>& p, unsigned int n)
{
    assert(n == 0 || n == 1 && "Pair index must be 0 or 1!");
    return n == 0 ? p.first: p.second;
}

/// @return the nth element in the pair. n must be 0 or 1.
template <class T>
T& pair_at(std::pair<T, T>& p, unsigned int index)
{
    assert(index == 0 || index == 1 && "Pair index must be 0 or 1!");
    return index == 0 ? p.first: p.second;
}

// usage:
pair<int, int> my_pair(1, 2);
for (int j=0; j < 2; ++j)
    ++pair_at(my_pair, j);

Note that we need two versions: one for read-only pairs and one for mutable pairs.

Don't be afraid to use non-member functions liberally. As Stroustrup himself said, there is no need to model everything with an object or augment everything through inheritance. If you do want to use classes, prefer composition to inheritance.

You can also do something like this:

/// Applies func to p.first and p.second.
template <class T, class Func>
void for_each_pair(const std::pair<T, T>& p, Func func)
{
    func(p.first);
    func(p.second);
}

/// Applies func to p.first and p.second.
template <class T, class Func>
void for_each_pair(std::pair<T, T>& p, Func func)
{
    func(p.first);
    func(p.second);
}

// usage:
pair<int, int> my_pair(1, 2);
for_each_pair(my_pair, [&](int& x){
    ++x;
});

That isn't too unwieldy to use if you have C++11 lambdas and is at least a bit safer since it has no potential to access out of bounds.

like image 163
stinky472 Avatar answered Dec 24 '22 01:12

stinky472


You cannot add functions to an existing class like this. And you certainly can't do it to things in the std namespace.

So you should define your own wrapper class:

class MyPair {
private:
    std::pair<int,int> p;

public:
    int &operator[](int i) { return (i == 0) ? p[0] : p[1]; }
    // etc.
};
like image 29
Oliver Charlesworth Avatar answered Dec 24 '22 01:12

Oliver Charlesworth