C++ swap() 用于双端队列索引

发布于 2024-12-03 09:26:22 字数 1456 浏览 1 评论 0原文

我有两个问题,第二个是可选的。 首先,在下面的程序(一个简单的卡片程序的原型)中,我收到以下错误:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Saygoodbye 2024-12-10 09:26:22

您收到该错误的原因是您声明 shuffle 作为不带任何参数的函数。

void shuffle();

另一个注意事项是,您可能希望在该函数中引用双端队列,否则您将打乱本地副本,并且不会产生所需的副作用。

您可能希望它看起来像这样:

void shuffle(deque<int>& dq);

此外,您可能希望使用 iter_swap 而不是 swap 来交换元素。在出队中,它可能不会产生影响,但对于 listmap 则会产生影响。

The reason you get that error is because you declare shuffle as a function not taking any arguments.

void shuffle();

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:

void shuffle(deque<int>& dq);

Also, you might want to use iter_swap instead of swap to swap the elements. In a dequeue it probably won't make a difference, but for list or map it would.

铜锣湾横着走 2024-12-10 09:26:22

我认为你忘记将参数放在函数声明中

void shuffle();

应该是

void shuffle(deque<int> dq);

I think you forgot to put the argument in your function declaration

void shuffle();

should be

void shuffle(deque<int> dq);
猫七 2024-12-10 09:26:22

我认为问题在于,在程序的顶部,您已将“shuffle”原型化为

void shuffle();

请注意,这不带任何参数。因为 C++ 使用一次性编译器,所以在调用 shuffle 时,这是唯一可用的 shuffle 声明,因为编译器稍后还没有看到实现。因此,它会给出上述错误,因为它认为您正在使用一个参数调用零参数函数。

要解决此问题,请更新原型,使其与您实际定义的函数匹配。

希望这有帮助!

I think that the problem is that at the top of your program you've prototyped `shuffle as

void shuffle();

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文