Overloading operator== for pair<>

T

Tom Sz.

Hello.
I was experimenting a bit and wrote something but the result is not
what I expected.
Can someone tell me why the code below doesn't display "Found".

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;


bool operator== (const pair<int, int>& l, const pair <int, int>& r)
{
return l.first == r.first;
}

int main()
{
vector<pair<int, int> > vec;
vec.push_back(pair<int, int>(0, 0));
vec.push_back(pair<int, int>(0, 1));
vec.push_back(pair<int, int>(1, 1));
vec.push_back(pair<int, int>(2, 1));

pair<int, int> p(2, 2);

vector<pair<int, int> >::iterator it = find(vec.begin(), vec.end(),
p);
if (it != vec.end()) cout << "Found" << endl;
}
 
A

AnonMail2005

Hello.
I was experimenting a bit and wrote something but the result is not
what I expected.
Can someone tell me why the code below doesn't display "Found".

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool operator== (const pair<int, int>& l, const pair <int, int>& r)
{
  return l.first == r.first;

}

int main()
{
   vector<pair<int, int> > vec;
   vec.push_back(pair<int, int>(0, 0));
   vec.push_back(pair<int, int>(0, 1));
   vec.push_back(pair<int, int>(1, 1));
   vec.push_back(pair<int, int>(2, 1));

   pair<int, int> p(2, 2);

   vector<pair<int, int> >::iterator it = find(vec.begin(), vec.end(),
p);
   if (it != vec.end()) cout << "Found" << endl;

}

std::pair is a template. Somehow, your definition is not being
recognized but the one defined by std::pair seems to be.

Technically, I don't know why, but it probably has something to do
with proper partial specialization of a template class.

HTH
 
P

Paul Bibbings

Tom Sz. said:
Hello.
I was experimenting a bit and wrote something but the result is not
what I expected.
Can someone tell me why the code below doesn't display "Found".

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;


bool operator== (const pair<int, int>& l, const pair <int, int>& r)
{
return l.first == r.first;
}

Here you are providing a custom op== for a standard library type,
std::pair. You should first of all be aware, if you are not already,
that one such op== already exists, declared as:

namespace std {
template <class T1, class T2>
bool operator==(const pair<T1, T2>&, const pair<T1, T2>&);
}

As this operator is declared in namespace std together with std::find,
it will be found and used by the latter and your own custom operator
will not be considered.

(To illustrate this, wrap your own custom operator in namespace std,
whereupon /it/ will be preferred as a non-template function. However,
this is by way of illustration only and should not be considered as a
solution to your problem.)
int main()
{
vector<pair<int, int> > vec;
vec.push_back(pair<int, int>(0, 0));
vec.push_back(pair<int, int>(0, 1));
vec.push_back(pair<int, int>(1, 1));
vec.push_back(pair<int, int>(2, 1));

pair<int, int> p(2, 2);

vector<pair<int, int> >::iterator it = find(vec.begin(), vec.end(),
p);
if (it != vec.end()) cout << "Found" << endl;
}

What you need here is probably a predicate and std::find_if. Something
like:

template<typename T1, typename T2>
class eq_first {
public:
eq_first(std::pair<T1, T2> p)
: p_(p)
{ }
bool operator()(std::pair<T1, T2> p)
{ return p.first == p_.first; }
private:
std::pair<T1, T2> p_;
};

// ...

std::vector<std::pair<int, int> >::iterator it =
std::find_if(vec.begin(), vec.end(), eq_first<int, int>(p));

Regards

Paul Bibbings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top