在用户定义类型 C++ 的容器上使用 std::find
using std::find on a container of a user-defined type c++
我试图编写一个搜索函数来通过起诉std:find来获取std::list中的元素。但是我卡在查找argorithm的第三个参数中,关于这个家伙如何在stl列表中搜索元素?我确实重载了运算符 == 几乎,但它似乎仍然不适用于 std::find。
这是我的代码:
class Node
{
string word;
int count;
public:
Node(string _word) :word(_word), count(0) {}
~Node() {}
const string& getWord() const{
return word;
}
bool operator == (const Node& other) const {
return (this->word.compare(other.word) == 0);
}
};
const Node &getNode(const list<Node> &nodes, const string &word){
list<Node>::iterator itr;
itr = find(nodes.begin(), nodes.end(), new Node(word)); <-- no viable overload '='
return *itr;
}
我现在对这个问题非常疯狂,请给我一些提示。谢谢
要使代码正常工作,只需从sort
中删除new
叫。但是,这不会使您的代码变得更好。
您不会检查是否实际找到该元素,而只是取消引用迭代器。如果未找到该元素,则这是未定义的行为。
现在,如何解决这个问题。
- 不要提供此功能。如果
Node
的用户有一个列表,她应该完全有能力自己调用std::sort
。
你 - 不想写包装的样板,你也不需要。由于单参数构造函数采用
string
,您的类可以从std::string
转换(不过,这应该通过引用获取字符串)。所以你可以写std::find(begin(nodes), end(nodes), "foobar");
. - 您还可以将构造函数标记为显式(大多数时候不需要转换行为),然后只需添加两个空闲
operator==(const Node&, const std::string&)
和operator==(const std::string&, const Node&)
。
无论如何。从标头中删除using namespace std;
。
您有两个主要问题:
-
首先,您的
find
调用是查找指向Node
的指针。new
分配内存并返回指针。你想要的是没有new
的确切文本。itr = find(nodes.begin(), nodes.end(), /*new*/ Node(word));
另请注意,您可以改用
word
,因为您为构造函数提供了一个字符串参数,因此它将被隐式转换。不过,这通常弊大于利,您最好将构造函数声明为explicit
。explicit Node(string _word) :word(_word), count(0) {} //need Node("hi")
这将导致将来不那么令人困惑的错误。默认情况下保持显式打开是个好主意。
-
其次,你的主要问题。您的函数返回一个
const string &
。您的迭代器类型为list<Node>::iterator
。这些不匹配。return *itr; //not const
你需要的是这个:
list<Node>::const_iterator itr; //create a constant iterator instead
其余的可以保持不变,它应该有效(或者至少它对我有用)。
相关文章:
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 为什么在向量上使用 std::find() 时会出现错误?
- 使用 std::find 时没有匹配的函数调用错误
- STL std::find() C++
- 如何将 std::find() 与 2d std: 数组一起使用?
- 如果迭代器没有因插入而无效,则使用std::find和C::insert()是线程安全的
- 未按值排序的std::find和std::any_of
- 在libc++和libstdc++之间的std::map上使用std::find时的实现差异
- 有没有办法在Cython中导入std::find find_if等?
- 由 std::find() 返回的迭代器不可取消引用
- C++ std::find() 寻址返回向量的类函数时的意外行为
- 在 std::find 中使用来自不同命名空间的运算符
- 使用 std::find 和用户提供的谓词 - 自制的 find() 重载
- 如何使用 std::find 找到元素的所有位置?
- 使用带有反向迭代器的std :: find()
- C++ STL: std::find with std::map
- std::find是否隐式修复无效参数
- 如何在 std::vector 内的自定义对象上使用 std::find
- std::find on empty vector 是否会导致未定义的行为
- 使用 std::find 在向量中定位随机生成的数字