无法在数组C ++的前面添加数字

时间:2014-09-27 20:26:46

标签: c++ arrays

我在将值插入数组时遇到问题。我试图先输入0然后输入1然后输入2,但是当我输出数组应该是什么样子时。它显示为2,0,1。它应该是2,1,0。

任何帮助?

//add x to front of list
void addFront(THING x)
{
    THING temp;
    if (numarr >= size) {
        //cout <<"Error, you're doing something stupid, out of room" << endl;
        //step 1: create a new, bigger array, copy items over
        resize(2 * size);

        if (numarr != 0) {
            back++;
            temp = myarr[front];
            myarr[front] = x;
            myarr[back] = temp;

            numarr++;
        } else {
            myarr[front] = x;
            numarr++;

        }
    } else {
        if (numarr != 0) {
            back++;
            temp = myarr[front];
            myarr[front] = x;
            numarr++;
            myarr[back] = myarr[numarr];
            myarr[front + 1] = temp;


        } else {
            myarr[front] = x;
            numarr++;

        }
    }
}

0 个答案:

没有答案