C++ 向有序集插入结构元素失败

C++ 向有序集插入结构元素失败,c++,insert,set,C++,Insert,Set,我需要将元素结构插入到集合中,如下所示: // In hpp file at private part of a class: struct BestPair { unsigned int p1; unsigned int p2; double goodness; bool operator<(BestPair other) const // Set descendent order

我需要将元素结构插入到集合中,如下所示:

// In hpp file at private part of a class:
struct BestPair {
        unsigned int p1;
        unsigned int p2;
        double goodness;

        bool operator<(BestPair other) const                      // Set descendent order.                                    
        {
            return goodness > other.goodness;
        }
    };
//在类的私有部分的hpp文件中:
结构最佳配对{
无符号整数p1;
无符号整数p2;
双重善;
bool算子的其他性质;
}
};
集合应按子代顺序排列

// at the cpp file, inside a method of the same class
void Pairs::fillGlobalStack (double *** source, unsigned int sz)
{
    BestPair bp;

    for (unsigned int i = 0; i != sz; ++i) {
        for (unsigned int j = i+1; j != sz; ++j) {
            bp.p1 = i;        
            bp.p2 = j;
            bp.goodness = (* source) [i][j];
            global_stack.insert (bp);                          // Insert into global stack.                                   
            if (debug) {
                cout << "[fillGlobalStack] i: " << i << "  j: " << j << "  goodness: " << bp.goodness << "  global_stack.size\
() " << global_stack.size() << endl;
            }
        }
    }
}
//在cpp文件中,在同一类的方法中
空对::fillGlobalStack(双***源,无符号整数sz)
{
最佳配对bp;
for(无符号整数i=0;i!=sz;++i){
for(无符号整数j=i+1;j!=sz;++j){
bp.p1=i;
bp.p2=j;
bp.goodness=(*来源)[i][j];
全局_stack.insert(bp);//插入全局堆栈。
如果(调试){

cout如果两个元素具有相等的
goodness
,则认为它们相等,并且不能存储在
set
中。如果希望允许重复,请使用
multiset

通常,如果
a
b

如果您不希望允许完全重复,但允许
goodness
-重复,则应在
goodness
相等的情况下添加任何排序,例如

bool operator<(const BestPair& other) const
{
    return goodness > other.goodness || goodness == other.goodnes && p1 < other.p1 || goodness == other.goodness && p1 == other.p1 && p2 < other.p2;
}
bool操作符other.goodness | | goodness==other.goodness&&p1
如果两个元素具有相等的
优度
,则认为它们相等,并且不能存储在
集合
中。如果希望允许重复,请使用
多集合

通常,如果
a
b

如果您不希望允许完全重复,但允许
goodness
-重复,则应在
goodness
相等的情况下添加任何排序,例如

bool operator<(const BestPair& other) const
{
    return goodness > other.goodness || goodness == other.goodnes && p1 < other.p1 || goodness == other.goodness && p1 == other.p1 && p2 < other.p2;
}
bool操作符other.goodness | | goodness==other.goodness&&p1
它不应该是三,因为第三个元素等于第二个元素(两个元素的优度都为0.794)集合不插入重复。也许你需要<代码> STD::MultSuth。但是很难确定。我不认为任何类型的集合都是一个很好的栈实现。

< P>它不应该是三,因为你的第三个元素等于你的第二个元素(两者都有善良0.794)。SET不插入重复。也许您需要<代码> STD::MultSuth但是很难确定。我不认为任何类型的集合都是一个很好的堆栈实现。

@ WOZKRAIG-它需要在某种意义上说它可以更高效,但这并不影响代码的含义。你在那里做了什么,那就是邪恶:)代码>operator@PeteBecker我从来没有说过,因此“旁注:”而不是“这是你的问题…”但也许我以后会克制,只直接回答问题;这是一个公平的观点。@WhozCraig-你的评论是合理的,但需要一点限定。我通常在开始评论时说“这并没有解决你的问题,但是…”.@PeteBecker和“旁注”:这不是一个足够的方法吗?不管怎样,避免未来的情况默认会解决它,所以不用担心。@WhozCraig-它需要这样做,因为它可以更有效,但这不会影响代码的含义。你在那里做的,那是邪恶的:)
operator@PeteBecker我从来没有声称是这样,因此“旁注:”而不是“这是你的问题…”但也许我以后会克制,只直接回答问题;这是一个公平的观点。@WhozCraig-你的评论是合理的,但需要一点限定。我通常在开始评论时会说“这并不能解决你的问题,但是…”。@PeteBecker和“旁注:"这不是一个足够的方法吗?不管怎样,避免未来的情况默认会解决它,所以不用担心。我认为具有相同的优度但p1和p2不同的元素在集合中永远不会被认为是相等的。谢谢!我认为具有相同的优度但p1和p2不同的元素在集合中永远不会被认为是相等的f或者是布景,谢谢!