c++中的集合数据结构的定义方式与在数学上下文中定义集合的方式相同。
更正式地说,集合是一种关联容器,其中每个元素必须是唯一的。
- 输入元素后,无法修改元素的值,但允许删除元素并插入新元素,就像我们在mathenatics中一样。
- 设置数据结构可用于建模,并设置自身。很容易找到交叉点,工会等。
- 与vector类似,但只允许使用唯一值。
- 当您将元素插入集合时,Set会按递增顺序排列元素。
使用set数据结构所需的头文件是'set'。即, #include<set>
必须在您的代码中,以便您使用set数据结构。
提示 : - 使用#include<bits/stdc++.h>
包含所有C ++数据结构和函数,而不是逐个添加它们。
可以使用set执行的一些功能: -
- begin() - 返回集合中第一个元素的迭代器
- end() - 返回到集合中最后一个元素后面的理论元素的迭代器
- size() - 返回集合中的元素数
- max_size() - 返回集合可容纳的最大元素数
- empty() - 返回集合是否为空
- erase(const g) - 从集合中删除值“g”
- clear() - 从集合中删除所有元素
让我们看一个例子: -
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
set <int> myset; //an empty set container. Note that size of the set need not be declared, similar to vector.
// insert elements in random order
myset.insert(65);
myset.insert(30);
myset.insert(80);
myset.insert(20);
myset.insert(9);
myset.insert(9); // only one 9 will be added to the list.
// printing set myset
set <int> :: iterator itr; //an iterator is like a pointer.
cout << "\nThe contents of myset : ";
for (itr = myset.begin(); itr != myset.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// remove all elements up to 65 in myset from the beginning:-
cout << "\nContents of myset after removal of elements less than 30 : ";
myset.erase(myset.begin(), myset.find(30));
for (itr = myset.begin(); itr != myset.end(); ++itr)
{
cout << '\t' << *itr;
}
// remove element with value 50 in myset
int num = myset.erase(80); //returns true (and deletes) if 80 is there in the list else returns 0.
cout<<"\n\n After doing myset.erase(80), "<<num<<" element is removed\n\n";
cout<<"Contents of the modified set:\t";
for (itr = myset.begin(); itr != myset.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
return 0;
}
更多C++教程
学习更多C++教程