random_shuffle 修改随机对象 (c++11)

random_shuffle modifies shuffled objects (c++11)

我有一个带有 Tour 对象的矢量,我想打乱它们。幸运的是有一个函数random_shuffle()。我打印洗牌前后的对象,但是,对象的一个​​字段根本没有洗牌。

首先想到的是复制或移动构造函数不正确。在两个构造函数中使用 cout 后,我​​发现使用了移动构造函数。奇怪的是,构造函数对我来说似乎是正确的。

代码和移动构造函数如下。回想一下,洗牌后只有一个字段不正确,即d_penalty。谁能帮我解决这个错误?

std::vector<Tour> tours;
// Fill vector with 4 tour objects

std::cout << "Print vector\n";
for (Tour const &tour: tours)
  std::cout << tour << std::endl;

std::cout << "Shuffle\n";
std::random_shuffle(tours.begin(), tours.end());

std::cout << "Print vector\n";
for (Tour const &tour: tours)
  std::cout << tour << std::endl;  

移动构造函数定义如下,

#include "tour.ih"

/**
 * Move constructor
 */

Tour::Tour(Tour &&other)
:
  d_capacity(other.d_capacity),
  d_demand(other.d_demand),
  d_feasible(other.d_feasible),
  d_length(other.d_length),
  d_time(other.d_time),
  d_penalty(other.d_penalty),
  d_tour(std::move(other.d_tour))
{ 
  cout << "Tour moved\n";
}

class定义如下,

class Tour
{
  // Current information about the tour
  int    d_capacity;    // Remaining capacity of the truck 
  int    d_demand;      // Total demand of all customers in the tour
  bool   d_feasible;    // Is the tour feasible in capacity
  double d_length;      // Length of the current tour
  double d_time;        // Driving time plus serving time
  double d_penalty;     // The penalty for infeasibility

  // The tour itself
  std::vector<City const *> d_tour;

  public:
    // Other functions

  private:
    // More functions
};

std::random_shuffle 交换元素。默认交换由 std::swap 实现,同时使用移动构造和移动赋值。

如果您没有移动赋值运算符,则可以使用复制赋值运算符。由于您的移动构造函数确实处理 d_penalty 正确,听起来您的复制赋值运算符没有正确实现。

一般来说,可以受益于移动语义的 class 应该同时具有移动构造函数和移动赋值运算符。在可能的情况下,特殊成员函数应定义为 = default;.

另外,std::random_shuffle 在 C++14 中被弃用,在 C++17 中被移除;您应该在 <random> header 中使用 std::shuffle 和一个 URNG,例如 std::mt19937.