23 using Objects = std::vector<std::pair<int, int>>;
24 Objects objects{ { 1, 3 }, { 2, 2 }, { 3, 65 }, { 1, 1 }, { 2, 2 },
25 { 4, 3 }, { 1, 1 }, { 10, 23 } };
26 const int capacity = 6;
27 auto size = [](std::pair<int, int> object) {
return object.first; }
29 auto value = [](std::pair<int, int> object) {
return object.second; }
33 std::cout <<
"Knapsack 0 / 1" << std::endl;
35 std::back_inserter(result), size, value);
37 std::cout <<
"Max value " << maxValue.first <<
", Total size "
38 << maxValue.second << std::endl;
39 for (
auto o : result) {
40 std::cout <<
"{ size = " << o.first <<
", value = " << o.second <<
"} ";
42 std::cout << std::endl;
int main()
[Knapsack Example]
auto knapsack_0_1(Objects &&objects, detail::FunctorOnRangePValue< ObjectSizeFunctor, Objects > capacity, OutputIterator out, ObjectSizeFunctor size, ObjectValueFunctor value=ObjectValueFunctor{})
Solution to Knapsack 0/1 problem.