Deque of Tuples in C++ with Examples
Last Updated :
05 Jan, 2022
What is deque?
In C++, a deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous storage allocation is guaranteed but this might not be the case with deques. Deque is the special case of a queue as the insertion and deletion operations are allowed at both ends.
Functions associated with a deque:
- push_front(): Used to push elements in the container from the front.
- push_back(): Used to push elements in the container from the back.
- front(): Used to refer to the first element of the container.
- back(): Used to refer to the last element of the container.
What is a Tuple?
A tuple in C++ is an object which is used to group elements together. In a tuple, elements can be of the same data type or different data types. The elements of tuples are initialized as in the order in which they will be accessed.
Functions associated with a tuple:
make_tuple(): make_tuple() is used to assign tuple with values. The values passed should be in order with the values declared in the tuple.2. get(): get() is used to access the tuple values and modify them, it accepts the index and tuple name as arguments to access a particular tuple element.
How to access a Tuple?
To access elements of a tuple use the get<>() function.
Syntax:
auto fistElement = get<0>(myTuple);
auto secondElement = get<1>(myTuple);
auto thirdElement = get<2>(myTuple);
This article focuses on how to create a 2D vector of tuples in C++.
Deque of tuples
Deque of tuples is a deque container in which each element is a tuple on its own. Though a tuple can contain more or fewer elements for simplicity we have considered tuples of three elements only.
Syntax:
deque<tuple<dataType1, dataType2, dataType3>> myContainer;
Here,
dataType1, dataType2 and dataType3 can be either similar or dissimilar data types
Example 1: Below is the C++ program to implement the deque of tuples.
C++
// C++ program to demonstrate
// the working of deque
// of tuples
#include <bits/stdc++.h>
using namespace std;
// Function to print deque elements
void print(deque<tuple<string,
int, bool> >& myContainer)
{
cout << "myContainer elements: \n\n";
for (auto currentTuple : myContainer)
{
// Each element of the deque is
// a tuple itself
tuple<string, int, bool> 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<tuple<string, int, bool> >
myContainer;
// Declaring a tuple
tuple<string, int, bool> 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<string, int, bool> 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<string, int, bool> 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<string, int, bool> 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;
}
OutputmyContainer elements:
[ Java, 11, 1]
[ GeeksforGeeks, 22, 1]
[ GFG, 33, 0]
[ Python, 44, 0]
Example 2: Below is the C++ program to implement deque of tuples.
C++
// C++ program to demonstrate
// the working of deque
// of tuples
#include <bits/stdc++.h>
using namespace std;
// Function to print deque elements
void print(deque<tuple<string,
float, bool> >& myContainer)
{
cout << "myContainer elements: \n\n";
for (auto currentTuple : myContainer)
{
// Each element of the deque is
// a tuple itself
tuple<string, float, bool> 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<tuple<string, float, bool> >
myContainer;
// Declaring a tuple
tuple<string, float, bool> 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<string, float, bool> 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<string, float, bool> 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<string, float, bool> 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;
}
OutputmyContainer elements:
[ Java, 1.123, 1]
[ GeeksforGeeks, 2.123, 0]
[ GFG, 3.123, 0]
[ Python, 4.123, 1]
Comparing elements of deques of tuples
This section focuses on comparing the elements of two deques of tuples. The elements of two deques of tuples can be compared by iterating over both deques.
Example: In the below program we have created two deques of tuples. Tuples are of type {int, char, bool}. We are comparing both deques element by element by using a compare function. Note that each element of both the deques is a tuple on its own and two tuples are considered equal if and if corresponding data objects of tuples are equal.
C++
// C++ program to demonstrate
// comparing of two deque
// of tuples
#include <bits/stdc++.h>
using namespace std;
// Function to compare deque elements
bool compare(deque<tuple<int, char,
bool> >& myContainer1,
deque<tuple<int, char,
bool> >& 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<int, char, bool> tp1 = (*it1);
tuple<int, char, bool> 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<tuple<int, char, bool> >
myContainer1;
// Declaring a tuple
tuple<int, char, bool> 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<int, char, bool> 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<int, char, bool> 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<int, char, bool> 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<tuple<int, char, bool> >
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<tuple<int, char, bool> >
myContainer3;
// Declaring a tuple
tuple<int, char, bool> 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;
}
OutputmyContainer1 and myContainer2 are equal.
myContainer1 and myContainer3 are not equal.
myContainer2 and myContainer3 are not equal.
Similar Reads
Deque of Pairs in C++ with Examples
What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s
5 min read
Map of Tuples in C++ with Examples
What is a tuple? A tuple in C++ is an object that has the ability to group a number of elements. The elements can be of the same type as well as different data types. The order in which tuple elements are initialized can be accessed in the same order. Functions associated with a tuple: 1. make_tuple
4 min read
Set of Tuples in C++ with Examples
What is a tuple?A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Operations on tuple:1. get(): get() is used to access the tuple values and modify the
4 min read
Multiset of Tuples in C++ with Examples
What is a tuple? A tuple in C++ is an object which binds a group of elements together. The elements can be similar as well as different data types. The elements of tuples are initialized as in the order in which they will be accessed. Syntax: tuple<data_type1, data_type2, dataType3, ....> myTu
5 min read
2D Vector of Tuples in C++ with Examples
What is Vector? In C++, a vector is similar to dynamic arrays with the ability to resize itself automatically. Vector elements are stored in contiguous memory locations so that they can be accessed and traversed using iterators. Functions associated with a vector: begin(): Returns an iterator pointi
6 min read
unordered set of tuples in C++ with Examples
What is a tuple? A tuple in C++ is an object which is used to group elements together. In a tuple, elements can be of the same data type or different data types. The elements of tuples are initialized as in the order in which they will be accessed. Functions associated with a tuple: 1. make_tuple():
6 min read
Priority Queue of Tuples in C++ with Examples
Priority Queue Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Functio
12 min read
Map of Vectors in C++ STL with Examples
Map in STL Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Vector in STL Vector is same as dynamic arrays with the ability to resize itself automatically when an element is insert
2 min read
Vector of Vectors in C++ STL with Examples
In C++, a vector of Vectors is a two-dimensional vector with a variable number of rows, where each row is a vector. Each index of a vector stores a vector that can be traversed and accessed using iterators. It is similar to an Array of Vectors but with dynamic properties. Syntax:C++vector<vector
4 min read
Multiset of Pairs in C++ with Examples
What is Multiset? A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element. Some of the functions associated with a multiset: begin(): Returns an iterator to the first element in the m
4 min read