Deque of Tuples in C++ with Examples
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; } |
myContainer 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; } |
myContainer 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; } |
myContainer1 and myContainer2 are equal. myContainer1 and myContainer3 are not equal. myContainer2 and myContainer3 are not equal.
Please Login to comment...