gpt4 book ai didi

C++ 对 shared_ptr 的 vector 进行排序

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:51 25 4
gpt4 key购买 nike

我正在尝试将 shared_ptrs 的 vector 排序为 Food 对象。食品类别定义为:

class Food {
private:
// Human-readable description of the food, e.g. "all-purpose wheat
// flour". Must be non-empty.
std::string _description;

// Human-readable description of the amount of the food in one
// sample, e.g. "1 cup". Must be non-empty.
std::string _amount;

// Number of grams in one sample; must be non-negative.
int _amount_g;

// Energy, in units of kilocalories (commonly called "calories"), in
// one sample; must be non-negative.
int _kcal;

// Number of grams of protein in one sample; most be non-negative.
int _protein_g;

public:
Food(const std::string& description,
const std::string& amount,
int amount_g,
int kcal,
int protein_g)
: _description(description),
_amount(amount),
_amount_g(amount_g),
_kcal(kcal),
_protein_g(protein_g) {

assert(!description.empty());
assert(!amount.empty());
assert(amount_g >= 0);
assert(kcal >= 0);
assert(protein_g >= 0);

}
const std::string& description() const { return _description; }
const std::string& amount() const { return _amount; }
int amount_g() const { return _amount_g; }
int kcal() const { return _kcal; }
int protein_g() const { return _protein_g; }

};

使用

// Alias for a vector of shared pointers to Food objects.
using FoodVector = std::vector<std::shared_ptr<Food>>;

我的排序算法是:

std::unique_ptr<FoodVector> greedy_max_protein(const FoodVector& foods,
int total_kcal)
{
std::unique_ptr<FoodVector> result(new FoodVector);
int result_cal = 0;
sort(foods.begin(), foods.end(), sortByProtein); //sorting error
...

此处的排序函数出现错误 ^^ 而我的 sortByProtein 函数是:

bool sortByProtein(const std::shared_ptr<Food>&lhs, const std::shared_ptr<Food>&rhs)
{
return lhs->protein_g() > rhs->protein_g();

}

我不断得到二进制'='找不到运算符,它采用左手操作数类型'const std::shared_ptr'或者没有可接受的转换。我试过创建自己的排序函数,但我得到了同样的错误。我需要在我的课上重载 operator= 吗?如果是这样,我该怎么做?任何帮助将不胜感激!

编辑

通过创建一个新指针解决了这个问题:

FoodVector *sorted = new FoodVector(foods);

谢谢!

最佳答案

我砍掉并隔离了问题。在删除“常量”之前,我遇到了与您遇到的相同的错误。但这可以编译。

#include <iostream>

//:For: std::vector
#include <vector>

//:For: std::shared_ptr
#include <memory>

//:For: std::sort
#include<algorithm>

class Food{ /* SomeLogicHere */ };
typedef std::vector<std::shared_ptr<Food>> FoodVector;

bool operator==(
std::shared_ptr<Food> lhs,
std::shared_ptr<Food> rhs
){
return true; //TODO: Actual Comparison Logic
}

bool sortByProtein(
std::shared_ptr<Food> lhs,
std::shared_ptr<Food> rhs
) {
//:Dont Care about implementation.
//:Just want minimal example that
//:gets the error.
return false;

}

std::unique_ptr<FoodVector> greedy_max_protein(
FoodVector foods,
int total_kcal
){

sort(foods.begin(),foods.end(),sortByProtein);

}

int main()
{
std::cout<<"Hello World";

return 0;
}

关于C++ 对 shared_ptr 的 vector 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606934/

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