矢量在C++中,我的代码可以工作,但不是我想要的那样

Vector in C++, my code works but not how I want it to?

本文关键字:我想要 工作 C++ 我的 代码      更新时间:2023-10-16

所以我正在尝试制作一个基本程序,我想制作一个名为数字的向量,并用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;