C++ 中元组的二维向量及示例
什么是向量?
在 C++ 中,向量类似于动态数组,具有自动调整自身大小的能力。向量元素存储在连续的内存位置,以便可以使用迭代器访问和遍历它们。
与向量相关的函数:
- begin() :返回一个指向向量中第一个元素的迭代器。
- end() :返回一个迭代器,指向向量中最后一个元素之后的理论元素。
- rbegin() :返回一个反向迭代器,指向向量中的最后一个元素(反向开始)。它从最后一个元素移动到第一个元素。
- size() :返回向量中的元素数。
- empty() :返回向量是否为空。
- push_back() :它将元素从后面推入一个向量。
- pop_back() :它用于从后面的向量中弹出或删除元素。
- insert() :在指定位置的元素之前插入新元素。
什么是二维向量?
在 C++ 中,2D 向量是向量的向量,这意味着 2D 向量的每个元素都是向量本身。它与借助向量实现的矩阵相同。
与二维向量相关的函数:
- size():返回二维向量中的元素个数。
- empty():返回二维向量是否为空。
- push_back() :它将一个向量从后面推入一个二维向量。
- pop_back() :它用于从后面的 2D 向量中弹出或删除元素。
什么是元组?
C++ 中的元组是用于将元素组合在一起的对象。在元组中,元素可以是相同的数据类型或不同的数据类型。元组的元素按照它们被访问的顺序进行初始化。
与元组关联的函数:
1. make_tuple() : make_tuple() 用于给元组赋值。传递的值应该与元组中声明的值顺序一致。
2. get() : get() 用于访问元组值并修改它们,它接受索引和元组名称作为参数来访问特定的元组元素。
如何访问元组?
要访问元组的元素,请使用 get<>()函数。
句法:
auto fistElement = get<0>(myTuple);
auto secondElement = get<1>(myTuple);
auto thirdElement = get<2>(myTuple);
本文重点介绍如何在 C++ 中创建元组的二维向量。
元组的二维向量
元组的二维向量或元组向量的向量是其中每个元素是元组本身的向量的向量。尽管为简单起见,一个元组可以包含任意数量的元素,但考虑了三个元素的元组。
句法:
vector
Here,
dataType1, dataType2, dataType3 can be similar or dissimilar data types.
示例 1:下面是实现 2D 元组向量的 C++ 程序。
C++
// C++ program to demonstrate the
// working of vector of vectors
// of tuples
#include
using namespace std;
// Function to print 2D vector elements
void print(vector > >& myContainer)
{
// Iterating over 2D vector elements
for (auto currentVector : myContainer) {
// Each element of the 2D vector
// is a vector itself
vector > myVector
= currentVector;
// Iterating over the the vector elements
cout << "[ ";
for (auto currentTuple : myVector) {
// Print the element
cout << "{";
cout << get<0>(currentTuple)
<< ", " << get<1>(currentTuple)
<< ", " << get<2>(currentTuple);
cout << "} ";
}
cout << "]\n";
}
}
// Driver code
int main()
{
// Declaring a 2D vector of tuples
vector > >
myContainer;
// Initializing vectors of tuples
// tuples are of type {int, int, int}
vector > vect1
= { { 1, 1, 2 }, { 2, 2, 4 }, { 3, 3, 6 }, { 4, 4, 8 } };
vector > vect2
= { { 1, 2, 3 }, { 1, 3, 4 }, { 1, 4, 5 }, { 1, 5, 6 } };
vector > vect3
= { { 4, 5, 2 }, { 8, 1, 9 }, { 9, 3, 1 }, { 2, 4, 8 } };
vector > vect4
= { { 7, 2, 1 }, { 6, 5, 1 }, { 1, 2, 9 }, { 10, 4, 8 } };
// Inserting vector of tuples in the 2D vector
myContainer.push_back(vect1);
myContainer.push_back(vect2);
myContainer.push_back(vect3);
myContainer.push_back(vect4);
// Calling print function
print(myContainer);
return 0;
}
C++
// C++ program to demonstrate the
// working of vector of vectors of tuples
#include
using namespace std;
// Function to print 2D vector elements
void print(vector > >& myContainer)
{
// Iterating over 2D vector elements
for (auto currentVector : myContainer) {
// Each element of the 2D vector
// is a vector itself
vector > myVector
= currentVector;
// Iterating over the the vector
// elements
cout << "[ ";
for (auto currentTuple : myVector) {
// Prstring the element
cout << "{";
cout << get<0>(currentTuple) << ", "
<< get<1>(currentTuple) << ", "
<< get<2>(currentTuple);
cout << "} ";
}
cout << "]\n";
}
}
// Driver code
int main()
{
// Declaring a 2D vector of tuples
vector > >
myContainer;
// Initializing vectors of tuples
// tuples are of type {string, string, string}
vector > vect1
= { { "Geeks", "for", "Geeks" },
{ "Swift", "Python", "Java" },
{ "Int", "Float", "Double" } };
vector > vect2
= { { "C++", "C", "C#" },
{ "R", "HTML", "CSS" },
{ "Javascript", "PHP", "Django" } };
vector > vect3
= { { "Bhuwanesh", "Harshit", "DS" },
{ "Piyush", "Jai", "Naveen" },
{ "Anil", "Rahul", "keshav" } };
vector > vect4
= { { "Sweta", "Tanu", "Kavita" },
{ "Nawal", "Bhargav", "Jitesh" },
{ "Daya", "Mohan", "Bhuwanesh" } };
// Inserting vector of tuples in the
// 2D vector
myContainer.push_back(vect1);
myContainer.push_back(vect2);
myContainer.push_back(vect3);
myContainer.push_back(vect4);
// Calling print function
print(myContainer);
return 0;
}
[ {1, 1, 2} {2, 2, 4} {3, 3, 6} {4, 4, 8} ]
[ {1, 2, 3} {1, 3, 4} {1, 4, 5} {1, 5, 6} ]
[ {4, 5, 2} {8, 1, 9} {9, 3, 1} {2, 4, 8} ]
[ {7, 2, 1} {6, 5, 1} {1, 2, 9} {10, 4, 8} ]
示例 2:下面是实现 2D 元组向量的 C++ 程序。
C++
// C++ program to demonstrate the
// working of vector of vectors of tuples
#include
using namespace std;
// Function to print 2D vector elements
void print(vector > >& myContainer)
{
// Iterating over 2D vector elements
for (auto currentVector : myContainer) {
// Each element of the 2D vector
// is a vector itself
vector > myVector
= currentVector;
// Iterating over the the vector
// elements
cout << "[ ";
for (auto currentTuple : myVector) {
// Prstring the element
cout << "{";
cout << get<0>(currentTuple) << ", "
<< get<1>(currentTuple) << ", "
<< get<2>(currentTuple);
cout << "} ";
}
cout << "]\n";
}
}
// Driver code
int main()
{
// Declaring a 2D vector of tuples
vector > >
myContainer;
// Initializing vectors of tuples
// tuples are of type {string, string, string}
vector > vect1
= { { "Geeks", "for", "Geeks" },
{ "Swift", "Python", "Java" },
{ "Int", "Float", "Double" } };
vector > vect2
= { { "C++", "C", "C#" },
{ "R", "HTML", "CSS" },
{ "Javascript", "PHP", "Django" } };
vector > vect3
= { { "Bhuwanesh", "Harshit", "DS" },
{ "Piyush", "Jai", "Naveen" },
{ "Anil", "Rahul", "keshav" } };
vector > vect4
= { { "Sweta", "Tanu", "Kavita" },
{ "Nawal", "Bhargav", "Jitesh" },
{ "Daya", "Mohan", "Bhuwanesh" } };
// Inserting vector of tuples in the
// 2D vector
myContainer.push_back(vect1);
myContainer.push_back(vect2);
myContainer.push_back(vect3);
myContainer.push_back(vect4);
// Calling print function
print(myContainer);
return 0;
}
[ {Geeks, for, Geeks} {Swift, Python, Java} {Int, Float, Double} ]
[ {C++, C, C#} {R, HTML, CSS} {Javascript, PHP, Django} ]
[ {Bhuwanesh, Harshit, DS} {Piyush, Jai, Naveen} {Anil, Rahul, keshav} ]
[ {Sweta, Tanu, Kavita} {Nawal, Bhargav, Jitesh} {Daya, Mohan, Bhuwanesh} ]