将C ++数组中的每个元素向左移动

时间:2016-09-16 18:34:02

标签: c++ arrays

我正在尝试编写一个函数删除一个C ++数组中的元素,也就是说,从索引开始将每个元素向左移动一个。

代码

#include <iostream>

using namespace std;

const int MAX_SIZE = 50;

void fill_array(char array[], int &current_size, const int max_size);

void print_array(const char array[], const int current_size);

int delete_index(char array[], int &current_size, int index);

int main () {

    char array[MAX_SIZE] = {' '};
    int current_size = 0;
    fill_array(array, current_size, MAX_SIZE);
    int index = 4;
    delete_index(array, current_size, index);
    cout << "After deleting element at index " << index << " the array is: ";
    print_array(array, current_size);

    return 0;
}

int delete_index(char array[], int &current_size, int index) {
    // Check input
    if (index > current_size) {
        cout << "Index must be between 0 and " << current_size - 1 << endl;
        return -1;
    }
    for (int i = index; i < current_size; i++) {
        cout << "array[" << index << "] = " << index << endl;
        array[index] = array[index + 1];
    }
    current_size--;
    cout << current_size << endl;
    return 0;
}

但是,输入“thisisnotworking”和索引4时得到的输出是:

Please input characters for the array (max of 50) or enter '*' to quit: 
onetwothree*
array[4] = 4
array[4] = 4
array[4] = 4
array[4] = 4
array[4] = 4
array[4] = 4
array[4] = 4
10
After deleting element at index 4 the array is: onetoothre

我不明白它没有增加for循环中的索引。

我在这里和其他地方读过类似的问题,但仍无法解决问题。知道为什么会这样吗?

注意:我只想使用数组(不是矢量等)。

4 个答案:

答案 0 :(得分:1)

  var ejs = require('ejs')

  it('should accept locals', function(done){
    var options = { name: 'tj', open: '{', close: '}' };
    ejs.renderFile('test/fixtures/user.ejs', options, function(err, html){
      if (err) return done(err);
      html.should.equal('<h1>tj</h1>');
      done();
    });
  })

您的循环计数器为 for (int i = index; i < current_size; i++) { cout << "array[" << index << "] = " << index << endl; array[index] = array[index + 1]; } 。在你的循环中,你应该使用i而不是i

修正:

index

答案 1 :(得分:0)

因为您需要array[index] = array[index + 1]时使用array[i] = array[i + 1] 这里也是一样:cout << "array[" << i << "] = " << i << endl;

答案 2 :(得分:0)

使用指针动态更改大小:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int* ClearElement(int array[], int index, const int& SizeOrig, int& NewSize)
{
    array[index] = -1;
    NewSize = SizeOrig;
    NewSize--;

    int* ptrArray = new int[NewSize];
    if(!ptrArray)
        throw "No more memory!";

    for(int i(0), j(0); i < SizeOrig; i++)
    {
        if(-1 != array[i])
        {
            ptrArray[j] = array[i];
            j++;
        }
    }
    return ptrArray;
}


int main()
{

    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int SizeOrig = (sizeof(array) / sizeof(int));
    int NewSize  = SizeOrig;

    int index;

    try{
            cout << "element index to clear: ";
            cin >> index;
            cout << endl;

            if(index < 0 || index > NewSize)
                throw "out of boundary index!";

            int* ptrArray = ClearElement(array, index, SizeOrig, NewSize);

            for(int i(0); i < NewSize; i++)
                cout << ptrArray[i] << ", "; 

            delete[] ptrArray;
        }
    catch(char* cp)
    {
        cout << cp << endl;
        exit(1);
    }
    catch(...)
    {
        cout << "sorry an error has happened!";
        exit(1);
    }


    cout << endl << endl << endl;
    return 0;
}

答案 3 :(得分:0)

您还可以使用算法中的std :: copy

int max = 10;
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int index = 4;
std::copy(array+index+1, array+max, array+index);