先决条件: C++ STL 中的向量
向量被称为动态数组,能够在插入或删除元素时自动调整自身大小,其存储由容器自动处理。
集合是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。元素的值一旦添加到集合中就无法修改,但可以删除和添加该元素的修改后的值。
集合向量可用于设计复杂而高效的数据结构,在本文中,我们将检查一个这样的实例,其中集合向量可能非常有用。
句法:
vector
在集合向量中插入
可以使用 C++ STL 的push_back()函数将元素插入到向量中。首先使用insert()将元素插入到集合中。然后使用push_back()将该集合插入向量中。
下面的示例演示了在集合向量中的插入操作:
C++
// C++ program to demonstrate the
// insertion into a vector of sets
#include
using namespace std;
// Defining the number of sets
// in the vector and number of
// elements in each set
#define ROW 4
#define COL 5
// Driver Code
int main()
{
// Initialize vector of sets
vector > v;
// Elements to insert
// in column
int num = 10;
// Inserting elements
// into vector
for (int i = 0; i < ROW; i++) {
// Stores the column elements
set s;
for (int j = 0; j < COL; j++) {
s.insert(num);
num += 5;
}
// Push the set in the vector
v.push_back(s);
}
// Display the vector of sets
for (int i = 0; i < v.size(); i++) {
for (auto x : v[i])
cout << x << " ";
cout << endl;
}
return 0;
}
C++
// C++ program to demonstrate
// the removal of sets from
// the end of vector of sets
#include
using namespace std;
// Defining the number of sets
// in the vector and number of
// elements in each set
#define ROW 4
#define COL 5
// Driver Code
int main()
{
// Initialize the
// vector of sets
vector > v;
// Elements to insert
// in column
int num = 10;
// Inserting elements
// into vector
for (int i = 0; i < ROW; i++) {
// Vector to store
// column elements
set s;
for (int j = 0; j < COL; j++) {
s.insert(num);
num += 5;
}
// Push the set
// into the vector
v.push_back(s);
}
// Display the vector of sets
// before removal of sets
cout << "Before Removal:" << endl;
for (int i = 0; i < v.size(); i++) {
for (auto x : v[i])
cout << x << " ";
cout << endl;
}
// Remove sets from last
// index of the vector
v.pop_back();
v.pop_back();
// Display the vector of sets
// after removal of sets
cout << endl
<< "After Removal:" << endl;
for (int i = 0; i < v.size(); i++) {
for (auto x : v[i])
cout << x << " ";
cout << endl;
}
return 0;
}
C++
// C++ program to demonstrate
// the removal of sets from
// the end of vector of sets
#include
using namespace std;
// Defining the number of sets
// in the vector and number of
// elements in each set
#define ROW 4
#define COL 5
// Driver Code
int main()
{
// Initialize vector of sets
vector > v;
// Elements to insert
// in column
int num = 10;
// Inserting elements
// into vector
for (int i = 0; i < ROW; i++) {
// Vector to store
// column elements
set s;
for (int j = 0; j < COL; j++) {
s.insert(num);
num += 5;
}
// Push the set
// into the vector
v.push_back(s);
}
// Display the vector of sets
// before removal of sets
cout << "Before Removal:" << endl;
for (int i = 0;
i < v.size(); i++) {
for (auto x : v[i])
cout << x << " ";
cout << endl;
}
// Erase 70 from 3rd set
v[2].erase(70);
// Erase 55 from 2nd set
v[1].erase(55);
// Display the vector of sets
// after removal of sets
cout << endl
<< "After Removal:" << endl;
for (int i = 0; i < v.size(); i++) {
for (auto x : v[i])
cout << x << " ";
cout << endl;
}
return 0;
}
C++
// C++ program to implement vector of sets
#include
using namespace std;
// Function to print set
// of different characters
void separateChar(string s)
{
// Vector of set
vector > v(3);
// Insert data in vector of set
for (int i = 0;
i < s.length(); i++) {
if (s[i] >= 'a'
&& s[i] <= 'z') {
// Insert vowels
if (s[i] == 'a' || s[i] == 'e'
|| s[i] == 'i' || s[i] == 'o'
|| s[i] == 'u')
v[0].insert(s[i]);
// Insert consonants
else
v[1].insert(s[i]);
}
// Insert special characters
else
v[2].insert(s[i]);
}
// Iterate over all the sets
for (int i = 0; i < 3; i++) {
cout << "Elements of set "
<< i + 1 << " :";
// Print elements of each set
for (auto it : v[i]) {
cout << it << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
string s = "geeks@for&geeks@";
// Function Call
separateChar(s);
}
输出:
10 15 20 25 30
35 40 45 50 55
60 65 70 75 80
85 90 95 100 105
集合向量中的移除或删除
-
可以使用 C++ STL 的pop_back()函数从集合向量的末尾删除集合。
下面的示例演示了从集合向量的末尾删除集合:
C++
// C++ program to demonstrate // the removal of sets from // the end of vector of sets #include
using namespace std; // Defining the number of sets // in the vector and number of // elements in each set #define ROW 4 #define COL 5 // Driver Code int main() { // Initialize the // vector of sets vector > v; // Elements to insert // in column int num = 10; // Inserting elements // into vector for (int i = 0; i < ROW; i++) { // Vector to store // column elements set s; for (int j = 0; j < COL; j++) { s.insert(num); num += 5; } // Push the set // into the vector v.push_back(s); } // Display the vector of sets // before removal of sets cout << "Before Removal:" << endl; for (int i = 0; i < v.size(); i++) { for (auto x : v[i]) cout << x << " "; cout << endl; } // Remove sets from last // index of the vector v.pop_back(); v.pop_back(); // Display the vector of sets // after removal of sets cout << endl << "After Removal:" << endl; for (int i = 0; i < v.size(); i++) { for (auto x : v[i]) cout << x << " "; cout << endl; } return 0; } 输出:Before Removal: 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 After Removal: 10 15 20 25 30 35 40 45 50 55
-
元素的值一旦添加到集合中就无法修改,但可以删除该元素的值。 erase()函数用于从集合向量的特定集合中删除特定元素。
下面的示例演示了从集合向量的特定集合中删除给定集合元素的过程:
C++
// C++ program to demonstrate // the removal of sets from // the end of vector of sets #include
using namespace std; // Defining the number of sets // in the vector and number of // elements in each set #define ROW 4 #define COL 5 // Driver Code int main() { // Initialize vector of sets vector > v; // Elements to insert // in column int num = 10; // Inserting elements // into vector for (int i = 0; i < ROW; i++) { // Vector to store // column elements set s; for (int j = 0; j < COL; j++) { s.insert(num); num += 5; } // Push the set // into the vector v.push_back(s); } // Display the vector of sets // before removal of sets cout << "Before Removal:" << endl; for (int i = 0; i < v.size(); i++) { for (auto x : v[i]) cout << x << " "; cout << endl; } // Erase 70 from 3rd set v[2].erase(70); // Erase 55 from 2nd set v[1].erase(55); // Display the vector of sets // after removal of sets cout << endl << "After Removal:" << endl; for (int i = 0; i < v.size(); i++) { for (auto x : v[i]) cout << x << " "; cout << endl; } return 0; } 输出:Before Removal: 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 After Removal: 10 15 20 25 30 35 40 45 50 60 65 75 80 85 90 95 100 105
以下示例演示了集合向量的使用:
给定一个字符串S ,任务是将给定的字符串S分成三个不同的字符集,即元音、辅音或特殊字符。
下面是上述问题的实现:
C++
// C++ program to implement vector of sets
#include
using namespace std;
// Function to print set
// of different characters
void separateChar(string s)
{
// Vector of set
vector > v(3);
// Insert data in vector of set
for (int i = 0;
i < s.length(); i++) {
if (s[i] >= 'a'
&& s[i] <= 'z') {
// Insert vowels
if (s[i] == 'a' || s[i] == 'e'
|| s[i] == 'i' || s[i] == 'o'
|| s[i] == 'u')
v[0].insert(s[i]);
// Insert consonants
else
v[1].insert(s[i]);
}
// Insert special characters
else
v[2].insert(s[i]);
}
// Iterate over all the sets
for (int i = 0; i < 3; i++) {
cout << "Elements of set "
<< i + 1 << " :";
// Print elements of each set
for (auto it : v[i]) {
cout << it << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
string s = "geeks@for&geeks@";
// Function Call
separateChar(s);
}
输出:
Elements of set 1 :e o
Elements of set 2 :f g k r s
Elements of set 3 :& @
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。