c++三维向量的使用 | C++3D Vector示例 18

作者 : 慕源网 本文共2982个字,预计阅读时间需要8分钟 发布时间: 2022-04-21 共1.25K人阅读

c++三维的使用,类似于三维阵列。它将元素存储在三个维度中。它可以像三维矩阵一样声明和赋值。三维向量是动态的,当要插入或删除元素时,它能够自动调整自身的大小。三维向量存储由容器自动处理。在本主题中,我们将学习c++三维向量的使用

语法

C++中三维向量的声明语法:

std :: vector< std :: vector< std :: vector< data_type > > > vectName;

C++中三维向量的初始化语法:

vectName[index1][ index2][ index3] = value;

或者

在C++中同时声明和初始化三维向量的语法:

std :: vector< std :: vector< std :: vector< data_type > > > vectName( size, std :: vector< std :: vector<data_type> > ( size, std :: vector< data_type  >( size, value)));

参数:

  • DATA_TYPE:指定我们要创建的向量的类型。数据类型可以是int、float、string等。
  • Size:指定我们要创建的向量的大小;在向量的每个维度中,它可以是相同的或不同的。
  • Value:指定Vector元素的初始化值,所有元素都按此值初始化。

C++中三维向量的处理

三维向量实际上是一个动态三维数组,它可以通过调整自身大小来插入和删除元素。与三维数组一样,三维向量也定义了它的类型,以便我们可以创建int类型、string类型、float类型和所有不同类型的三维向量。

三维向量以块大小、行大小和列大小的形式存储三维元素,通过使用三个不同的下标来表示。

通过使用这些下标或索引,我们可以将值存储到三维向量中,并且还可以访问存储在Vector中的值。

为Vector的所有元素创建值为1的三维向量的示例。

接下来,我们编写C++代码,以便通过下面的示例更清楚地理解三维向量,其中2*2*3的三维向量大小和初始化值为1的所有元素。

例#1

代码:

#include <iostream>
// for 3D vector
#include <vector>
using namespace std;
int main()
{
// Initializing 3D vector "vect3d" with
// values 1
//here creating the vector of 1D of 2 size, 2D of 2 size and 3D of 3 size
std::vector<std::vector<std::vector<int> > > vect3d(2, std::vector<std::vector<int> > (2, std::vector<int>(3,1)));
// Displaying the 3D vector by using 3 iterators
for (int i = 0; i < vect3d.size(); i++) {
cout << "Elements at block "
<< i << ": ";
// Displaying element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
cout << "Elements at row "
<< j << ": ";
for (int k = 0; k != vect3d[i][j].size(); k++) {
// use all indexes to get the values store in the vector
cout << vect3d[i][j][k]<< ' ';
}
cout << endl;
}
}
return 0;
}

输出:

如在上面的程序中,三维向量被声明为每个维度具有不同的大小,块大小为2,行大小为2,列大小为3,因此该Vector中的元素数量将为12个元素。正如我们在输出中所看到的,在声明时,向量的所有12个元素的初始化值为1。

使用用户输入的值创建三维向量的示例。

接下来,我们编写C++代码来理解三维向量,其中我们将创建2*2*3大小的三维向量,并将用户提供的值存储到Vector中。

例#2

代码:

#include <iostream>
// for 3D vector
#include <vector>
using namespace std;
int main()
{
// Initializing 3D vector "vect3d" with
// user provided values
//here creating the vector of 1D of 2 size, 2D of 2 size and 3D of 3 size
std::vector<std::vector<std::vector<int> > > vect3d(2, std::vector<std::vector<int> > (2, std::vector<int>(3)));
// Inserting elements into the vector
for (int i = 0; i < vect3d.size(); i++) {
cout << "Elements at block "
<< i << ": ";
// Inserting element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
cout << "Elements at row "
<< j << ": ";
for (int k = 0; k != vect3d[i][j].size(); k++) {
cout<<"\nEnter number: ";
// use all indexes to insert the values into the vector
cin >> vect3d[i][j][k];
}
cout << endl;
}
}
// Displaying the 3D vector by using 3 iterator
cout << "The vectore values are: " <<" ";
for (int i = 0; i < vect3d.size(); i++) {
// Displaying element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
for (int k = 0; k != vect3d[i][j].size(); k++) {
// use all indexes to get the values store in the vector
cout << vect3d[i][j][k]<< ' ';
}
cout << endl;
}
}
return 0;
}

输出:

如在上面的程序中,三维向量被声明为每个维度具有不同的大小,块大小为2,行大小为2,列大小为3。在后面的代码中,用户提供的值在三个索引或iterators的帮助下存储到Vector中,同样,Vector在三个索引的帮助下显示。

结论

Vector是C++中的内置序列容器,在<Vector>头文件中定义。三维向量,就像三维数组一样,通过使用iterators,我们可以访问向量元素。

推荐文章

这是一篇关于C++三维向量的指南。在这里,我们分别讨论了C++中三维向量的介绍、工作方式以及示例。您还可以查看以下文章以了解更多信息–


慕源网 » c++三维向量的使用 | C++3D Vector示例 18

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录