标签:object bit sea tab -- 迭代器 soc rip src
ITP系列之集合运算
集合并集: ITP2_9_A: Set Union
集合交集: ITP2_9_B: Set Intersection
集合差: ITP2_9_C: Set Difference
集合差异: ITP2_9_D: Set Symmetric Difference
std::set is an associative container that contains a sorted set of unique objects of type Key. Sorting is done using the key comparison function Compare. Search, removal, and insertion operations have logarithmic complexity. Sets are usually implemented as red-black trees.
std :: set是一个关联容器,包含一组有序的Key类型的唯一对象。 使用密钥比较功能比较进行排序。 搜索,删除和插入操作具有对数复杂性。 集合通常实现为红黑树。
Everywhere the standard library uses the Compare requirements, uniqueness is determined by using the equivalence relation. In imprecise terms, two objects a and b are considered equivalent if neither compares less than the other: !comp(a, b) && !comp(b, a).
标准库的每个地方都使用比较要求,唯一性通过使用等价关系来确定。 不精确的说,如果两个对象的比较小于另一个,则认为两个对象a和b是等价的:!comp(a,b)&&!comp(b,a)。
方法名 | 描述 | 示例 |
---|---|---|
size() | 返回set中的元素数 | cout << my_set.size(); |
clear() | 清空set | my_set.clear(); |
begin() | 返回指向set开头的迭代器 | set |
end() | 返回指向set末尾的迭代器 | if (it != my_set.end()) {...} |
insert(key) | 向set中插入元素key | my_set.insert(5); |
erase(key) | 删除与key匹配的元素 | my_set.erase(5); |
find(key) | 搜索与key一致的元素, 并返回指向该元素的迭代器, 如果不存在该元素, 则返回末尾end() | if (my_set.find(5) != my_set.end()) {...} |
count(key) | 返回与key匹配的元素数 | if (my_set.count(5)) {...} |
lower_bound(key) | 搜索不小于key的第一个元素, 并返回指向该元素的迭代器 | it = my_set.lower_bound(5); |
upper_bound(key) | 搜索大于key的第一个元素, 并返回指向该元素的迭代器 | it = my_set.upper_bound(5); |
// file name: 求两个集合的并集
// Written by: by_sknight
// Date: 2019/6/12
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
set<int> A, B, result;
set<int>::iterator it;
int n, m, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
A.insert(x);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x;
B.insert(x);
}
for (it = A.begin(); it != A.end(); it++) {
result.insert(*it);
}
for (it = B.begin(); it != B.end(); it++) {
result.insert(*it);
}
for (it = result.begin(); it != result.end(); it++) {
cout << *it << endl;
}
return 0;
}
// file name: 求两个集合的交集
// Written by: by_sknight
// Date: 2019/6/12
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
set<int> A, B, result;
set<int>::iterator it;
int n, m, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
A.insert(x);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x;
B.insert(x);
}
for (it = A.begin(); it != A.end(); it++) {
if (B.find(*it) != B.end()) {
result.insert(*it);
}
}
for (it = result.begin(); it != result.end(); it++) {
cout << *it << endl;
}
return 0;
}
// file name: 求两个集合的差(A - B), 并不是所有AB不同的元素
// Written by: by_sknight
// Date: 2019/6/12
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
set<int> A, B, result;
set<int>::iterator it;
int n, m, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
A.insert(x);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x;
B.insert(x);
}
for (it = A.begin(); it != A.end(); it++) {
if (B.find(*it) == B.end()) {
result.insert(*it);
}
}
for (it = result.begin(); it != result.end(); it++) {
cout << *it << endl;
}
return 0;
}
// file name: 求两个集合的差异(A中有B中无, B中有A中无)
// Written by: by_sknight
// Date: 2019/6/12
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
set<int> A, B, result;
set<int>::iterator it;
int n, m, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
A.insert(x);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x;
B.insert(x);
}
for (it = A.begin(); it != A.end(); it++) {
if (B.find(*it) == B.end()) {
result.insert(*it);
}
}
for (it = B.begin(); it != B.end(); it++) {
if (A.find(*it) == A.end()) {
result.insert(*it);
}
}
for (it = result.begin(); it != result.end(); it++) {
cout << *it << endl;
}
return 0;
}
3160702 Solution for ITP2_9_A by c7c7
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
set<int>a,b;
cin>>n;
while(n--){
int x;
cin>>x;
a.insert(x);
}
cin>>n;
while(n--){
int x;
cin>>x;
b.insert(x);
}
vector<int>c;
set_union(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
for(int i=0;i<c.size();i++)cout<<c[i]<<endl;
}
3160698 Solution for ITP2_9_B by c7c7
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
set<int>a,b;
cin>>n;
while(n--){
int x;
cin>>x;
a.insert(x);
}
cin>>n;
while(n--){
int x;
cin>>x;
b.insert(x);
}
vector<int>c;
set_intersection(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
for(int i=0;i<c.size();i++)cout<<c[i]<<endl;
}
3160705 Solution for ITP2_9_C by c7c7
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
set<int>a,b;
cin>>n;
while(n--){
int x;
cin>>x;
a.insert(x);
}
cin>>n;
while(n--){
int x;
cin>>x;
b.insert(x);
}
vector<int>c;
set_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
for(int i=0;i<c.size();i++)cout<<c[i]<<endl;
}
3160709 Solution for ITP2_9_D by c7c7
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
set<int>a,b;
cin>>n;
while(n--){
int x;
cin>>x;
a.insert(x);
}
cin>>n;
while(n--){
int x;
cin>>x;
b.insert(x);
}
vector<int>c;
set_symmetric_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
for(int i=0;i<c.size();i++)cout<<c[i]<<endl;
}
看了别人的代码, 发现有可以直接用的方法, 但是当时并不知道...
[Aizu] ITP2_9_A~D: Set Operation
标签:object bit sea tab -- 迭代器 soc rip src
原文地址:https://www.cnblogs.com/by-sknight/p/11007385.html