求教,为什么下面这两段代码输出不一样。
test1:
#include <bits/stdc++.h>
using namespace std;
int b[114];
struct Comp {
bool operator () (int x, int y) {
return b[x] < b[y];
}
};
set <int, Comp> S;
void show() {
cout << "item : ";
for(auto x : S) cout << x << ' ';
cout << endl;
}
int main() {
b[1] = 1, b[2] = 2;
S.insert(1);
show();
S.insert(2);
show();
b[1] = 3;
show();
auto it = S.lower_bound(1);
S.erase(it);
show();
S.insert(1);
show();
}
output1:
item : 1
item : 1 2
item : 1 2
item : 2
item : 2 1
test2
#include <bits/stdc++.h>
using namespace std;
int b[114];
struct Comp {
bool operator () (int x, int y) {
return b[x] < b[y];
}
};
set <int, Comp> S;
void show() {
cout << "item : ";
for(auto x : S) cout << x << ' ';
cout << endl;
}
int main() {
b[1] = 1, b[2] = 2;
S.insert(1);
show();
S.insert(2);
show();
b[1] = 3;
show();
//auto it = S.lower_bound(1);
S.erase(1);
show();
S.insert(1);
show();
}
output2:
item : 1
item : 1 2
item : 1 2
item :
item : 1