gpt4 book ai didi

c++ - 使用带有唯一指针 vector 的查找算法

转载 作者:行者123 更新时间:2023-12-02 09:51:14 25 4
gpt4 key购买 nike

我有一个独特的指针 vector 。我正在使用移动语义对其进行添加。这是局部类:

class execution_collection {
typedef std::unique_ptr<execution> exec_ptr;
typedef std::vector<exec_ptr> exec_ptr_vector;
exec_ptr_vector executions;
...
public:
...
double last_buy_fill_price(const std::string &) const;
...
};

当我尝试使用lambda std::find_if时,我得到 error: use of deleted function 'std::unique_ptr...。我不明白我要在哪里复制PTR?
double execution_collection::last_buy_fill_price(const string &symbol) const {

auto it = find_if(executions.begin(),executions.end(),
[&symbol](exec_ptr ptr){
return ptr->getSymbol() == symbol && ptr->getQuantity() > 0;
});

if (it != executions.end())
return it->get()->getPrice();
else
return 0;
}

最佳答案

您正在尝试复制unique_ptr,这违反了unique_ptr的规则。通过引用传递。

[&symbol](const exec_ptr & ptr)
{
return ptr->getSymbol() == symbol && ptr->getQuantity() > 0;
});

关于c++ - 使用带有唯一指针 vector 的查找算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com