C++ 如何确保boost::无序的自定义数据集中没有重复项

C++ 如何确保boost::无序的自定义数据集中没有重复项,c++,boost,stdset,boost-unordered,C++,Boost,Stdset,Boost Unordered,我需要我的容器只包含唯一的元素,所以我的结构如下: class OD { private: std::string key; public: OD(){} OD(const WayPoint &origin, const WayPoint &destination): origin(origin), destination(destination) { std::stringstream str("");

我需要我的容器只包含唯一的元素,所以我的结构如下:

class OD
{
private:
    std::string key;
public:
    OD(){}
    OD(const WayPoint &origin, const WayPoint &destination):
        origin(origin), destination(destination)
    {
        std::stringstream str("");
        str << origin.node_->getID() << "," << destination.node_->getID();
        key = str.str();
    }
    bool operator<(const OD & rhs) const
    {
        return key < rhs.key;
    }
    bool operator()(const OD & rhs, const OD & lhs)
    {
        return rhs < lhs;
    }
};
等级OD
{
私人:
std::字符串键;
公众:
OD(){}
OD(恒定航路点和起点、恒定航路点和目的地):
起点(起点)、终点(终点)
{
std::stringstream str(“”);

str getID()下面是一个为
无序集定义自定义哈希和比较运算符的示例:

#include <iostream>
#include <functional>
#include <unordered_set>

struct X
{
    std::string key_;
};

int main() {
    std::unordered_set<X,
                       std::function<size_t(const X&)>,
                       std::function<bool(const X&, const X&)> > s{
             5, // initial bucket count
             [](const X& x) { return std::hash<decltype(x.key_)>()(x.key_); },
             [](const X& lhs, const X& rhs) { return lhs.key_ == rhs.key_; }
         };
    s.insert({"one"});
    s.insert({"two"});
    s.insert({"three"});
    for (auto& x : s)
        std::cout << x.key_ << '\n';
}
#包括
#包括
#包括
结构X
{
std::字符串键;
};
int main(){
std::无序的_集{
5,//初始桶计数
[](const X&X){return std::hash()(X.key);},
[](常数X&lhs,常数X&rhs){return lhs.key_==rhs.key_;}
};
s、 插入({“一”});
s、 插入({“两”});
s、 插入({“三”});
用于(自动和x:s)

std::cout
set
std::less
)的默认比较器已遵从
的要求,答案传达了所需的信息。
#include <iostream>
#include <functional>
#include <unordered_set>

struct X
{
    std::string key_;
};

int main() {
    std::unordered_set<X,
                       std::function<size_t(const X&)>,
                       std::function<bool(const X&, const X&)> > s{
             5, // initial bucket count
             [](const X& x) { return std::hash<decltype(x.key_)>()(x.key_); },
             [](const X& lhs, const X& rhs) { return lhs.key_ == rhs.key_; }
         };
    s.insert({"one"});
    s.insert({"two"});
    s.insert({"three"});
    for (auto& x : s)
        std::cout << x.key_ << '\n';
}