Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Copy Into a multimap

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?

like image 255
Jonathan Mee Avatar asked Mar 10 '23 20:03

Jonathan Mee


1 Answers

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.

like image 57
krzaq Avatar answered Mar 21 '23 07:03

krzaq