如何使 find() 与一组结构一起工作?

2022-01-20 00:00:00 set find containers struct c++

我正在使用 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.

相关文章