Reputation: 330
I want to use the std::fill
function from the algorithm library to fill in '-' in the second part of the pair. Is this possible to achieve?
#include <algorithm>
#include <map>
int main()
{
std::map<int, unsigned char> board(10);
// What I want the std::fill to do the equivalent of
for (auto& cell : board) {
cell.second = '-';
}
// What I've tried:
std::fill(board.begin(), board.end(), [&](auto &pair){pair.second = '-';});
}
Upvotes: 0
Views: 372
Reputation: 157504
fill
assigns to each element of the range it is called on, whereas you want to assign to the values of those elements (key-value pairs). From C++20 you can use the ranges::views::values
range adaptor:
std::ranges::fill(std::ranges::views::values(board), '-');
If you can't use C++20, your code will work by changing fill
to for_each
:
std::for_each(board.begin(), board.end(), [&](auto &pair){pair.second = '-';});
Upvotes: 1