关于set
  • 板块学术版
  • 楼主zjpwlyw
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/11/18 21:40
  • 上次更新2024/11/19 10:36:43
查看原帖
关于set
807557
zjpwlyw楼主2024/11/18 21:40

求教,为什么下面这两段代码输出不一样。

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
2024/11/18 21:40
加载中...