简体   繁体   English

如何在结构中重载'=='运算符?

[英]How to overload '==' operator within a struct?

I have a struct which I wish to use to remove items from a vector, I do not know what index the item will appear at so I'm removing it by value. 我有一个要用于从向量中删除项目的结构,我不知道该项目将出现在什么索引上,因此我要按值删除它。 Here is my struct: 这是我的结构:

struct IsUnderScore{
bool operator()(char c){
    return c=='_';
}
};

I can use this struct to remove values from strings but when I try to use on a vector like so: 我可以使用此结构从字符串中删除值,但是当我尝试在像这样的向量上使用时:

abc.erase(std::remove(abc.begin(), abc.end(), IsUnderScore()), abc.end());

I get a compiler error saying there is no match for the == operator. 我收到一个编译器错误,指出==运算符不匹配。

So I know I have to overload this operator itself, and in looking at some of the other implementations of it throughout SO, can't really find an implementation that matches my case, most other versions have variables declared in their structs but this is a simple bool to see if the current character matches underscore. 因此,我知道我必须重载此运算符本身,并且在整个SO中查看它的其他实现时,无法真正找到与我的情况匹配的实现,大多数其他版本在其结构中声明了变量,但这是一个简单的布尔值,以查看当前字符是否与下划线匹配。 I'm kind of confused as how I should build my overload to match my case. 我有点困惑,因为我应该如何建立过载以匹配我的情况。

EDIT : The vector in question is a vector string by the way. 编辑 :顺便说一句,所述向量是向量字符串。

You use std::remove - it does not use the comparator as the third argument, but just the value to remove. 您使用std::remove它不使用比较器作为第三个参数,而只是使用要删除的值。 Assuming that your abc vector is vector<char> use 假设您的abc向量是vector<char>使用

std::remove(abc.begin(), abc.end(), '_')

The problem is not with a missing operator== , but with using the wrong function+argument pair. 问题不在于缺少operator== ,而在于使用了错误的function + argument对。 remove() takes an item as the 3rd argument, not a predicate. remove()项目作为第三个参数,而不是谓词。 For a predicate, you need remove_if . 对于谓词,您需要remove_if So do one of these: 因此,请执行以下操作之一:

std::remove(abc.begin(), abc.end(), '_')

// or

std::remove_if(abc.begin(), abc.end(), IsUnderScore())

EDIT 编辑

The 3rd parameter of remove is of the type of the iterator's value type. remove的第三个参数属于迭代器的值类型。 The parameter of the predicate used in remove_if takes the same type. remove_if使用的谓词参数采用相同的类型。 Based on your edit, this is std::string in your case, so you must use it accordingly. 根据您的编辑,在您的情况下,这是std::string ,因此您必须相应地使用它。 Either use remove with a string: 使用带有字符串的remove

std::remove(abc.begin(), abc.end(), "_")

// or 

std::remove(abc.begin(), abc.end(), std::string("_"))

Or update the predicate for use with remove_if : 或更新谓词以与remove_if一起使用:

struct IsUnderScore{
  bool operator()(const std::string &s){
    return s == "_";
  }
};

// ...

std::remove_if(abc.begin(), abc.end(), IsUnderScore())

You are trying remove instead remove_if . 您正在尝试使用remove代替remove_if (Or you are trying remove with a functor, instead a char). (或者您尝试使用函子而不是char进行remove )。

The best and simple solution: 最佳和简单的解决方案:

abc.erase(std::remove(abc.begin(), abc.end(), '_'), abc.end());

The alternative (not too useful here, but just in case you need a more complex comparison to determine what element to remove): 替代方法(在这里不太有用,但是以防万一,您需要进行更复杂的比较以确定要删除的元素):

abc.erase_if(std::remove_if(abc.begin(), abc.end(), IsUnderScore()), abc.end());

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

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