C+中的背包回溯算法是否有一个替代move的解决方案+; 我用C++背包求解回溯解。我从这里又找到了解决办法。 我的编译器在行移动时出错。有没有不使用移动的解决方案?如何在不使用移动的情况下修改代码 #include<iostream> #include<vector> using namespace std; int weights[] = {2, 3, 1}, values[] = {6, 15, 7}; int solution = 0, n = 3; std::vector<int> vsol; std::vector<int> temp; bool issol; void Knapsack (int i, int max, int value) { for (int k = i; k < n; k++) { if ( max > 0) { if (weights[k] <= max) { temp.push_back(k); if (value+ values[k] >= solution) { solution = value + values[k]; issol = true; } } if ( (k+1) < n) { Knapsack (k+1, max - weights[k], value + values[k]); } else { if (issol == true) { if (! vsol.empty()) vsol.clear(); std::move(temp.begin(), temp.end(), std::back_inserter(vsol)); temp.clear(); issol = false; } else temp.clear(); return; } } else { if (issol == true) { if (! vsol.empty()) vsol.clear(); std::move(temp.begin(), temp.end(), std::back_inserter(vsol)); temp.clear(); issol = false; } else temp.clear(); return; } } } int main() { Knapsack(0, 2, 0); cout << "solution: " << solution << endl; for(vector<int>::iterator it = vsol.begin(); it != vsol.end(); it++) cout << *it << " "; return 0; } #包括 #包括 使用名称空间std; 整数权重[]={2,3,1},值[]={6,15,7}; int解=0,n=3; std::向量vsol; std::向量温度; 布尔伊索尔; 无效背包(整数i,整数max,整数值) { for(int k=i;k0) { if(权重[k]=解) { 溶液=值+值[k]; issol=真; } } 如果((k+1)

C+中的背包回溯算法是否有一个替代move的解决方案+; 我用C++背包求解回溯解。我从这里又找到了解决办法。 我的编译器在行移动时出错。有没有不使用移动的解决方案?如何在不使用移动的情况下修改代码 #include<iostream> #include<vector> using namespace std; int weights[] = {2, 3, 1}, values[] = {6, 15, 7}; int solution = 0, n = 3; std::vector<int> vsol; std::vector<int> temp; bool issol; void Knapsack (int i, int max, int value) { for (int k = i; k < n; k++) { if ( max > 0) { if (weights[k] <= max) { temp.push_back(k); if (value+ values[k] >= solution) { solution = value + values[k]; issol = true; } } if ( (k+1) < n) { Knapsack (k+1, max - weights[k], value + values[k]); } else { if (issol == true) { if (! vsol.empty()) vsol.clear(); std::move(temp.begin(), temp.end(), std::back_inserter(vsol)); temp.clear(); issol = false; } else temp.clear(); return; } } else { if (issol == true) { if (! vsol.empty()) vsol.clear(); std::move(temp.begin(), temp.end(), std::back_inserter(vsol)); temp.clear(); issol = false; } else temp.clear(); return; } } } int main() { Knapsack(0, 2, 0); cout << "solution: " << solution << endl; for(vector<int>::iterator it = vsol.begin(); it != vsol.end(); it++) cout << *it << " "; return 0; } #包括 #包括 使用名称空间std; 整数权重[]={2,3,1},值[]={6,15,7}; int解=0,n=3; std::向量vsol; std::向量温度; 布尔伊索尔; 无效背包(整数i,整数max,整数值) { for(int k=i;k0) { if(权重[k]=解) { 溶液=值+值[k]; issol=真; } } 如果((k+1),c++,c++11,c++builder,backtracking,knapsack-problem,C++,C++11,C++builder,Backtracking,Knapsack Problem,您可以使用进行更改。通常情况下,它们是不等效的,但在这里您使用的是int(因此没有性能损失),并且源数组立即被清除(temp.clear()) 所以你可以写: if (!vsol.empty()) vsol.clear(); std::copy(temp.begin(), temp.end(), std::back_inserter(vsol)); 事实上,这是经过深思熟虑的。最简单的是: vsol = temp; 清晰、快速,并可与C++98一起使用(取决于您的C++Builder版本,C

您可以使用进行更改。通常情况下,它们是不等效的,但在这里您使用的是
int
(因此没有性能损失),并且源数组立即被清除(
temp.clear()

所以你可以写:

if (!vsol.empty()) vsol.clear();
std::copy(temp.begin(), temp.end(), std::back_inserter(vsol));
事实上,这是经过深思熟虑的。最简单的是:

vsol = temp;
清晰、快速,并可与C++98一起使用(取决于您的C++Builder版本,C++11兼容性可能是一个问题)

修改后的版本:

#include <iostream>
#include <vector>

int weights[] = {2, 3, 1}, values[] = {6, 15, 7};

int solution = 0, n = 3;

std::vector<int> vsol;
std::vector<int> temp;

bool issol;

void Knapsack(int i, int max, int value)
{
  for (int k = i; k < n; ++k)
  {
    if (max > 0)
    {
      if (weights[k] <= max)
      {
        temp.push_back(k);
        if (value + values[k] >= solution)
        {
          solution = value + values[k];
          issol = true;
        }
      }

      if (k + 1 < n)
      {
        Knapsack(k + 1, max - weights[k], value + values[k]);
      }
      else
      {
        if (issol)
        {
          vsol = temp;
          issol = false;
        }

        temp.clear();
        return;
      }
    }
    else
    {
      if (issol)
      {
        vsol = temp;
        issol = false;
      }

      temp.clear();
      return;
    }
  }
}

int main()
{
  Knapsack(0, 2, 0);
  std::cout << "solution: " << solution << '\n';
  for(std::vector<int>::iterator it = vsol.begin(); it != vsol.end(); ++it)
    std::cout << *it << " ";

  return 0;
}
#包括
#包括
整数权重[]={2,3,1},值[]={6,15,7};
int解=0,n=3;
std::向量vsol;
std::向量温度;
布尔伊索尔;
无效背包(整数i,整数max,整数值)
{
对于(int k=i;k0)
{
if(权重[k]=解)
{
溶液=值+值[k];
issol=真;
}
}
if(k+1std::您是否包括
?您是否在编译器中启用了C++11模式?是的,我也尝试了实用程序,但没有任何区别。请提供一个问题(如果您主要想让
移动
正常工作)。我编辑了我的问题。我试图解释得更清楚。我希望现在一切正常。@Baum-mitAugen@xxxx如果它解决了您的问题,请接受答案。:)