[英]Error while finding an object in std::set
I am trying to find an object inside the set using the find() method. 我正在尝试使用find()方法在集合内找到一个对象。 However, I am unable to do that it seems I am sending a wrong key. 但是,我似乎无法发送错误的密钥。 What would be right way to do it? 什么是正确的方法?
#include <iostream>
#include <set>
#include <utility>
struct Class {
std::pair<int,int> data;
int val = 0;
int id = 0; ///ID for the object
};
struct CompClass {
bool operator() (const Class& lhs, const Class& rhs) const
{
if (lhs.data.first == rhs.data.first) return lhs.data.second < rhs.data.second;
return lhs.data.first<rhs.data.first;
}
};
int main ()
{
Class c1,c2,c3,c4;
c1.val = 92;c1.id = 2; c1.data = std::make_pair(92,2);
c2.val = 94;c2.id = 3; c2.data = std::make_pair(94,3);
c3.val = 92;c3.id = 1; c3.data = std::make_pair(10,1);
std::set<Class,CompClass> fifth; // class as Compare
fifth.insert(c1);fifth.insert(c2);fifth.insert(c3);
for (auto x: fifth) {std::cout << x.id << " " << x.val << std::endl;}
if (fifth.find( std::make_pair(92,2) ) != fifth.end() ) {std::cout << "Got it";} //Error
return 0;
}
std::set<T>::find
takes an argument of type T
in C++11. std::set<T>::find
在C ++ 11中采用T
类型的参数。 You need to provide a Class
object, not a std::pair
: 您需要提供一个Class
对象,而不是std::pair
:
if (fifth.find( Class{92,2,0,0} ) != fifth.end() ) {std::cout << "Got it";}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.