简体   繁体   English

std :: find如何运作? 运算符==

[英]std::find how does it work? operator==

If I have a class 如果我有课

class Point
{
public:
  Point() {}
  Point(int _col, int _row) : row(_row), col(_col) {}
  int row, col;
};

how can I use std::find() to check whether the point is already in vector? 如何使用std :: find()检查该点是否已经在向量中? DO I have to overload operator== ? 我必须重载operator ==吗?

I am trying to do this 我正在尝试这样做

#include <algorithm>

if(std::find(v.begin(), v.end(), x) != v.end()) {
    /* v contains x */
} else {
    /* v does not contain x */
}

Almost every answer I find on Stack Overflow suggest using find to check whether the object is in std::vector but none of them explains whether it compares the pointer of objects or the actual values of the object. 我在Stack Overflow上找到的几乎所有答案都建议使用find检查对象是否在std::vector但是它们都不能解释它是比较对象的指针还是对象的实际值。

The C++ standard (draft N3242) says (in section 25.2.5 [alg.find]) that std::find : C ++标准(草案N3242)(在第25.2.5节[alg.find]中)说std::find

Returns : The first iterator i in the range [first,last) for which the following corresponding conditions hold: *i == value [...]. 返回 :范围为[first,last)的第一个迭代器i ,其符合以下条件: *i == value [...]。 Returns last if no such iterator is found. 如果找不到这样的迭代器,则返回last

Your question of whether it will search based on the value or the address of the object depends on how operator== is implemented. 您是否根据对象的值或地址进行搜索的问题取决于operator==的实现方式。 The simple answer is: std::find will return an iterator to the object for which operator== returned true. 简单的答案是: std::find将迭代器返回到operator==返回true的对象。

Usually, this will just be a value-based comparison (because operator== is usually implemented to compare the values of two objects), and so you should generally expect std::find to search the range for the value you've provided (not the address of the object you provided). 通常,这只是基于值的比较(因为通常使用operator==来比较两个对象的值),因此通常应该期望std::find在范围内搜索您提供的值(而不是您提供的对象的地址)。

It's possible for operator== to be implemented such that it compares based on address , like so: 可以实现operator== ,使其基于address进行比较 ,如下所示:

bool operator==(const Point& left, const Point& right) {
    return &left == &right;
}

Using this operator== will compare addresses, and so std::find will search for an object that has the same address as the one you've provided. 使用该operator==将比较地址,因此std::find将搜索与您提供的地址相同的对象。 It's generally a bad idea to implement operator== like this, though. 但是,像这样实现operator==通常是一个坏主意。 Most people would implement operator== like so: 大多数人会这样实现operator==

bool operator==(const Point& left, const Point& right) {
    return left.row == right.row && left.col == right.col;
}

which, when used with std::find , will compare Point s based on their values . 当与std::find ,它将根据Point的值比较Point

Unless your types are PODs fundamental types, you will need to provide an equality function, member or not. 除非您的类型是POD的基本类型,否则您将需要提供一个等于或不等于成员的相等函数。

There are two fundamental versions of std::find , one that assumes an equality operator and the other uses an equality function you supply. std::find有两个基本版本,一个假定相等运算符,另一个使用您提供的相等函数。

I recommend that you add operator== and operator< to any class that will be compared for equality or ordered. 我建议您将operator==operator<添加到将被比较相等性或有序的任何类。

Here's an updated version of your class: 这是您课程的更新版本:

class Point
{
  int x;  // These are private by default.
  int y; 
  public:  
    Point(int new_x, int new_y) : x(new_x), y(new_y)
    { ; }
    bool operator==(const Point& p) const
    {
      return (x == p.x) && (y == p.y);
    }
};

The member method operator== allows comparison without exposing the values to friends or the public. 成员方法operator==允许进行比较,而不会向朋友或公众公开这些值。

If you want to use a free standing comparison function, you will need to either make the values public or make the function a friend: 如果要使用独立式比较功能,则需要将值公开或将该功能设为朋友:

class Point
{
  int x;  // These are private by default.
  int y; 
  public:  
    Point(int new_x, int new_y) : x(new_x), y(new_y)
    { ; }
    friend bool operator==(const Point& a, const Point& b);
};

bool operator==(const Point& a, const Point& b)
{
    return (a.x == b.x) && (a.y == b.y);
}

If you want to use the free standing function with std::find , the example would be: 如果要对std::find使用独立功能,则示例为:

std::vector<Point> point_container;
//...
Point p;
std::vector<Point>::const_iterator iter;
iter = std::find(point_container.begin(), point_container.end(),
                 p,
                 Equal_Points);

Where Equal_Points is a free standing function that can compare the members of two Points. 其中Equal_Points是一个独立功能,可以比较两个Point的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM