In C++17, how do you declare and initialize a vector of pairs(or tuples) with an optional element?
std::vector<std::pair<int, optional<bool> > > vec1 = { {1, true}, {2, false}, {3, nullptr}};
I have a pair where the second element may be null/optional.
Here you go: #include <utility> vector<pair<int, int>> myVec (N, std::make_pair(-1, -1));
A 2D vector of pairs or vector of vectors of pairs is a vector in which each element is a vector of pairs itself. 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.
You are looking for std::nullopt
instead of nullptr
.
std::vector<std::pair<int, std::optional<bool> > > vec1 = { {1, true}, {2,false}, {3,std::nullopt} };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With