C++ 中的 2d 向量 - 实用指南 2d 向量

也称为向量的向量,C++中的2D向量构成动态创建矩阵、表或任何其他结构的基础。在讨论C++中的2D向量之前,建议先阅读使用一维C++.中的向量]的教程

包含向量头文件

如果不是程序开头包含的头文件,我们就不可能在C++中使用向量。为了利用2D向量,我们包括:

1#include<vector>

我们可以通过以下方式包括所有标准模板库,而不是逐个包括各种标准模板库(STL):

1#include<bits/stdc++.h>

在C++中初始化2D向量

首先,我们将学习初始化2-D向量的某些方法。下面的代码片段解释了当所有元素都已知时二维向量的初始化。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6    vector<vector<int>> v {{1, 0, 1}, {0, 1}, {1, 0, 1}}; 
 7    for(int i=0;i<v.size();i++){
 8    	for(int j=0;j<v[i].size();j++)
 9    		cout<<v[i][j]<<" ";
10    	cout<<endl;
11    }					   
12}

运行上述代码后,我们将获得以下输出:

11 0 1 
20 1 
31 0 1

使用vector <vector<> vector象征着我们正在处理一个向量的向量。第一组大括号中的每个值,如{1,0,1}{0,1},都是独立的向量。

:在C++中创建不同数据类型的2D向量时,可以将数据类型放在最内侧的尖括号中,如<char>

由于我们正在处理二维数据结构,因此我们需要两个loops来高效地遍历完整的数据结构。外部循环沿行移动,而内部循环遍历列。

注意: ‘Size()’函数提供的是2D向量内部的向量个数,而不是每个向量内部的元素总数。

指定2D向量初始化大小

2D向量可以是大尺寸的。我们不能期望程序员将每个值都输入。因此,我们可以根据行数和列数初始化一个二维向量。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6    //Number of columns
 7    int num_col = 3;
 8
 9    // Number of rows
10    int num_row = 4;
11
12    // Initializing a single row
13    vector<int> row(num_col, 0);
14
15    // Initializing the 2-D vector
16    vector<vector<int>> v(num_row, row) ;
17
18    for(int i=0;i<v.size();i++){
19    	for(int j=0;j<v[i].size();j++)
20    		cout<<v[i][j]<<" ";
21    	cout<<endl;
22    }					   
23}

输出将为:

10 0 0 
20 0 0 
30 0 0 
40 0 0

根据向量的标准初始化‘向量<int>v(10,0)’,第一个参数表示向量的大小,而第二个参数表示每个单元格持有的缺省值。

在上面的代码片段中,我们遵循两个标准初始化步骤:

  • ‘向量<int>row(num_ol,0)’-在该语句中,我们创建了一个称为‘row’的一维向量,其长度由‘num_ol’定义,缺省值为‘0’。它基本上形成了我们的二维矢量的每一行。
  • ‘VECTOR<VECTOR<INT>>v(num_row,row)-在此语句中,我们通过将二维向量的每个值定义为在最后一条语句中创建的’row‘来创建完整的二维向量。

在理解了上述过程之后,我们可以通过以下方式改进在C++中对2D向量的初始化:

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6    //Number of columns
 7    int num_col = 3;
 8
 9    // Number of rows
10    int num_row = 4;
11
12    // Initializing the 2-D vector
13    vector<vector<int>> v(num_row, vector<int> (num_col, 0)) ;
14
15    for(int i=0;i<v.size();i++){
16    	for(int j=0;j<v[i].size();j++)
17    		cout<<v[i][j]<<" ";
18    	cout<<endl;
19    }					   
20}

上面的代码将提供与前面类似的输出,因为我们正在做完全相同的事情,只是在一行代码中。

如果我们没记错的话,标准初始化看起来有点像上面的。创建二维向量需要将每个元素的缺省值设置为一维向量。

最后一种方法是在不知道行或列的情况下创建2-D矢量。它是由以下人员完成的:

1vector<vector<int>> v;

上面的声明创建了一个空容器,该容器能够以向量的形式存储元素。


2D向量的迭代器

与使用索引遍历2D向量不同,C++为每个特定的STL数据结构提供了迭代器。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6
 7    vector<vector<int>> v {{1, 0, 1}, {0, 1}, {1, 0, 1}}; 
 8
 9    // Iterator for the 2-D vector
10    vector<vector<int>>::iterator it1;
11
12    // Iterator for each vector inside the 2-D vector
13    vector<int>::iterator it2;
14
15    // Traversing a 2-D vector using iterators
16    for(it1 = v.begin();it1 != v.end();it1++){
17    	for(it2 = it1->begin();it2 != it1->end();it2++)
18    		cout<<*it2<<" ";
19    	cout<<endl;
20    }					   
21}

输出:

11 0 1 
20 1 
31 0 1

当我们使用某些需要参数进行定位的操作时,迭代器就派上了用场。返回迭代器值的两个最常用的函数是:

  • ‘v.egin()’-它返回二维向量中第一个向量的迭代器。
  • ‘v.end()’-它返回2-D向量末尾的迭代器。

让我们看看在2-D向量上可能的一些运算。


向二维向量添加元素

要在二维向量的末尾添加元素,我们使用push_back(push)函数。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6
 7    // Initializing the 2-D vector
 8    vector<vector<int>> v;
 9
10    v.push_back({1, 0, 1});
11    v.push_back({0, 1});
12    v.push_back({1, 0, 1});
13
14    for(int i=0;i<v.size();i++){
15    	for(int j=0;j<v[i].size();j++)
16    		cout<<v[i][j]<<" ";
17    	cout<<endl;
18    }					   
19}

输出:

11 0 1 
20 1 
31 0 1

因为我们的容器是一个向量的向量,所以只有将完整的向量推入其中才有意义。因此,在‘PUSH_BACK()’函数中传递的参数必须是向量。

注: v [i]表示一维向量。因此,如果程序员需要在二维向量中的某个向量中添加元素,他可以使用'v [i].push_back(value '`。

要在特定位置添加一个完整的向量,我们使用"insert(``函数。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6
 7    // Initializing the 2-D vector
 8    vector<vector<int>> v;
 9
10    v.push_back({1, 0, 1});
11    v.push_back({0, 1});
12    v.push_back({1, 0, 1});
13
14    // Iterator for the 2-D vector
15    vector<vector<int>>::iterator it = v.begin();
16
17    // Inserting the vector = {1, 2, 3} as the second vector
18    v.insert(it + 1, {1, 2, 3});
19    			  
20    for(int i=0;i<v.size();i++){
21    	for(int j=0;j<v[i].size();j++)
22    		cout<<v[i][j]<<" ";
23    	cout<<endl;
24    }				   
25}

输出:

11 0 1 
21 2 3 
30 1 
41 0 1

insert()函数需要一个位置参数作为迭代器,而不是整数索引。它后面跟着一个应该插入到指定位置的向量。


在C++中去除2D向量中的元素

PUSH_BACK()‘相反,C++为’POP_BACK()‘函数提供了从给定向量中删除最后一个元素的职责。

在本文的上下文中,‘op_back()’函数将负责从2-D向量中删除最后一个向量。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6
 7    // Initializing the 2-D vector
 8    vector<vector<int>> v ;
 9
10    // Adding vectors to the empty 2-D vector
11    v.push_back({1, 0, 1});
12    v.push_back({0, 1});
13    v.push_back({1, 0, 1});
14
15    // Remove the last vector from a 2-D vector
16    v.pop_back();
17    			  
18    for(int i=0;i<v.size();i++){
19    	for(int j=0;j<v[i].size();j++)
20    		cout<<v[i][j]<<" ";
21    	cout<<endl;
22    }				   
23}

输出:

11 0 1 
20 1

除了‘POP_BACK()’函数之外,我们还有一个‘erase()’函数,我们可以使用该函数从指定的索引中删除元素。

 1#include<iostream>
 2#include<vector>
 3using namespace std;
 4
 5int main(){
 6
 7    // Initializing the 2-D vector
 8    vector<vector<int>> v ;
 9
10    // Pushing vector inside the empty 2-D vector
11    v.push_back({1, 0, 1});
12    v.push_back({0, 1});
13    v.push_back({1, 0, 1});
14
15    // Iterator for the 2-D vector
16    vector<vector<int>>::iterator it = v.begin();
17
18    // Remove the second vector from a 2-D vector
19    v.erase(it + 1);
20    			  
21    for(int i=0;i<v.size();i++){
22    	for(int j=0;j<v[i].size();j++)
23    		cout<<v[i][j]<<" ";
24    	cout<<endl;
25    }				   
26}

输出:

11 0 1 
21 0 1

‘INSERT()’函数类似,它需要一个位置参数作为迭代器。要从2-D向量中删除所有向量,可以使用‘Clear()’函数。

在C++中使用2-D向量时,上述函数可能足以满足您的要求。


总结

如果程序员知道所涉及的语法,那么C++中的二维向量非常容易使用。当我们解决与矩阵、图形和其他二维对象相关的问题时,这种向量会派上用场。

我们希望本教程能在使用2-D向量的主题上启发读者。如有任何与该主题相关的问题,请随时在下面发表评论。

Published At
Categories with 技术
Tagged with
comments powered by Disqus