I have a std::map<CompositeKey, std::string>
, where CompositeKey
is a class I wrote.
This CompositeKey
has three int
data members, all the constructors, all the copy assignment operators and a friend bool operator<
, which compares the sum of the three data members.
I understood how to use emplace
and emplace_hint
.
For example:
// emplace
std::map<CompositeKey, std::string> my_map;
int first_id = 10, second_id = 100, third_id = 1000;
std::string my_string = "foo";
auto [ insertedIt, success ] = my_map.emplace(std::piecewise_construct,
std::forward_as_tuple(first_id, second_id, third_id),
std::forward_as_tuple(my_string));
// emplace_hint
first_id = 5, second_id = 50, third_id = 500;
my_string = "bar";
std::tie(insertedIt, success) = my_map.emplace_hint(insertedIt
std::piecewise_construct,
std::forward_as_tuple(first_id, second_id, third_id),
std::forward_as_tuple(my_string));
The only way I was able to use try_emplace
was in the version without hint:
first_id = 1, second_id = 10, third_id = 100;
my_string = "foobar";
std::tie(insertedIt, success) = my_map.try_emplace({first_id, second_id, third_id},
my_string);
My questions are:
- Is this the only way to call
try_emplace
, without hint? If not, how could I call it? - How could I call the
try_emplace
version with a hint? I made some attempts but always failed. - Is it correct to assume that
try_emplace
"move" or "copy" the CompositeKey inside the map? I am asking about the behaviour oftry_emplace
both because I read something similar on another discussion, and because I wrote a verbose copy and move constructors to make a test.
I am sorry, I made my research but do not understand these points from the cppreference documentation