C++:尝试用double填充动态向量时的错误

Red*_*ter 3 c++ vector

我在课堂上遇到的课程遇到了麻烦,连老师都找不到问题.我们正在做一个程序,要求用户输入double然后当他停止时,它会扫描数组并将正面和负面分开,将它们放在不同的数组中.

我们注意到,当我们使用float时,程序工作更多的数字,但如果我们输入太多仍然是bug,如果我们只使用几个数字后使用双重bug.我的意思是错误,程序运行良好但是当它显示结果时,数组中有一些奇怪的数字.这是使用double的代码:

#include <iostream>
using namespace std;

void filling(double *, int &);
void sortPositiveNegative(double *, double *, double *, int, int &, int &);
void display(const double *, int);

int main () {
    double * vecteur = new double;
    double * positive = new double;
    double * negative = new double;
    int counter = 0, counterPos = 0, counterNeg = 0;

    cout << "Filling of the real number vector " << endl;
    filling(vecteur, counter);

    cout << endl << "Display of the real number vector " << endl;
    display(vecteur, counter);

    cout << endl << "Sort of the positive and negative in the real number vector: " << endl;
    sortPositiveNegative(vecteur, positive, negative, counter, counterPos, counterNeg);

    cout << endl << "Display of the positive real number : " << endl;
    display(positive, counterPos);

    cout << endl << "Display of the negative real number : " << endl;
    display(negative, counterNeg);

    system("PAUSE");
    return 0;
}

void filling (double *vecteur, int &counter)
{
    bool validation;
    char choice = 'Y';
    do
    {
        do
        {
            validation = true;
            cout << "Please enter the value of case " << counter+1 << ": ";
            cin >> vecteur[counter];
            if(cin.fail())
            {
                cerr << "The number entered is not valid." << endl;
                cin.clear();
                validation = false;
            }
            while(cin.get() != '\n'){}
        }while(!validation);
        counter++;
        do
        {
            validation = true;
            cout <<"Do you wish to continue? (Y/N): ";
            cin >> choice;
            if(toupper(choice) != 'Y' && toupper(choice) != 'N')
            {
                cerr << "We don't understand your choice, please try again." << endl;
                cin.clear();
                validation = false;
            }
            while(cin.get() != '\n'){}
        }while(!validation);
    }
    while(toupper(choice)=='Y');
}

void sortPositiveNegative(double *vecteur, double *positive, double *negative, int counter, int &counterPos, int &counterNeg)
{
    int i = 0;
    for(i; i<counter;i++)
    {
        if(vecteur[i] >= 0)
            positive[counterPos++] = vecteur[i];
        else
            negative[counterNeg++] = vecteur[i];
    }
}

void display (const double *vecteur, int counter)
{
    for(int i = 0; i<counter;i++)
        cout << vecteur[i]<<endl;
    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

我的老师认为这可能是一个记忆问题,但我们不知道为什么会这样做.

提前致谢.

小智 7

肯定存在内存问题,我不知道如何使用float它来修复它.例如,下面的代码行只分配了一个 double而不是double数组:

double * vecteur = new double;
Run Code Online (Sandbox Code Playgroud)

然后,您使用vecteur它就好像它是一个N元素的数组.这会触发未定义的行为.

要修复它,您必须根据需要分配一个尽可能多的值数组.例如,假设你需要10,然后你像这样分配10:

double * vecteur = new double[10];
Run Code Online (Sandbox Code Playgroud)

但是,如果您事先不知道元素的数量,则每次要添加元素时都需要扩展数组.如果你用C写这个,我建议你使用realloc().但是考虑到你使用C++,只需坚持下去std::vector<double>,就会自动管理内存.例如:

#include <vector>

int main()
{
    std::vector<double> vecteur; // Use vector to store array of doubles.

    // Add as many elements as you want.
    // Vector will resize itself if/when needed.
    vecteur.push_back(.1);
    vecteur.push_back(.2);
    vecteur.push_back(.3);
    vecteur.push_back(.4);
    vecteur.push_back(.5);
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.祝好运!