C++ 随机与数组
C++ Random & Array
我尝试将随机函数用于不同的事情。现在它可以工作,但我有一个问题,我想在我的代码中随机生成F,但之后它们需要变成数组。我也需要使用它,如果我需要做它一个一个我认为我的代码会太长和混乱。你知道我该怎么做吗?
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
int t = rand() % 10 + 1 ;
cout << "There is "<< t << " Time;
int* F1 = new int [t];
int* F2 = new int [t];
int* F3 = new int [t];
int* F4 = new int [t];
int* F5 = new int [t];
cout << "Time per F: 0 Not available, 1 available;
//For F1
for(int i = 0; i < t; i++){
//random numbers 0 or 1:
F1[i] = rand() % 2 ;
}
cout << "The Time for F1 is ";
for(int a = 0; a < t; a++){
cout << " "<< F1[a] <<" ";
}
//For F2
for(int j = 0; j < t; j++){
//random numbers 0 or 1:
F2[j] = rand() % 2 ;
}
cout << "The Time slot for F2 is ";
for(int b = 0; b < t; b++){
cout << " "<< F2[b] << " ";
}
return 0;
}
谢谢
编辑:你给我的解决方案有助于找到解决方案
由于您使用的是c++,并且可以访问标准库,因此可以像std::vector:
那样编写得更干净、更安全。#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
srand(time(0));
//Random number from 3 to 7
int numberOfVectors = (rand() % 7) + 3;
//Random number from 1 to 10.
int size = (rand() % 10) + 1;
std::cout << "There are " << numberOfVectors << " vectors." << std::endl;
std::cout << "Each vector has " << size << " elements." << std::endl;
std::vector< std::vector<int> > vectorOfVectorOfInt;
std::cout << "Value per element: 0 = Not available, 1 = available" << std::endl;
for(int vec = 0; vec < numberOfVectors; vec++)
{
//Create a new vector with 'size' elements.
std::vector<int> newVector(size);
for(int i = 0; i < size; i++)
{
//Generate a random value between 0 and 50
newVector[i] = (rand() % 50);
}
//Add the vector to our vector-of-vectors.
vectorOfVectorOfInt.push_back(newVector);
std::cout << "The values for Vector #" << (vec+1) << " is:";
for(int b = 0; b < size; b++)
{
int value = vectorOfVectorOfInt[vec][b];
std::cout << "t" << value;
}
std::cout << std::endl;
}
return 0;
}
您可以运行它并在这里看到结果:http://ideone.com/RWQhjO
使用array:
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
int t = rand() % 10 + 1 ;
cout << "Time per F: 0 Not available, 1 available";
const int f_num = 5;
cout << "There is "<< t << "Time";
int* F = new int*[f_num];
for(int i = 0; i < f_num; i++){
F[i] = new int[t];
cout << "The Time for F"<<i<<" is :";
for(int j = 0; j < t; j++){
//random numbers 0 or 1:
F[i][j] = rand() % 2 ;
cout << " "<< F1[i][j] <<" ";
}
}
}
顺便说一句,你的代码很像c。在c++中,我们通常使用std::vector
而不是普通数组,使用http://en.cppreference.com/w/cpp/numeric/random而不是rand()。另外,不要使用using namespace std
。这会导致名称冲突。
我尝试对不同的事情使用随机函数。
大概是随机的。
现在它可以工作,但我有一个问题是,我想在我的代码中随机生成F,但它们需要成为数组。
您可以像生成t
的值一样生成该值。
const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
我也需要使用它,如果我需要一个接一个地做,我认为我的代码会太长,混乱。你知道我该怎么做吗?
你的代码已经太长太乱,并且有内存泄漏。您应该使用来自标准容器库的容器,而不是指向永远不会得到delete
d的指针的指针。
如果你使用std::map
或std::vector
,你的代码看起来可能是这样的:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
const int t = rand() % 10 + 1;
std::cout << "There is "<< t << " Time" << std::endl;
const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
std::map<int, std::vector<int> > f;
std::cout << "Time per F: 0 Not available, 1 available" << std::endl;
for (int f_slot = 1; f_slot <= number_of_fs; ++f_slot) {
for(int i = 0; i < t; i++){
//random numbers 0 or 1:
f[f_slot].push_back(rand() % 2);
}
std::cout << "The Time for F" << f_slot << " is " << std::endl;
std::copy(f[f_slot].begin(), f[f_slot].end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
}
看它运行!
当然,这些容器的内容的填充和打印可能应该提取到单独的函数中,但我不想完全重写您的代码
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- C++具有随机计数的数组
- 从C++数组中选择一个随机元素
- 为什么我的 2d 数组的第二行从 RAM 中获取随机值?
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 为什么我的数组值会更新为随机值?
- 在 c++ 的 10x10 数组中生成特定数量的多个随机字符
- SFINAE - 检测类型 T 是指针、数组还是带有随机访问运算符的容器,以及给定的值类型
- 动态数组传递给 main 中用随机值填充的函数,返回值3221226356
- 选择随机字符串数组的排序
- 如何在 3x4 数组中随机 4 个不同的位置
- 字符数组中的十六进制导致写入套接字时出现随机字符
- 更快地访问 C++ 数组中的随机元素
- 通过数组C 随机增加
- C++ 数组随机播放不起作用
- 将多维数组随机化(随机播放)为另一个 2D 数组
- 使用二维数组随机生成器objective c和c++
- 数组随机存取c++
- 多维数组随机漫步程序