集合
c++中用来表示集合。
构造的时候,可以直接,这样我们就定义了一个叫,类型的集合,初始的时候集合为空。
插入元素:
用来插入元素,如果这个元素之前有那么就不会再存一遍,因为集合有互异性。
#include <set>
#include <string>
using namespace std;
int main() {
set<string> country; // {}
country.insert("China"); // {"China"}
country.insert("America"); // {"China", "America"}
country.insert("France"); // {"China", "America", "France"}
country.insert("China"); // {"China", "America", "France"}
return 0;
}删除元素:
用来删除一个元素,如果集合中没有这个元素,那么不做任何的操作。
#include <set>
#include <string>
using namespace std;
int main() {
set<string> country; // {}
country.insert("China"); // {"China"}
country.insert("America"); // {"China", "America"}
country.insert("France"); // {"China", "America", "France"}
country.erase("America"); // {"China", "France"}
country.erase("England"); // {"China", "France"}
return 0;
}判断元素是否存在:
用来查找,如果有则返回1,没有返回0。
#include <set>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
set<string> country; // {}
country.insert("China"); // {"China"}
country.insert("America"); // {"China", "America"}
country.insert("France"); // {"China", "America", "France"}
if (country.count("China")) {
cout << "China belong to country" << endl;
}
return 0;
}遍历元素:
c++可以通过迭代器来访问每一个元素,我们可以通过来改变它指向的元素。
通过可以指向下一个元素,通过可以指向上一个元素。
迭代器的写法是固定的 这样就定义了一个指向这种集合的的迭代器,,是任意的数据类型。返回的是容器中起始元素的迭代器,返回容器的尾后的迭代器。
#include <set>
#include <string>
#include <iostream>
using namespace std;
int main() {
set<string> country; // {}
country.insert("China"); // {"China"}
country.insert("America"); // {"China", "America"}
country.insert("France"); // {"China", "America", "France"}
for (set<string>::iterator it = country.begin(); it != country.end(); it++) {
cout << *it << endl;
}
return 0;
}输出为
America
China
France由此可以看出是会帮我们排序的。
清空:
用可以清空整个,同时也可以消除所占的内存。
重载运算符:
struct Node {
int x, y;
bool operator<(const Node &rhs) const {
if (x == rhs.x) {
return y < rhs.y;
} else {
return x < rhs.x;
}
}
};是表示我们要重载一个小于号,这个代码为比较固定的格式。
多重集合:
这是一个数学中的概念,是集合的推广,在多重集合里一个元素可以出现多次,比如{1,1,1,2,2,3}就是一个多重集合,一个元素在多重集合中出现的次数,称为这个元素在这个多重集合中的重数(或重次,重复度)。
c++中的多重集合为,同样是在这个头文件下定义的,相关函数与集合的是一致的。
multiset<int> ms; //初始时集合为空 {}
ms.insert(5); //插入元素 5 {5}
ms.insert(4); //插入元素 4 {4,5}
ms.insert(5); //插入元素 5 {4,5,5}
ms.insert(7); //插入元素 7 {4,5,5,7}
ms.erase(3); //删除元素 3 {4,5,5,7}
ms.erase(4); //删除元素 4 {5,5,7}
ms.erase(5); //删除元素 5 {7}这里有一个需要注意的,在多重集合中使用(删除)时,会将所有的元素都删去。像上面的这个代码,删除5时,将两个5都删除了。
这里出现了一个新的函数——
函数指向了第一个值为的元素的迭代器,若不存在与集合中,则返回尾后迭代器。
multiset<int> ms; //初始时集合为空 {}
ms.insert(5); //插入元素 5 {5}
ms.insert(4); //插入元素 4 {4,5}
ms.insert(5); //插入元素 5 {4,5,5}
ms.insert(7); //插入元素 7 {4,5,5,7}
multiset<int>::iterator it;
it = ms.find(5); //it 指向第一个 5 的位置
cout << *it << endl; //输出 5
it = ms.find(6); //此时 it 为尾后迭代器 ms.end()然后,我们再看删除,如果想只删除一个元素的话,就可以将和连用。
multiset<int> ms; //初始时集合为空 {}
ms.insert(5); //插入元素 5 {5}
ms.insert(4); //插入元素 4 {4,5}
ms.insert(5); //插入元素 5 {4,5,5}
ms.insert(7); //插入元素 7 {4,5,5,7}
cout << ms.count(5) << endl; //容器中 5 的个数有 2 个
ms.erase(ms.find(5)); //删除第一个元素 5 {4,5,7}
cout << ms.count(5) << endl; //容器中 5 的个数有 1 个
这里还有两个和功能类似的函数’lower_bound’和’upper_bound’,复杂度都是log n。
lower_bound:
返回指向第一个大于等于元素的迭代器,若不存在则返回尾后迭代器。
upper_bound:
返回指向第一个大于元素的迭代器,若不存在则返回尾后迭代器。
multiset<int> ms; //初始时集合为空 {}
ms.insert(5); //插入元素 5 {5}
ms.insert(4); //插入元素 4 {4,5}
ms.insert(5); //插入元素 5 {4,5,5}
ms.insert(7); //插入元素 7 {4,5,5,7}
cout << *ms.lower_bound(4) << endl; //容器中第一个大于等于 4 的元素是 4
cout << *ms.upper_bound(4) << endl; //容器中第一个大于 4 的元素是 5
cout << *ms.lower_bound(6) << endl; //容器中第一个大于等于 6 的元素是 7
cout << *ms.upper_bound(6) << endl; //容器中第一个大于 6 的元素是 7