使用基于范围的for循环放置元素时获取随机数

问题描述 投票:0回答:0

我写了一个程序,用 CLion 在 C++ 中插入数字。

首先,输入一组正整数并将它们放入名为 pour 的向量中。 然后,取这些正整数的负数,然后将它们放入浇注中。 但是,当我尝试使用基于范围的 for 循环将负数插入倒数时,最后一个元素总是无缘无故地给出一个随机数。 但是一旦我不使用基于范围的循环,最后一个元素就不会给我一个随机数。

#include <iostream>
#include <vector>

using namespace std;

int n , m ;
vector<int> pour ;

int main()
{
    for (int i=0 ; i<3 ; ++i)
    {
        cin >> m ;
        pour.emplace_back(m) ;
    }
    for (auto i : pour)
    {
        pour.emplace_back(-i) ;
    }
    for (auto i : pour)
    {
        cout << i << endl ;
    }
    return 0;
}
c++ auto range-based-loop
© www.soinside.com 2019 - 2024. All rights reserved.