我需要将元素结构插入到集合中,如下所示:
// 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;
}
};
该集合应按后代顺序排列。
// 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;
}
}
}
}
但是在运行时,代码从不插入第三个、第四个等元素,这对我来说似乎很奇怪,因为它们是不同的元素。
// The output:
[fillGlobalStack] p1: 0 p2: 1 goodness: 0 global_stack.size() 1
[fillGlobalStack] p1: 0 p2: 2 goodness: 0.794 global_stack.size() 2
[fillGlobalStack] p1: 0 p2: 3 goodness: 0.794 global_stack.size() 2 <-- It should be 3
我究竟做错了什么?如何解决?