C++ 中元组的双端队列与示例
什么是双端队列?
在 C++ 中,双端队列是一个序列容器,也称为双端队列。顾名思义,双端队列允许从两端插入和删除。虽然双端队列类似于向量,但双端队列比向量更有效。在向量中,可以保证连续的存储分配,但对于双端队列可能并非如此。 Deque 是队列的特例,因为两端都允许插入和删除操作。
与双端队列相关的函数:
- push_front() :用于从前面推送容器中的元素。
- push_back() :用于从后面推送容器中的元素。
- front() :用于引用容器的第一个元素。
- back() :用于引用容器的最后一个元素。
什么是元组?
C++ 中的元组是用于将元素组合在一起的对象。在元组中,元素可以是相同的数据类型或不同的数据类型。元组的元素按照它们被访问的顺序进行初始化。
与元组关联的函数:
make_tuple() : make_tuple() 用于给元组赋值。传递的值应该与 tuple.2 中声明的值顺序一致。 get():get() 用于访问元组值并修改它们,它接受索引和元组名称作为参数来访问特定的元组元素。
如何访问元组?
要访问元组的元素,请使用 get<>()函数。
句法:
auto fistElement = get<0>(myTuple);
auto secondElement = get<1>(myTuple);
auto thirdElement = get<2>(myTuple);
本文重点介绍如何在 C++ 中创建元组的二维向量。
元组的双端队列
元组的双端队列是一个双端队列容器,其中每个元素本身就是一个元组。尽管为简单起见,一个元组可以包含更多或更少的元素,但我们只考虑了三个元素的元组。
句法:
deque
Here,
dataType1, dataType2 and dataType3 can be either similar or dissimilar data types
示例 1:下面是实现元组双端队列的 C++ 程序。
C++
// C++ program to demonstrate
// the working of deque
// of tuples
#include
using namespace std;
// Function to print deque elements
void print(deque >& myContainer)
{
cout << "myContainer elements: \n\n";
for (auto currentTuple : myContainer)
{
// Each element of the deque is
// a tuple itself
tuple tp =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(tp) << ', ' <<
get<1>(tp) << ', ' <<
get<2>(tp);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a deque of tuples
// of type {string, int, bool}
deque >
myContainer;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
22, true);
// Push the tuple at the front
// in the deque
myContainer.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("GFG",
33, false);
// Push the tuple at the back
// in the deque
myContainer.push_back(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("Java",
11, true);
// Push the tuple at the front
// in the deque
myContainer.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Python",
44, false);
// Push the tuple at the back
// in the deque
myContainer.push_back(tuple4);
// Calling print function
print(myContainer);
return 0;
}
C++
// C++ program to demonstrate
// the working of deque
// of tuples
#include
using namespace std;
// Function to print deque elements
void print(deque >& myContainer)
{
cout << "myContainer elements: \n\n";
for (auto currentTuple : myContainer)
{
// Each element of the deque is
// a tuple itself
tuple tp =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(tp) << ', ' <<
get<1>(tp) << ', ' <<
get<2>(tp);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a deque of tuples
// of type {string, float, bool}
deque >
myContainer;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
2.123, false);
// Push the tuple at the front
// in the deque
myContainer.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("GFG",
3.123, false);
// Push the tuple at the back
// in the deque
myContainer.push_back(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("Java",
1.123, true);
// Push the tuple at the front
// in the deque
myContainer.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Python",
4.123, true);
// Push the tuple at the back
// in the deque
myContainer.push_back(tuple4);
// Calling print function
print(myContainer);
return 0;
}
C++
// C++ program to demonstrate
// comparing of two deque
// of tuples
#include
using namespace std;
// Function to compare deque elements
bool compare(deque >& myContainer1,
deque >& myContainer2)
{
// If deques are of unequal size
if (myContainer1.size() !=
myContainer2.size())
return false;
// Iterators
// Initially pointing to the first elements
auto it1 = myContainer1.begin();
auto it2 = myContainer2.begin();
while (it1 != myContainer1.end() &&
it2 != myContainer2.end())
{
// Each element of the deque is
// a tuple itself
tuple tp1 = (*it1);
tuple tp2 = (*it2);
// Comparing corresponding tuples
if (get<0>(tp1) != get<0>(tp2) ||
get<1>(tp1) != get<1>(tp2) ||
get<2>(tp1) != get<2>(tp2))
return false;
it1++;
it2++;
}
return true;
}
// Driver code
int main()
{
// Declaring a deque of tuples
// of type {int, char, bool}
deque >
myContainer1;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple(10, 'g',
false);
// Push the tuple at the front
// in the deque
myContainer1.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple(20, 'd',
false);
// Push the tuple at the back
// in the deque
myContainer1.push_back(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(30, 'a',
true);
// Push the tuple at the front
// in the deque
myContainer1.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(40, 'k',
true);
// Push the tuple at the back
// in the deque
myContainer1.push_back(tuple4);
// Declaring another deque of tuples
// of type {int, char, bool}
deque >
myContainer2;
// Push the tuple at the front
// in the deque
myContainer2.push_front(tuple1);
// Push the tuple at the back
// in the deque
myContainer2.push_back(tuple2);
// Push the tuple at the front
// in the deque
myContainer2.push_front(tuple3);
// Push the tuple at the back
// in the deque
myContainer2.push_back(tuple4);
// Declaring another deque of tuples
// of type {int, char, bool}
deque >
myContainer3;
// Declaring a tuple
tuple tuple5;
tuple5 = make_tuple(1, 'a',
true);
// Push the tuple at the front
// in the deque
myContainer3.push_front(tuple1);
// Push the tuple at the back
// in the deque
myContainer3.push_back(tuple2);
// Push the tuple at the front
// in the deque
myContainer3.push_front(tuple3);
// Push the tuple at the back
// in the deque
myContainer3.push_back(tuple5);
// Calling compare function
if (compare(myContainer1, myContainer2))
cout <<
"myContainer1 and myContainer2 are equal.";
else
cout << "myContainer1 and " <<
"myContainer2 are not equal.";
cout << '\n';
// Calling compare function
if (compare(myContainer1, myContainer3))
cout << "myContainer1 and " <<
"myContainer3 are equal.";
else
cout << "myContainer1 and " <<
"myContainer3 are not equal.";
cout << '\n';
// Calling compare function
if (compare(myContainer2, myContainer3))
cout << "myContainer2 and " <<
"myContainer3 are equal.";
else
cout << "myContainer2 and " <<
"myContainer3 are not equal.";
return 0;
}
myContainer elements:
[ Java, 11, 1]
[ GeeksforGeeks, 22, 1]
[ GFG, 33, 0]
[ Python, 44, 0]
示例 2:下面是实现元组双端队列的 C++ 程序。
C++
// C++ program to demonstrate
// the working of deque
// of tuples
#include
using namespace std;
// Function to print deque elements
void print(deque >& myContainer)
{
cout << "myContainer elements: \n\n";
for (auto currentTuple : myContainer)
{
// Each element of the deque is
// a tuple itself
tuple tp =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(tp) << ', ' <<
get<1>(tp) << ', ' <<
get<2>(tp);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a deque of tuples
// of type {string, float, bool}
deque >
myContainer;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
2.123, false);
// Push the tuple at the front
// in the deque
myContainer.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("GFG",
3.123, false);
// Push the tuple at the back
// in the deque
myContainer.push_back(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("Java",
1.123, true);
// Push the tuple at the front
// in the deque
myContainer.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Python",
4.123, true);
// Push the tuple at the back
// in the deque
myContainer.push_back(tuple4);
// Calling print function
print(myContainer);
return 0;
}
myContainer elements:
[ Java, 1.123, 1]
[ GeeksforGeeks, 2.123, 0]
[ GFG, 3.123, 0]
[ Python, 4.123, 1]
比较元组双端队列的元素
本节重点比较两个元组双端队列的元素。可以通过迭代两个双端队列来比较两个双端队列的元素。
示例:在下面的程序中,我们创建了两个元组双端队列。元组的类型为 {int, char, bool}。我们使用比较函数逐个元素地比较两个双端队列。请注意,两个双端队列的每个元素本身都是一个元组,如果元组的相应数据对象相等,则认为两个元组相等。
C++
// C++ program to demonstrate
// comparing of two deque
// of tuples
#include
using namespace std;
// Function to compare deque elements
bool compare(deque >& myContainer1,
deque >& myContainer2)
{
// If deques are of unequal size
if (myContainer1.size() !=
myContainer2.size())
return false;
// Iterators
// Initially pointing to the first elements
auto it1 = myContainer1.begin();
auto it2 = myContainer2.begin();
while (it1 != myContainer1.end() &&
it2 != myContainer2.end())
{
// Each element of the deque is
// a tuple itself
tuple tp1 = (*it1);
tuple tp2 = (*it2);
// Comparing corresponding tuples
if (get<0>(tp1) != get<0>(tp2) ||
get<1>(tp1) != get<1>(tp2) ||
get<2>(tp1) != get<2>(tp2))
return false;
it1++;
it2++;
}
return true;
}
// Driver code
int main()
{
// Declaring a deque of tuples
// of type {int, char, bool}
deque >
myContainer1;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple(10, 'g',
false);
// Push the tuple at the front
// in the deque
myContainer1.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple(20, 'd',
false);
// Push the tuple at the back
// in the deque
myContainer1.push_back(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(30, 'a',
true);
// Push the tuple at the front
// in the deque
myContainer1.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(40, 'k',
true);
// Push the tuple at the back
// in the deque
myContainer1.push_back(tuple4);
// Declaring another deque of tuples
// of type {int, char, bool}
deque >
myContainer2;
// Push the tuple at the front
// in the deque
myContainer2.push_front(tuple1);
// Push the tuple at the back
// in the deque
myContainer2.push_back(tuple2);
// Push the tuple at the front
// in the deque
myContainer2.push_front(tuple3);
// Push the tuple at the back
// in the deque
myContainer2.push_back(tuple4);
// Declaring another deque of tuples
// of type {int, char, bool}
deque >
myContainer3;
// Declaring a tuple
tuple tuple5;
tuple5 = make_tuple(1, 'a',
true);
// Push the tuple at the front
// in the deque
myContainer3.push_front(tuple1);
// Push the tuple at the back
// in the deque
myContainer3.push_back(tuple2);
// Push the tuple at the front
// in the deque
myContainer3.push_front(tuple3);
// Push the tuple at the back
// in the deque
myContainer3.push_back(tuple5);
// Calling compare function
if (compare(myContainer1, myContainer2))
cout <<
"myContainer1 and myContainer2 are equal.";
else
cout << "myContainer1 and " <<
"myContainer2 are not equal.";
cout << '\n';
// Calling compare function
if (compare(myContainer1, myContainer3))
cout << "myContainer1 and " <<
"myContainer3 are equal.";
else
cout << "myContainer1 and " <<
"myContainer3 are not equal.";
cout << '\n';
// Calling compare function
if (compare(myContainer2, myContainer3))
cout << "myContainer2 and " <<
"myContainer3 are equal.";
else
cout << "myContainer2 and " <<
"myContainer3 are not equal.";
return 0;
}
myContainer1 and myContainer2 are equal.
myContainer1 and myContainer3 are not equal.
myContainer2 and myContainer3 are not equal.