C++ std::set<;背后的直觉是什么;键、比较、分配器>;::使用'<';运算符而不是'==';操作人员
为什么一组自定义类(比如Person)上的find()函数调用不等式运算符“C++ std::set<;背后的直觉是什么;键、比较、分配器>;::使用'<';运算符而不是'==';操作人员,c++,stl,c++14,C++,Stl,C++14,为什么一组自定义类(比如Person)上的find()函数调用不等式运算符“”,因为std::find使用的是等价而不是相等。等价使用运算符要求可以对集合中键的元素进行排序,即通过根据某种标准对它们进行比较,以某种顺序一致地放置。如果唯一的比较方法是==,则不可能这样做。考虑一个有三个键的集合,“A”/代码>代码> B“,和 C”< /代码>。比较“A”==“B”,“A”==“C”,和“B”==“C”都将比较为false-这完全不提供有关如何将它们按任何顺序排序的信息。然而,进行比较时,“A”
”,因为std::find
使用的是等价而不是相等。等价使用运算符要求可以对集合中键的元素进行排序,即通过根据某种标准对它们进行比较,以某种顺序一致地放置。如果唯一的比较方法是==
,则不可能这样做。考虑一个有三个键的集合,<代码>“A”/代码>代码> B“,和<代码> C”< /代码>。比较“A”==“B”
,“A”==“C”
,和“B”==“C”
都将比较为false-这完全不提供有关如何将它们按任何顺序排序的信息。然而,进行比较时,“A”<“B”
,“A”<“C”
,“B”<“C”
确实给出了如何对它们进行排序的信息。除了排序之外,请注意等价性是比相等性更弱的条件,而要求平等的定义将是不必要的限制。在老年退休金计划的情况下,这也意味着每个年龄组中不能有超过一个人。所以std::set s{{“Larry”,3},{“Moe”,3}代码>将有大小1不为2(因为代码< > N314159:尽管可以通过将<代码>名称<代码>合并到比较中来解决,但是提供了一个总排序。在现代C++中也很容易有效地实现,这得益于 STD::TIA 提供了一个零拷贝的方式来执行字典排序:只需替换<代码>操作符。
#include<iostream>
#include<stdio.h>
#include<string>
#include<set>
using namespace std ;
class Person {
friend ostream & operator<<(ostream &os , const Person p) ;
string name ;
int age;
public :
Person()
:name{"Unknown"}, age{0}{
}
Person(string name , int age )
:name{name}, age{age}
{
}
//OVERLOADED operators
bool operator<(const Person &rhs) const;
bool operator ==(const Person &rhs) const;
};
bool Person::operator<(const Person &rhs) const{
cout<<" < operator called"<<endl;
return this->age < rhs.age;
}
bool Person::operator==(const Person &rhs) const{
cout<<"Equality operator"<<endl;
return (this->age == rhs.age && this->name == rhs.name);
}
ostream & operator<<( ostream &os , const Person p ){
os<<p.name <<":"<<p.age<<endl;
return os;
}
template<class T>
void display(const set<T> &s1){
for (const auto &temp : s1){
cout<<temp <<" ";
}
cout<<endl;
}
void test2(){
cout<<"====================TEST2=========="<<endl;
set<Person> stooges {
{"Larry",2},
{"Moe",1},
{"Curly",3},
};
cout<<"Something random "<<endl;
auto it = stooges.find(Person{"Moe",1}); //Calls the '<' operator
}
int main(){
test2();
return 0;
}
====================TEST2==========
< operator called
< operator called
< operator called
< operator called
< operator called
Something random
< operator called
< operator called
< operator called
Hit any key to continue...
!(a < b) && !(b < a)