Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 阵列中的随机元素_C++_Arrays - Fatal编程技术网

C++ 阵列中的随机元素

C++ 阵列中的随机元素,c++,arrays,C++,Arrays,我正试着随机化我的一副牌。我想在底部函数中这样做,只是我得到了一个多字符数组>我试图检查目标元素是否为空,如果为空,它将通过不断检查目标[randomNumber]是否为空,将顶部卡从未缓冲的卡片组放入数组,依此类推。我该怎么做?我这样做对吗 // deck of cards // below are initializations #include <iostream> #include <fstream> #include <ctime> #include

我正试着随机化我的一副牌。我想在底部函数中这样做,只是我得到了一个多字符数组>我试图检查目标元素是否为空,如果为空,它将通过不断检查目标[randomNumber]是否为空,将顶部卡从未缓冲的卡片组放入数组,依此类推。我该怎么做?我这样做对吗

// deck of cards
// below are initializations
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>
using namespace std;

//globals
const int maxCards = 52;

//Structs
struct card {
char suit[8];
char rank[6];
int cvalue;
char location;
};

//Function List
char * strcopy(char destination[], const char source[]);
void shuffle(card destination[],card source[]);

//program
int main()
{

//begin seeding the time
srand(time(NULL));

//constants
bool gameOn =true;
int choice;
char tempSuit[8];
char tempRank[maxCards];

//create struct array
card deck[52]; 

//Shuffle function
void shuffle(card destination[],card source[])
{

int randomNumber;
for(int i=0;i<52;i++)
{
randomNumber = rand()%52;

while(destination[i].cvalue != '/0')
{
randomNumber = rand()%52;
{
destination[randomNumber].suit = source[i].suit;
destination[randomNumber].rank = source[i].rank;
destination[randomNumber].cvalue = source[i].cvalue;
}


}
//一副牌
//下面是初始化
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//全球的
常量int maxCards=52;
//结构
结构卡{
半诉讼[8];
字符秩[6];
int值;
字符位置;
};
//功能列表
char*strcopy(char destination[],const char source[]);
无效洗牌(卡目的地[],卡来源[]);
//节目
int main()
{
//开始播种时间
srand(时间(空));
//常数
bool gameOn=true;
智力选择;
char tempSuit[8];
字符tempRank[maxCards];
//创建结构数组
卡片组[52];
//洗牌功能
无效洗牌(卡目的地[],卡来源[])
{
整数随机数;

对于(inti=0;i只需使用
std::random_shuffle
(C++03)或
std::shuffle
(C++11):


我建议对代码进行缩进,以便于人们阅读。
card deck[52];
// init cards ...

std::random_shuffle(deck, deck + 52);
card deck[52];
// init cards ...

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

std::shuffle(std::begin(deck), std::end(deck), std::default_random_engine(seed));