Iss*_* T. 6 c++ stl set comparator
我无法解释插入新元素时std :: set所做的比较次数.这是一个例子:
对于此代码
struct A {
int i = 0;
bool operator()(int a, int b)
{
++i;
return a < b;
}
};
int main()
{
A a;
set<int, A> s1(a);
s1.insert(1);
cout << s1.key_comp().i << endl;
s1.insert(2);
cout << s1.key_comp().i << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是
0
3
Run Code Online (Sandbox Code Playgroud)
为什么插入第二个元素需要进行3次比较?O_O
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |