std::map::insert_or_assign with range

238 views
Skip to first unread message

Tomasz

unread,
Mar 7, 2015, 9:19:47 AM3/7/15
to std-pr...@isocpp.org
Hi Everyone,

I would like to propose extending this #N4279 proposal with one std::map::insert_or_assign overload. Consider code like follow:

std::map<int, int> foo { {1, 2}, {2, 3}, {3, 4} };
std::map<int, int> bar { {1, 5}, {4, 6}, {2, 7} };
    
for (const auto& v : bar)
  foo[v.first] = v.second;

As you can see we are going to insert or update values from bar to foo. To achieve that goal we may write it down in other ways, for instance:

std::for_each(std::begin(bar), std::end(bar), [&foo](const auto& v) { foo[v.first] = v.second; });

or (in assumption that bar is non const):

bar.insert(std::begin(foo), std::end(foo));
foo.swap(bar);

In my opinion it would be nice if std::map::insert_or_assign may take range and apply insert or update (assign) operation for each element from that range to destination map.
Similar overload is already defined in std::map::insert operation:

template <template InputIt>
void insert(InputIt first, InputIt last);

What do you think?

Best regards,
Tomasz Jaskólski (Satirev)
Reply all
Reply to author
Forward
0 new messages