如何检查C++ std :: vector等的成员资格?

D A*_*ent 0 c++ stl

我在下面的程序中做错了什么?

我想std::find()在容器上使用它来决定它是否包含给定的元素.下面的程序适用于空容器,但不适用于带有元素的容器.

#include <iostream>
#include <vector>
#include <cassert>

struct Pair {int x,y;};

const bool operator==(Pair p, Pair q) {return p.x == p.x && q.y == q.y ;}

typedef std::vector<Pair> p_containr_t;

int main (int argc, char * const argv[]) {
  const Pair start_p = {1,2};
  const Pair second_p = {3,4};
  const Pair other_p = {5,6};
  p_containr_t v;
  p_containr_t::iterator where;

  where = std::find(v.begin(),v.end(),other_p);
  assert(where == v.end());
  std::cout << "OK for empty\n";                     // Program reaches here.

  v.push_back(start_p);
  where = std::find(v.begin(),v.end(),other_p);
  assert(where == v.end());                          // This assertion fails.
  std::cout << "OK for first element\n";

  v.push_back(second_p);
  where = std::find(v.begin(),v.end(),other_p);
  std::cout << "OK for second element\n";      // Fails too (if I edit above).

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*rtz 6

const bool operator==(Pair p, Pair q) {return p.x == p.x && q.y == q.y ;}
Run Code Online (Sandbox Code Playgroud)

仔细看看那条线.