如何对unique_ptr向量进行排序
How can I sort a vector of unique_ptr?
我声明了一个向量,如下所示:vector<unique_ptr<Worker>> Workers
. Worker
是具有私有字段name
的基类,它有两个派生类:Builder
和 Driver
。
我添加到 Builder
和 Driver
的Workers
矢量对象,然后我想通过使用这样的#include <algorithm>
name
对矢量进行排序:
sort(Workers.begin(), Workers.end(), cmp_by_name);
bool cmp_by_name(const Worker &a, const Worker &b)
{
return a.getName() < b.getName();
}
但是VS编译器说:
错误 1 错误 C2664: 'bool (const Worker &,const Worker &)' :无法将参数 2 从 'std::unique_ptr>' 转换为 'const Worker &' c:\program files (x86)\Microsoft Visual Studio 12.0\vc\include\algorithm 3071 1 应用
如何修复此错误?
感谢@NathanOliver、@Rabbid76和这个问题,我将cmp_by_name
编辑成以下形式:
struct cmp_by_name
{
inline bool operator()(const unique_ptr<Worker>& a, const unique_ptr<Worker>& b)
{
return a->getName() < b->getName();
}
};
我像这样调用排序函数:
sort(Workers.begin(), Workers.end(), cmp_by_name());
std::sort
使用的比较函数需要采用以下形式:
bool cmp(const Type1 &a, const Type2 &b);
这里的类型Type1
和Type2
必须能够取消引用迭代器,然后隐式转换为它们。
在您的情况下,取消引用Workers.begin()
会给您一个unique_ptr<Worker>
而不是Worker
。您需要更改比较函数才能进行const unique_ptr<Worker>&
。
在这种情况下,它最终看起来像:
bool cmp_by_name(const std::unique_ptr<Worker>& a, const std::unique_ptr<Worker>& b)
{
return a->getName() < b->getName();
}
std::vector<std::unique_ptr<Worker>>
的数据类型是 std::unique_ptr<Worker>
,所以你的比较函数必须看起来像这样:
bool cmp_by_name(const std::unique_ptr<Worker> &a, const std::unique_ptr<Worker> &b)
{
return a->getName() < b->getName();
}
比较函数期望参数,以便std::vector
的对象可以转换为它们。
从 C++11 开始,您还可以使用 lambda 表达式而不是定义比较函数:
int main()
{
using workers_t = std::unique_ptr<Worker>;
std::vector<workers_t> Workers;
Workers.emplace_back(std::make_unique<Worker>(Worker("Paul")));
Workers.emplace_back(std::make_unique<Worker>(Worker("Anna")));
Workers.emplace_back(std::make_unique<Worker>(Worker("John")));
std::sort(std::begin(Workers), std::end(Workers), [](const workers_t& a, const workers_t& b) {
return a->getName() < b->getName();
});
for (auto const &worker : Workers)
std::cout << worker->getName() << std::endl;
return 0;
}
注意:为了清楚起见,此示例直接使用Worker
对象,但它也适用于派生的Builder
和Driver
对象。
Ideone上的代码
- 将结构向量排序为子组
- C++数组与向量排序(在我的情况下,向量比数组慢~2.5倍(无优化))
- 如何在对向量排序后更改索引值?c++
- C++向量排序给出0作为输出
- 将许多向量排序在一起
- 根据一个向量对多个向量排序
- 编译错误向量排序和联合
- C++我自己的函数进行向量排序
- 将字符串的向量排序为日期"yyyymmdd"
- C++ 通过使用旧向量进行预排序来改进向量排序
- C 向量排序 .h .cpp中的单独文件
- 向量排序-c++
- 将向量排序到一个无序映射c++11中
- 向量的向量排序
- c++向量排序方法编译失败,返回预期表达式
- 如何对bitset向量排序
- 向量排序的基础上只有先
- 我如何以相同的方式对两个向量排序,而条件只使用其中一个向量
- 列表排序和结构体向量排序之间的性能差距.c++
- 如何基于第二个字符串对字符串向量的向量排序