I am trying to find an object inside the set using the find() method. 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. You need to provide a Class
object, not a std::pair
:
if (fifth.find( Class{92,2,0,0} ) != fifth.end() ) {std::cout << "Got it";}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.