How can I make find() work with a set of structs?(如何使 find() 与一组结构一起工作?)
问题描述
我正在使用 set
来保存包含多个字符串的结构.我希望能够使用集合的 find()
功能.但是,由于该集合包含结构,因此它不起作用.我希望 find()
只查看结构中的一个字符串.如何做到这一点?
I am using a set
to hold structs which contain several strings. I want to be able to use the find()
functionality of sets. However, since the set is holding structs, it doesn't work. I want find()
to look only at one of the strings in the struct. How can this be done?
这是我尝试使用的代码.除了使用 find()
的部分外,它工作正常:
Here's the code that I tried to use. It works fine except for the part where find()
is used:
#include <iostream>
#include <string>
#include <set>
using namespace std;
struct test
{
string key;
string data;
};
bool operator<(const test & l, const test & r)
{
return l.key < r.key;
}
bool operator==(const test & l, const test & r)
{
return l.key == r.key;
}
set<test> s;
int main()
{
test newmember;
newmember.key = "key";
newmember.data = "data";
s.insert(newmember);
s.find("key");
}
以下是我尝试编译时收到的错误消息:
Here are the error messages that I get when I try to compile it:
test.cpp:30:7: error: no matching member function for call to 'find'
s.find("key");
~~^~~~
In file included from test.cpp:3:
In file included from /usr/include/c++/4.2.1/set:65:
/usr/include/c++/4.2.1/bits/stl_set.h:429:7: note: candidate function not viable: no known conversion from 'const char [4]' to 'const key_type' (aka 'const test') for 1st argument
find(const key_type& __x)
^
/usr/include/c++/4.2.1/bits/stl_set.h:433:7: note: candidate function not viable: no known conversion from 'const char [4]' to 'const key_type' (aka 'const test') for 1st argument
find(const key_type& __x) const
^
1 error generated.
推荐答案
我建议你把 operator<
和 operator==
放到你的结构体中,而不是重载全局操作符,我觉得它更干净;示例:
I suggest you operator<
and operator==
to your struct instead of overloading the global operator, I find it much cleaner; example:
struct test
{
string key;
string data;
bool operator<(const test& rhs) const
{
return key < rhs.key;
}
bool operator==(const test& rhs) const
{
return key == rhs.key;
}
};
现在回到你真正的问题 - 你正在将一个字符串传递给 find()
函数,但它只接受 test
类型的结构.为此,添加一个用于自动转换的构造函数,因此最终的结构如下所示:
Now on to your real problem - your are passing a string to the find()
function, but it only accepts structs of type test
. In order to do so, add a constructor for automatic conversion, so the final struct would look like this:
struct test
{
string key;
string data;
test(const std::string& strKey = "", const std::string& strData = "")
: key(strKey),
data(strData) {}
bool operator<(const test& rhs) const
{
return key < rhs.key;
}
bool operator==(const test& rhs) const
{
return key == rhs.key;
}
};
然后将字符串传递给 find()
将自动调用构造函数并创建一个仅包含相关键的临时 test
结构.请注意,在这种特殊情况下,构造函数不得声明为 explicit
.
Then passing a string to find()
would automatically call the constructor and create a temporary test
struct containing only the relevant key. Note that in this special case, the constructor must not be declared explicit
.
这篇关于如何使 find() 与一组结构一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 find() 与一组结构一起工作?
基础教程推荐
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01