Given an istream_iterator<int>
and multimap<char, int> output
.
I want to copy all the values into output
's 'a'
key.
How's the best way to handle that?
I had tried to use:
transform(
istream_iterator<int>(input),
istream_iterator<int>(),
begin(output),
[](const auto value){
return make_pair('a', value);
}
)
But I'm getting the error:
error: assignment of read-only member
std::pair<const char, int>::first
I think this means I can't write to begin(output)
. Is my only option to use for_each
?
You're very close, but you should use std::inserter
:
transform(
istream_iterator<int>(input),
istream_iterator<int>(),
inserter(output, begin(output)),
[](const auto value){
return make_pair('a', value);
}
);
The second parameter is a hint, but for multimap
it's going to be ignored. The interface demands that you provide it, though.
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