C++ STL 中的集合数组
数组是存储在连续内存位置的项目的集合。它是将多个相同类型的项目存储在一起。这使得通过每个元素的位置访问存储在其中的元素变得更加容易。
集合是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。元素的值一旦添加到集合中就无法修改,但可以删除和添加该元素的修改后的值。
因此,集合数组是具有固定行数的二维数组,其中每行是一组可变长度。数组的每个索引存储一个可以使用迭代器遍历和访问的集合。
句法:
set
例子:
set< int >S[5], where S is the array of sets of int of size 5
在集合数组中插入:使用 insert()函数在每个集合中插入元素。下面是演示在集合数组中插入操作的示例:
C++
// C++ program to demonstrate the
// insertion in the array of sets
#include
using namespace std;
#define ROW 4
#define COL 5
// Driver Code
int main()
{
// Declaring array of sets
set s[ROW];
// Elements to insert
// in set
int num = 10;
// Inserting elements
// into sets
for (int i = 0; i < ROW; i++) {
// Insert the column elements
for (int j = 0; j < COL; j++) {
s[i].insert(num);
num += 5;
}
}
// Display the array of sets
for (int i = 0; i < ROW; i++) {
cout << "Elements at index " << i << ": ";
// Print the array of sets
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
return 0;
}
C++
// C++ program to demonstrate the
// deletion in the array of sets
#include
using namespace std;
// Defining the length of array
// and number of elements in
// each set
#define ROW 4
#define COL 5
// Driver Code
int main()
{
// Declaring array of sets
set s[ROW];
int num = 10;
// Inserting elements in the set
// at each index of array
for (int i = 0; i < ROW; i++) {
// insert the column elements
for (int j = 0; j < COL; j++) {
s[i].insert(num);
num += 5;
}
}
cout << "Before removal elements are:"
<< endl;
// Display the array of sets
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
// Erase 70 from 3rd set
s[2].erase(70);
// Erase 55 from 2nd set
s[1].erase(55);
// Display the array of sets
// after removal of elements
cout << endl
<< "After removal elements are:"
<< endl;
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
// Print the current set
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
return 0;
}
C++
// C++ program to demonstrate the
// traversal in the array of sets
#include
using namespace std;
#define ROW 2
// Driver Code
int main()
{
// Declaring array of sets
set s[ROW];
// Inserting elements into sets
// Insert 10, 15, and 35 in 1st set
s[0].insert(10);
s[0].insert(15);
s[0].insert(35);
// Insert 20 and 30 in 2nd set
s[1].insert(20);
s[1].insert(30);
// Traversing of sets s to print
// elements stored in it
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
// Traversing and printing
// element at each column,
// begin() is the starting
// iterator, end() is the
// ending iterator
for (auto it = s[i].begin();
it != s[i].end();
it++) {
// (*it) is used to get the
// value at iterator is pointing
cout << *it << ' ';
}
cout << endl;
}
return 0;
}
输出:
Elements at index 0: 10 15 20 25 30
Elements at index 1: 35 40 45 50 55
Elements at index 2: 60 65 70 75 80
Elements at index 3: 85 90 95 100 105
删除集合数组中的元素:从每个集合中删除元素是使用erase()函数的。下面是一个例子来演示集合数组中的删除操作:
C++
// C++ program to demonstrate the
// deletion in the array of sets
#include
using namespace std;
// Defining the length of array
// and number of elements in
// each set
#define ROW 4
#define COL 5
// Driver Code
int main()
{
// Declaring array of sets
set s[ROW];
int num = 10;
// Inserting elements in the set
// at each index of array
for (int i = 0; i < ROW; i++) {
// insert the column elements
for (int j = 0; j < COL; j++) {
s[i].insert(num);
num += 5;
}
}
cout << "Before removal elements are:"
<< endl;
// Display the array of sets
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
// Erase 70 from 3rd set
s[2].erase(70);
// Erase 55 from 2nd set
s[1].erase(55);
// Display the array of sets
// after removal of elements
cout << endl
<< "After removal elements are:"
<< endl;
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
// Print the current set
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
return 0;
}
输出:
Before removal elements are:
Elements at index 0: 10 15 20 25 30
Elements at index 1: 35 40 45 50 55
Elements at index 2: 60 65 70 75 80
Elements at index 3: 85 90 95 100 105
After removal elements are:
Elements at index 0: 10 15 20 25 30
Elements at index 1: 35 40 45 50
Elements at index 2: 60 65 75 80
Elements at index 3: 85 90 95 100 105
集合数组中的遍历:在 array[index] 的帮助下从集合数组中遍历每个集合,并使用迭代器执行集合中的遍历。下面是说明在集合数组中遍历的程序:
C++
// C++ program to demonstrate the
// traversal in the array of sets
#include
using namespace std;
#define ROW 2
// Driver Code
int main()
{
// Declaring array of sets
set s[ROW];
// Inserting elements into sets
// Insert 10, 15, and 35 in 1st set
s[0].insert(10);
s[0].insert(15);
s[0].insert(35);
// Insert 20 and 30 in 2nd set
s[1].insert(20);
s[1].insert(30);
// Traversing of sets s to print
// elements stored in it
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
// Traversing and printing
// element at each column,
// begin() is the starting
// iterator, end() is the
// ending iterator
for (auto it = s[i].begin();
it != s[i].end();
it++) {
// (*it) is used to get the
// value at iterator is pointing
cout << *it << ' ';
}
cout << endl;
}
return 0;
}
输出:
Elements at index 0: 10 15 35
Elements at index 1: 20 30
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。