矢量在C++中,我的代码可以工作,但不是我想要的那样
Vector in C++, my code works but not how I want it to?
所以我正在尝试制作一个基本程序,我想制作一个名为数字的向量,并用10个介于1到100之间的随机数填充它。这就是我迄今为止所拥有的;
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers;
int in = rand() % 100 + 1;
for (int i = 0; i < in; i++)
{
int n = rand() % 100 + 1;
numbers.push_back(n);
cout << "Number is: " << numbers[i];
system("pause");
return 0;
}
}
它只输出一个数字,我希望输出十个随机数字。
删除以下行
system("pause");
这将停止程序的继续。还要将return 0;
移动到main
中的最后一条语句,否则它将在第一次迭代后停止。
此外,如果您只想生成10
数字,则将其设置为循环的停止条件
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers;
const int in = 10; // Generate 10 random numbers
for (int i = 0; i < in; i++)
{
int n = rand() % 100 + 1;
numbers.push_back(n);
std::cout << "Number is: " << numbers[i] << std::endl;
}
return 0; // now you may return since the loop is complete
}
工作演示
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Declare and define a vector holding ints
vector<int> numbers;
// Run loop 10 times, increase i by one every time it's finished
for (int i = 0; i < 10; i++)
{
// Generate a random number, get the last 3 digits, add one
int n = rand() % 100 + 1;
// Push that number back to the the vector
// Like adding the number to a list
numbers.push_back(n);
// Print the current number from inside the vector
// by using the counter/index i using an array-like syntax
cout << "Number is: " << numbers[i] << std::endl;
}
// This executes the command pause, after it has been terminated,
// stop the execution, with "success" by returning 0
system("pause");
return 0;
}
这很可能是你想要的。
但是要注意,在这种情况下使用向量会产生一些开销。我会简单地打印n,而不是把数字推到向量上,然后再检索它。但由于这可能只是为以后更先进地使用矢量做准备,所以这应该没问题。
输出:
编号为:84编号为:87号码是:78编号为:16编号为:94编号为:36编号为:87编号为:93数字为:50编号为:22
return 0
将导致它在第一次迭代后终止。return
对C中的任何循环都没有意义,此语句用于终止函数。
如果代码正确缩进,您可能会立即看到这一点。缩进很重要。
此外,您并没有告诉它循环10次,而是告诉它循环in
次,而in
就是rand() % 100 + 1
。
在C和C++中,random使用种子来生成输出,事实上,如果你不给random种子,random会一次又一次地生成相同的数字。
你可以——也应该——用时间戳为它播种
#include"time.h"
srand(time(0));
之后,随机数将生成"真"随机数。
另外,你必须删除这两行:
system("pause");
return 0;
它在第一次迭代中打破了你的循环
以下想法对我很有用…
//Initialize in ctor
std::vector<int> vecI;
for (int i=0; i<100; ++i) vecI.push_back(i);
time_t seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(vecI.begin(), vecI.end(), std::default_random_engine(seed));
// in ctor, ok to display for debug, or disable for release
if(debugEnabled) {
for (int i=0; i<100; ++i)
std::cout << vecI[i] << " "; //
std::cout << "n" << std::endl;
}
在应用程序中,您可以对随机值进行恒定时间访问。
// easy to use in application
for (int i=0; i<10; ++i)
testWith (vecI[i]); // ten selections from values in range 0..99
// easy to test with all in-range values, in shuffled order
for (int i=0; i<100; ++i)
testWith (vecI[i]); // all values in shuffled order
// test with all in-range values, in order
for (int i=0; i<100; ++i)
testWith (i); // all values in order
您应该将system ("pause")
和return
放在循环之外,而不是放在内部
...
for () {...}
system ("pause");
return 0;
相关文章:
- 如何设置一个范围来提取我想要获得的信息
- 链接列表运算符重载没有打印出我想要的内容
- 莱克斯没有返回我想要的东西
- 而不是那么多的 if 语句,我想要一个逻辑,我可以用一个语句或优化的方式来完成
- 我想要一个具有子函数的函数访问相同的命名函数,而不使用它取决于其子类的类
- 如何让m_refcount变量打印出我想要的值而不是垃圾
- 我想要一个改变数组快速排序的2个数字的函数
- 输出不是我想要C++的
- 使用运算符覆盖排序没有得到我想要的
- C ++,否则文本程序不会进入我想要的途径
- 如何声明一个未知大小的数组,然后输入直到我想要,然后获取数组的大小
- 我如何使用 juce 的文件过滤器描述我想要的文件过滤器?
- 如何设置此视觉工作室 (2015) 自定义生成步骤(工具?基本上,我想要一个修改头文件(c ++)的预处理器步骤
- 代码不会发送我想要的权衡
- 无法在openGL和GLUT中为我想要的确切对象设置动画
- 我如何强迫Visual Studio使用我想要的依赖性库名称
- 我的 OpenGL QQuickItem 没有我想要的大小
- 矢量在C++中,我的代码可以工作,但不是我想要的那样
- 虽然功能不像我想要的那样工作
- visual c++ OpenSSL HMACSHA1工作,但不是我想要的