I have the following test code:
class Person{};
shared_ptr<Person> sp1;
shared_ptr<Person> sp2;
shared_ptr<Person> sp3;
vector<shared_ptr<Person>> members = {sp1,sp2,sp3};
map<string, shared_ptr<Person>> mymap;
How can I set the vector members
to the second element of mymap
? I tried with for
and copy
.
map
is an associative container, so you need to specify the key in string
type to associate it to the shared_ptr<Person>
. I don't know what you need, but the following loop should work:
for (size_t i = 0; i < members.size(); ++i) {
const std::string key = "sp" + std::to_string(i+1);
mymap[key] = members[i];
}