C++ STL如何使用verctor的vector?代码示例

2021年4月3日18:49:44 发表评论 1,203 次浏览

先决条件: C++ STL中的向量

向量被称为动态数组能够在插入或删除元素时自动调整自身大小, 并且容器自动处理其存储。

向量的向量是一个二维向量行数可变, 其中每一行都是向量。向量的每个索引存储一个向量, 可以使用以下方法遍历和访问该向量迭代器。它类似于向量数组但具有动态特性。

语法如下:

vector<vector<data_type>> vec;

例子:

vector<vector<int>> vec{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 4 } }; 
where vec is the vector of vectors with different
      number of elements in different rows

在向量的向量中插入

元素可以使用推回()C ++ STL的功能。

下面的示例演示了向量载体中的插入操作。该代码使用push_back()函数创建2D向量, 然后显示矩阵。

语法如下:

vector_name.push_back(value)

where value refers to the element
      to be added in the back of the vector

范例1:

v2 = {1, 2, 3}
v1.push_back(v2);

此函数将向量v2推入向量v1的向量中。因此v1变成{{1, 2, 3}。

范例2:

v2 = {4, 5, 6}
v1.push_back(v2);

此函数将向量v2推入向量v1的现有向量中, 并且v1变为v1 = {{{1, 2, 3}, {4, 5, 6}}

以下是演示插入向量载体的示例。

// C++ program to demonstrate insertion
// into a vector of vectors
  
#include <iostream>
#include <vector>
using namespace std;
  
// Defining the rows and columns of
// vector of vectors
#define ROW 4
#define COL 5
  
int main()
{
     // Initializing the vector of vectors
     vector<vector< int > > vec;
  
     // Elements to insert in column
     int num = 10;
  
     // Inserting elements into vector
     for ( int i = 0; i < ROW; i++) {
         // Vector to store column elements
         vector< int > v1;
  
         for ( int j = 0; j < COL; j++) {
             v1.push_back(num);
             num += 5;
         }
  
         // Pushing back above 1D vector
         // to create the 2D vector
         vec.push_back(v1);
     }
  
     // Displaying the 2D vector
     for ( int i = 0; i < vec.size(); i++) {
         for ( int j = 0; j < vec[i].size(); j++)
             cout << vec[i][j] << " " ;
         cout << endl;
     }
     return 0;
}

输出如下:

10 15 20 25 30 
35 40 45 50 55 
60 65 70 75 80 
85 90 95 100 105

向量向量中的删除或删除

可以使用向量从向量的向量中删除元素pop_back()C ++ STL的功能。

下面的示例演示了向量向量中的删除操作。该代码使用pop_back()函数从2D向量中删除元素, 然后显示矩阵。

语法如下:

vector_name[row_position].pop_back()

范例1:令向量的向量为向量v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

v[2].pop_back()

此函数从最后一行向量中删除元素9。因此v变为{{1, 2, 3}, {4, 5, 6}, {7, 8}。

范例2:

v[1].pop_back()

此函数从最后的第二行向量中删除元素6。因此v变为{{1, 2, 3}, {4, 5}, {7, 8}}。

以下是演示从向量载体中删除的示例。

// C++ program to demonstrate removal
// from a vector of vectors
  
#include <iostream>
#include <vector>
using namespace std;
  
// Driver Method
int main()
{
     // Initializing 2D vector "vect" with
     // sample values
     vector<vector< int > > vec{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  
     // Removing elements from the
     // last row of the vector
     vec[2].pop_back();
     vec[1].pop_back();
  
     // Displaying the 2D vector
     for ( int i = 0; i < 3; i++) {
         for (
             auto it = vec[i].begin();
             it != vec[i].end(); it++)
             cout << *it << " " ;
         cout << endl;
     }
     return 0;
}

输出如下:

1 2 3 
4 5 
7 8

向量的遍历

向量的向量可以使用迭代器在C ++中。以下代码演示了2D向量的遍历。

语法如下:

for i in [0, n)
{
    for (iterator it = v[i].begin();
         it != v[i].end(); it++) 
   {
        // Operations to be done
        // For example to print
        print(*it)
    }
}

以下是演示向量向量中遍历的示例。

// C++ code to demonstrate traversal
// of a 2D vector
  
#include <iostream>
#include <vector>
using namespace std;
  
// Driver Method
int main()
{
     // Initializing 2D vector "vect" with
     // sample values
     vector<vector< int > > vec{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  
     // Displaying the 2D vector
     for ( int i = 0; i < 3; i++) {
         for (
             auto it = vec[i].begin();
             it != vec[i].end(); it++)
             cout << *it << " " ;
         cout << endl;
     }
  
     return 0;
}

输出如下:

1 2 3 
4 5 6 
7 8 9

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: