In C++, if a function returns a std::pair<int, int>
, we can auto-receive it as follows:
auto pr = some_function();
std::cout << pr.first << ' ' << pr.second;
Now, C++17 standard provides a beautiful way of unpacking this pair directly into separate variables as follows:
auto [x, y] = some_function();
std::cout << x << ' ' << y;
And then there is std::minmax_element()
library function, which returns a pair of iterators. So if I pass a vector<int>
to this function, it gives me back pair of iterators pointing to the smallest and the largest element in the vector.
Now one way I can accept these iterators is as usual, and then de-reference them later as follows.
std::vector<int> v = {4,1,3,2,5};
auto [x, y] = std::minmax_element(v.begin(), v.end());
std::cout << (*x) << ' ' << (*y); // notice the asterisk(*)
Now my question is: Is there any way to de-reference them while unpacking? Or more precisely, given the following code, can I replace var1
and var2
with something that is valid C++ and prints the value pointed by those iterators?
std::vector<int> v = {4,1,3,2,5};
auto [var1, var2] = std::minmax_element(v.begin(), v.end());
std::cout << var1 << ' ' << var2; // notice there is NO asterisk(*)