![]() |
VOOZH | about |
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.
Some of the functions associated with a vector:
In C++, a 2D vector is a vector of vectors which means that each element of a 2D vector is a vector itself. It is the same as a matrix implemented with the help of vectors.
Some of the functions associated with a 2D vector:
Utility header in C++ provides us pair container. A pair consists of two data elements or objects.
To access elements of a pair use the dot (.) operator.
Syntax:
auto fistElement = myPair.first;
auto fistElement = myPair.second;
A 2D vector of pairs or vector of vectors of pairs is a vector in which each element is a vector of pairs itself.
Syntax:
vector<vector<pair<dataType1, dataType2>> myContainer
Here,
dataType1 and dataType2 can be similar or dissimilar data types.
Example 1: In the below C++ program, a vector of vectors of pairs of type {int, string} is used.
Output:
[ {0 , GeeksforGeeks} {1 , GFG} {2 , Computer Science} ]
[ {0 , C#} {1 , C} {2 , C++} ]
[ {0 , HTML} {1 , CSS} {2 , Javascript} ]
[ {0 , R} {1 , Swift} {2 , Python} ]
Example 2: In the below C++ program, a vector of vectors of pairs of type {char, bool} is used.
Output:
[ {G , 0} {e , 0} {e , 0} {k , 0} ]
[ {G , 1} {e , 1} {e , 1} {k , 1} ]
[ {G , 0} {e , 0} {e , 0} {k , 1} ]
[ {G , 0} {e , 1} {e , 0} {k , 1} ]
Time Complexity: O(N*M) where N is the size of the vector and M is the size of the pair.
Auxiliary Space: O(M)