C+中未定义的行为+;创建数组并从中检索数据时 我有不明确的行为,因为我是C++初学者,我甚至不知道如何解决这个问题。我在看,它显示了可能的错误。我唯一的猜测是我应该用malloc,也许 #include <iostream> using namespace std; int main() { int population[100]; for (int i = 0; i < 100; ++i) { population[i] = rand()%(10); } int firstTournament[2]; int secondTournament[2]; int randomNumbers[4] = {}; for (int i = 0; i < 4; ++i) { int indexOfIndividual = rand()%(101); randomNumbers[i] = indexOfIndividual; } for(int i = 0; i < 4; i++) { if(i < 2) { firstTournament[i] = population[randomNumbers[i]]; cout << "first tournament " << firstTournament[i] <<endl; } else { secondTournament[i] = population[randomNumbers[i]]; cout << "second tournament " << secondTournament[i] <<endl; } } for (int i = 0; i < 2; ++i) { // This is where the problem starts. It prints gibberish cout << "before tournament first " << firstTournament[i] << endl; cout << "before tournament second " << secondTournament[i] << endl; } return 0; }

C+中未定义的行为+;创建数组并从中检索数据时 我有不明确的行为,因为我是C++初学者,我甚至不知道如何解决这个问题。我在看,它显示了可能的错误。我唯一的猜测是我应该用malloc,也许 #include <iostream> using namespace std; int main() { int population[100]; for (int i = 0; i < 100; ++i) { population[i] = rand()%(10); } int firstTournament[2]; int secondTournament[2]; int randomNumbers[4] = {}; for (int i = 0; i < 4; ++i) { int indexOfIndividual = rand()%(101); randomNumbers[i] = indexOfIndividual; } for(int i = 0; i < 4; i++) { if(i < 2) { firstTournament[i] = population[randomNumbers[i]]; cout << "first tournament " << firstTournament[i] <<endl; } else { secondTournament[i] = population[randomNumbers[i]]; cout << "second tournament " << secondTournament[i] <<endl; } } for (int i = 0; i < 2; ++i) { // This is where the problem starts. It prints gibberish cout << "before tournament first " << firstTournament[i] << endl; cout << "before tournament second " << secondTournament[i] << endl; } return 0; },c++,arrays,undefined-behavior,C++,Arrays,Undefined Behavior,我如何处理这个问题?我做错了什么 我编译它运行 g++ -std=c++11 -c -I /stuff/include main.cc 程序的逻辑有错误。这是您填充第二次锦标赛的地方: for(int i = 0; i < 4; i++) { if(i < 2) { firstTournament[i] = population[randomNumbers[i]]; cout <&

我如何处理这个问题?我做错了什么

我编译它运行

g++ -std=c++11 -c -I /stuff/include main.cc

程序的逻辑有错误。这是您填充第二次锦标赛的地方:

for(int i = 0; i < 4; i++)
    {
        if(i < 2)
        {    
            firstTournament[i] = population[randomNumbers[i]];
            cout << "first tournament " << firstTournament[i] <<endl;
        }
        else
        {    
            secondTournament[i] = population[randomNumbers[i]];
            cout << "second tournament " << secondTournament[i] <<endl;
        }
    }

您正在打印未设置的
second锦标赛[0]
。相反,您正在设置
SecondTornament[2]
SecondTornament[3]
,这两个选项都是不允许的。也许你想要的是第二场比赛[i-2]=…你的程序逻辑有一个错误。这是您填充第二次锦标赛的地方:

for(int i = 0; i < 4; i++)
    {
        if(i < 2)
        {    
            firstTournament[i] = population[randomNumbers[i]];
            cout << "first tournament " << firstTournament[i] <<endl;
        }
        else
        {    
            secondTournament[i] = population[randomNumbers[i]];
            cout << "second tournament " << secondTournament[i] <<endl;
        }
    }

您正在打印未设置的
second锦标赛[0]
。相反,您正在设置
SecondTornament[2]
SecondTornament[3]
,这两个选项都是不允许的。也许您想要的是
secondcontraction[i-2]=…

标准库中有许多函数,它们完全可以执行您在这里尝试执行的操作

#include <iostream>
#include <random>
#include <algorithm>

int main()
{
    std::mt19937 gen{std::random_device{}()}; // or any other prng   
    std::uniform_int_distribution<int> dis(0, 9);

    int population[100];
    std::generate(population, population + 100, [&]{ return dis(gen); });

    int firstTournament[2];
    int secondTournament[2];

    std::sample(population, population + 100, firstTournament, 2, gen);
    std::sample(population, population + 100, secondTournament, 2, gen);

    for (int i = 0; i < 2; ++i)
    {
        std::cout << "before tournament first  " << firstTournament[i] << std::endl;
        std::cout << "before tournament second " << secondTournament[i] << std::endl;
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
std::mt19937 gen{std::random_设备{}()};//或任何其他prng
标准:均匀分布分布图(0,9);
国际人口[100];
std::generate(总体,总体+100,[&]{returndis(gen);});
国际锦标赛[2];
国际锦标赛[2];
标准::样本(总体,总体+100,第一次锦标赛,第二代);
标准::样本(总体,总体+100,第二次锦标赛,第2代);
对于(int i=0;i<2;++i)
{

标准库有许多函数,这些函数正是您在这里要做的

#include <iostream>
#include <random>
#include <algorithm>

int main()
{
    std::mt19937 gen{std::random_device{}()}; // or any other prng   
    std::uniform_int_distribution<int> dis(0, 9);

    int population[100];
    std::generate(population, population + 100, [&]{ return dis(gen); });

    int firstTournament[2];
    int secondTournament[2];

    std::sample(population, population + 100, firstTournament, 2, gen);
    std::sample(population, population + 100, secondTournament, 2, gen);

    for (int i = 0; i < 2; ++i)
    {
        std::cout << "before tournament first  " << firstTournament[i] << std::endl;
        std::cout << "before tournament second " << secondTournament[i] << std::endl;
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
std::mt19937 gen{std::random_设备{}()};//或任何其他prng
标准:均匀分布分布图(0,9);
国际人口[100];
std::generate(总体,总体+100,[&]{returndis(gen);});
国际锦标赛[2];
国际锦标赛[2];
标准::样本(总体,总体+100,第一次锦标赛,第二代);
标准::样本(总体,总体+100,第二次锦标赛,第2代);
对于(int i=0;i<2;++i)
{

STD::当代码< >代码> CUT>代码> RAND()101时,Cud:考虑< <代码> > <代码> >;<代码>可以返回<代码> 100代码> >索引> > int人口[100 ];< /C> >考虑<代码> > <代码> > <代码> CUD>代码> RAND()%(101);
可以返回
100
,这超出了索引
int population[100]的范围;