术语不评估为1参数错误的函数?

时间:2018-05-08 23:05:18

标签: c++ algorithm function random shuffle

我写这段代码基本上是为了改变一个向量,我得到了这个错误,我不确定是什么问题。我已经包含了算法。谢谢!

// Shuffle the vector
random_shuffle(names.begin(), names.end(), rand());

// Prelims
cout << "ROUND PRELIMINATION: BEGIN" << endl;
cout << names[32] << " versus " << names[29] << endl << "Please enter the winner: ";
cin >> winner;
round1.push_back(winner);
cout << endl << names[33] << " versus " << names[30] << endl << "Please enter the winner: ";
cin >> winner;
round1.push_back(winner);
cout << endl << names[34] << " versus " << names[31] << endl << "Please enter the winner: ";
cin >> winner;
round1.push_back(winner);
for (int i = 0; i < 29; i++) {
    round1.push_back(names[i]);
}

1 个答案:

答案 0 :(得分:1)

random_shuffle的最后一个参数需要是一个返回随机选择值的函数对象。 rand()评估为int。因此,它不能用作函数的最后一个参数。

以下情况应该有效。

// Shuffle the vector
random_shuffle(names.begin(), names.end(), [](int n) { return rand()%n; });