如何add/removeto/from向量<unique_ptr>?

How add/remove to/from vector<unique_ptr>?

我使用 unique_ptr 而不是 shared_ptr 因为它不是计数参考而且速度更快。您可以将其更改为 shared_ptr。我的问题是如何 add/remove 正确 to/from 智能指针向量? 关键行在这里:

CEO->add(std::move(headSales));
CEO->remove(headSales);

代码:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <vector>
#include <memory>
class Employee
{
protected:
    std::string name;
    std::string department;
    std::uint32_t salary;
    std::vector<std::unique_ptr<Employee>> subordinates;

public:
    Employee(std::string n, std::string d, std::uint32_t s) : name(n), department(d), salary(s)
    {}
    bool operator==(const Employee &e)
    {
        return name == e.name && department == e.department && salary == e.salary;
    }
    void add(std::unique_ptr<Employee> e)
    {
        subordinates.push_back(e);
    }
    void remove(const std::unique_ptr<Employee> e)
    {
        subordinates.erase(std::remove(subordinates.begin(), subordinates.end(), e), subordinates.end());
    }
    std::vector<std::unique_ptr<Employee>> getSubordinates() const
    {
        return subordinates;
    }
};
#endif  //EMPLOYEE_H


#include "Employee.h"


int main()
{
    std::unique_ptr<Employee> CEO = std::make_shared<Employee>("John", "CEO", 30000);
    std::unique_ptr<Employee> headSales = std::make_shared<Employee>("Robert", "Head Sales", 20000);
    CEO->add(std::move(headSales));
    CEO->remove(headSales);
}
std::unique_ptr<Employee> CEO = std::make_shared<Employee>("John", "CEO", 30000);
     ^^^^^^                               ^^^^^^

这是错误的,因为共享指针不能转换为唯一指针。目前尚不清楚您打算拥有共享所有权还是独有所有权。


 CEO->remove(headSales);
 subordinates.push_back(e);

这些都是错误的,因为您正在尝试复制一个唯一的指针。但是,唯一指针不可复制 - 否则它们将不会保持唯一。相反,您必须从原始指针移动:

subordinates.push_back(std::move(e));

首先,使用唯一指针作为 Employee::remove 的值参数是不明智的。下面详细介绍。


std::vector<std::unique_ptr<Employee>> getSubordinates() const
{
    return subordinates;
}

这次您要复制一个唯一指针向量。问题仍然存在——指针不可复制。您也不能移动向量,因为该函数是常量。也许您打算简单地授予读取权限。这可以通过返回引用来实现:

 const std::vector<std::unique_ptr<Employee>>& getSubordinates() const

CEO->add(std::move(headSales));
CEO->remove(headSales);

您正在使用您已经从中移出的指针。唯一指针的所有权被唯一转移到add,因此移动后使用它不再有意义。

正如我所提到的,使用唯一指针作为删除的按值参数毫无意义。为了拥有唯一的指针,您必须拥有该指针 - 唯一的。因此向量不可能拥有它。您可以改为引用向量中包含的指针,并使用它:

void remove(const std::unique_ptr<Employee>& e)

CEO->remove(CEO->getSubordinates().back());

You can change it to shared_ptr.

嗯,那会简单得多。将所有 unique 替换为 shared,并删除那个 std::move,程序将是 well-formed.


PS。您想要的 std::remove 在您忘记包含的 <algorithm> header 中。

PPS。我不清楚为什么一个员工应该拥有其他员工的任何所有权——独特的或共享的。我建议所有员工都属于其他东西 - 比如 Company,员工只有 non-owning 关联。我建议 std::weak_ptr.

您的代码不需要唯一或共享指针。

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <vector>
#include <memory>
class Employee
{
protected:
  std::string name;
  std::string department;
  std::uint32_t salary;
  std::vector<Employee> subordinates;

public:
  Employee(std::string n, std::string d, std::uint32_t s) : name(n), department(d), salary(s)
  {}
  bool operator==(const Employee &e) const
  {
    return name == e.name && department == e.department && salary == e.salary && subordinates == e.subordinates;
  }
  void add(Employee e)
  {
    subordinates.push_back(std::move(e));
  }
  void remove(Employee const& e)
  {
    subordinates.erase(std::remove(subordinates.begin(), subordinates.end(), e), subordinates.end());
  }
  std::vector<Employee> getSubordinates() const
  {
    return subordinates;
  }
};
#endif  //EMPLOYEE_H


#include "Employee.h"


int main()
{
  Employee CEO = {"John", "CEO", 30000};
  Employee headSales = {"Robert", "Head Sales", 20000};
  CEO->add(headSales);
  CEO->remove(headSales);
}

另外你的 == 应该是 const,它应该比较 subordinates。因为它现在是 Employee 的向量,所以递归调用 ==