我正在尝试添加唯一的 map 指针。
auto monkey= std::unique_ptr<Animal> (new Animal(1,"Tom"));
std::map<int, std::unique_ptr<const Animal>> animals; //Map of animals
animals.insert(pair<int, unique_ptr<Animal>>(1,monkey)); // Error here
Error:error: no matching function for call to 'std::pair<int, std::unique_ptr<Animal> >::pair(int&, std::unique_ptr<Animal>&)'
添加它的方式是什么?
最佳答案
std::unique_ptr
不可复制,但可以移动,您必须对其进行std::move
。
您可以这样做:
animals.emplace(1, std::move(monkey));
关于c++ - 添加要映射的对象的唯一指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59680006/