我在下面的代码中创建了一个std::set
,其中元素基于数组进行排序。std :: set元素消失
int weight[] = {0, 1, 58, 21, 10, 21, 24};
struct Comp
{
public:
bool operator() (const int &a, const int &b) const
{
if (weight[a] == weight[b])
return a < b;
else
return weight[a] < weight[b];
}
};
set<int, Comp> s;
令人惊讶地,当我改变任何元件的weight
阵列中,在所述一组相应的元件消失了。这里是我的测试功能:
void print()
{
printf("Elements = ");
for(int i = 1; i <= 6; i++)
if(s.find(i) != s.end())
printf("%2d ", i);;
printf("\n");
}
int main()
{
for(int i = 1; i <= 6; i++)
s.insert(i);
print();
weight[2] = 1;
weight[5] = 15;
print();
return 0;
}
输出:
Elements = 1 2 3 4 5 6
Elements = 1 3 4 6
我使用buntu gcc 4.6.3
。
时间来重新生成一组新的,这是为什么奇怪? 's.find()'使用你的'weight'数组,所以如果你改变它,你应该期待'find'的行为不同。 – 2013-04-08 13:23:40
您正在修改组合后的订购标准。这是行不通的。 – juanchopanza 2013-04-08 13:24:07