先决条件: 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
-
尽管可以删除该元素的值,但是一旦将该元素的值添加到集合中就无法对其进行修改。 delete()函数用于从集合的向量的特定集合中删除特定元素。
下面的示例演示了如何从集合的向量的特定集合中移除给定的集合元素:
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++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。