我需要创建一组自定义项目:
在这个例子中,我使用std::set和一个重载的operator<( )。据我所知,std::set使用operator<( )进行排序和唯一性检查。但是我的代码失败了,因为我能够插入具有重复名称的项目:
#include <iostream>
#include <set>
struct Item
{
std::string name;
int priority;
};
bool operator<( const Item& a, const Item& b )
{
// Always returns false if names are equal.
// Set should consider elements equal when a < b is false and b < a is also false.
if( a.name == b.name )
return false;
return ( a.priority < b.priority );
}
int main()
{ …Run Code Online (Sandbox Code Playgroud) c++ ×1