C++ swap() 用于双端队列索引
我有两个问题,第二个是可选的。 首先,在下面的程序(一个简单的卡片程序的原型)中,我收到以下错误:
(29):错误 C2660:“shuffle”:函数不接受 1 个参数 使用以下代码:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <deque>
#include <algorithm>
using namespace std;
deque<int> cardDeck (51);
void flip(); //Prototype flip()
void shuffle(); //Prototype shuffle()
int _tmain(int argc, _TCHAR* argv[])
{
ostream& operator<<(ostream& os, deque<int> dq); //overload << operator to accept deque
//arguments
for (int a=52; a>0; a--) { //initialize the 52 cards in a deck
cardDeck.push_front(a);
}
flip(); //prompt my input to check data
return 0;
}
void flip() { //flip over card in specified location in the deck
int input;
cin >> input;
cout<<cardDeck[input]<<endl;
shuffle(cardDeck);
flip();
}
void shuffle(deque<int> dq) { //use Fisher-Yates algorithm to efficiently and accurately
//randomize card order
for(int i=dq.size()-1; i>-1; i--) {
int j = rand() % (i + 1);
if(i != j) {
swap(dq[j], dq[i]);
}
}
}
为什么我会收到此错误? (我环顾四周并尝试自己解决它)
其次,我不确定我是否正确执行了 Fisher-yates 算法,因为 C++ 文档不容易找到(对于使用 swap() 的版本) ;)(布朗尼点回答这个问题或指出任何可怕的编码实践,不包括缺少类)
提前致谢!
I have two questions, the second being optional.
First, in the program below (a prototype of a simple card program), I am getting the following error:
(29): error C2660: 'shuffle' : function does not take 1 arguments
with the following code:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <deque>
#include <algorithm>
using namespace std;
deque<int> cardDeck (51);
void flip(); //Prototype flip()
void shuffle(); //Prototype shuffle()
int _tmain(int argc, _TCHAR* argv[])
{
ostream& operator<<(ostream& os, deque<int> dq); //overload << operator to accept deque
//arguments
for (int a=52; a>0; a--) { //initialize the 52 cards in a deck
cardDeck.push_front(a);
}
flip(); //prompt my input to check data
return 0;
}
void flip() { //flip over card in specified location in the deck
int input;
cin >> input;
cout<<cardDeck[input]<<endl;
shuffle(cardDeck);
flip();
}
void shuffle(deque<int> dq) { //use Fisher-Yates algorithm to efficiently and accurately
//randomize card order
for(int i=dq.size()-1; i>-1; i--) {
int j = rand() % (i + 1);
if(i != j) {
swap(dq[j], dq[i]);
}
}
}
Why do I receive this error? (I have looked around and attempted to solve it myself)
Secondly, I'm not certain if I'm doing the fisher-yates algorithm properly because c++ documentation isn't easy to find on it (for the version that utilizes swap();) (Brownie points for answering this or pointing out any horribly awful coding practices, not including the lack of classes)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您收到该错误的原因是您声明
shuffle
作为不带任何参数的函数。另一个注意事项是,您可能希望在该函数中引用双端队列,否则您将打乱本地副本,并且不会产生所需的副作用。
您可能希望它看起来像这样:
此外,您可能希望使用
iter_swap
而不是swap
来交换元素。在出队中,它可能不会产生影响,但对于list
或map
则会产生影响。The reason you get that error is because you declare
shuffle
as a function not taking any arguments.Another note is that you probably want to take a reference to the deque in that function, otherwise you'll shuffle a local copy and won't have the desired side effect.
You probably want it to lok like this:
Also, you might want to use
iter_swap
instead ofswap
to swap the elements. In a dequeue it probably won't make a difference, but forlist
ormap
it would.我认为你忘记将参数放在函数声明中
应该是
I think you forgot to put the argument in your function declaration
should be
我认为问题在于,在程序的顶部,您已将“shuffle”原型化为
请注意,这不带任何参数。因为 C++ 使用一次性编译器,所以在调用 shuffle 时,这是唯一可用的 shuffle 声明,因为编译器稍后还没有看到实现。因此,它会给出上述错误,因为它认为您正在使用一个参数调用零参数函数。
要解决此问题,请更新原型,使其与您实际定义的函数匹配。
希望这有帮助!
I think that the problem is that at the top of your program you've prototyped `shuffle as
Notice that this takes no arguments. Because C++ uses a one-pass compiler, at the point that you call shuffle, this is the only declaration of shuffle available because the compiler hasn't seen the implementation later on. Consequently, it gives you the above error, because it thinks you are calling a zero-argument function with one argument.
To fix this, update the prototype so that it matches the function you've actually defined.
Hope this helps!