【发布时间】:2018-09-18 06:15:17
【问题描述】:
我正在尝试按 cakeTypes 向量的重量大小对其进行排序。但是在排序实现中出现错误。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class CakeType
{
public:
const unsigned int weight_;
const unsigned int value_;
CakeType(unsigned int weight = 0, unsigned int value = 0) :
weight_(weight),
value_(value)
{}
};
bool compareCakes(const CakeType& cake1, const CakeType& cake2) {
return cake1.weight_ < cake2.weight_;
}
unsigned long long maxDuffelBagValue(const std::vector<CakeType>& cakeTypes,
unsigned int weightCapacity)
{
// calculate the maximum value that we can carry
unsigned cakeTypesSize = cakeTypes.size();
unsigned long long valueCalculator[weightCapacity+1][cakeTypesSize+1];
for (unsigned int i = 0; i<=weightCapacity+1; i++) {
valueCalculator[i][0] = 0;
}
for (unsigned int i = 0; i<=cakeTypesSize+1; i++) {
valueCalculator[0][i] = 0;
}
vector<CakeType> sortedCakeTypes(cakeTypes);
sort(sortedCakeTypes.begin(), sortedCakeTypes.end(), compareCakes);
return 0;
}
这是错误的一部分:
以非零代码 (1) 退出。
在 solution.cc:1 中包含的文件中:
在 /usr/include/c++/v1/iostream:38:38 中包含的文件中:
在 /usr/include/c++/v1/ios:216 包含的文件中:
在 /usr/include/c++/v1/__locale:15:
中包含的文件中 在 /usr/include/c++/v1/string:439:
中包含的文件中 /usr/include/c++/v1/algorithm:3856:17: 错误:没有匹配函数调用'swap'swap(*__first, *__last); ^~~~
我试过这个解决方案sort() - No matching function for call to 'swap',但不是同一个问题。
【问题讨论】:
标签: algorithm sorting c++11 quicksort swap