问题向量在哪里找到c++ ??

发布于 2024-11-26 20:02:23 字数 419 浏览 1 评论 0原文

当我编译时出现错误,我不明白问题出在哪里?

class Edge{  
public:  
      int nid;  
      bool operator==(const Edge& edge) const {  
            return nid == edge.nid;  
      }  
 };  

这里有

vector<Edge> edges;  
vector<Edge>::iterator it;  
it = find (edges.begin(), edges.end(), nid);

if( it != edges.end() )  
      edges.erase(it); 

什么想法吗?!!!?

when i compile i has an error and i cannot understand where is the problem?

class Edge{  
public:  
      int nid;  
      bool operator==(const Edge& edge) const {  
            return nid == edge.nid;  
      }  
 };  

and problem here

vector<Edge> edges;  
vector<Edge>::iterator it;  
it = find (edges.begin(), edges.end(), nid);

if( it != edges.end() )  
      edges.erase(it); 

any ideas ?!!!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凤舞天涯 2024-12-03 20:02:23

find 会将向量中 Edge 类型的对象与 nid 进行比较(使用 ==)。我猜 nid 的类型是 int ,除非您在 Edge 之间实现 operator== ,否则它将无法工作。和int

您可以尝试:

it = find (edges.begin(), edges.end(), Edge(nid));

find will compare (with ==) the objects of type Edge in the vector with nid. I guess that nid is of type int and that won't work unless you implement operator== between Edge and int.

You can try :

it = find (edges.begin(), edges.end(), Edge(nid));
瞳孔里扚悲伤 2024-12-03 20:02:23

您还没有描述问题的提取症状,但我猜想这与没有定义 operator!=

您可能也想将其定义为

   bool operator!=(const Edge&edge)const {
      return !(*this == edge);
   }

如果您的 find(.. .,nid) 将 nid 作为整数参数,您可能还需要将 == 运算符重载为

   bool operator==(const int&edge)const {
      return this->nid == edge;
   }

You haven't described the extract symptoms of your problem, but I guess that it is related to that didn't define the operator!=

You probably want to define it as

   bool operator!=(const Edge&edge)const {
      return !(*this == edge);
   }

also if your find(...,nid) is taking nid as an integer argument, you probably also need to overload the == operator as

   bool operator==(const int&edge)const {
      return this->nid == edge;
   }
做个ˇ局外人 2024-12-03 20:02:23

您也可以重载运算符!=,或者简单地否定 if 条件。

You can either overload the operator!=, too, or simply negate the if condition.

牵强ㄟ 2024-12-03 20:02:23

假设 nid 与类中的类型相同,我假设您收到有关 find() 第三个参数类型的投诉。我认为你可以将其切换为:

vector<Edge>::iterator it = find (edges.begin(), edges.end(), edges.begin() + nid);

Assuming nid is the same type as in the class I assume you get a complaint about the type of the third argument to find(). I think you can switch it to:

vector<Edge>::iterator it = find (edges.begin(), edges.end(), edges.begin() + nid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文