JGerulskis
JGerulskis

Reputation: 808

Integer Array Slicing

I was reading through this thread Array slicing in c++ and came close to what I was looking for but not exactly. I want to remove the first 3 elements from an int array.

What I Have Now:

void shiftArray(int *arr, int size) {
    for (int i = 0; i < size - 3; i++) {
        arr[i] = arr[i + 3];
        arr[i + 3] = -1; // can't re size array so set them to -1
    }
}

What I Want:

int* shiftArray(int *arr, int size) { // return type can stay void if possible
    return arr[3::]; // python
}

Is it possible to have a non iterative approach to shift the first three elements to the end with value -1 and move the rest of the elements up to take their place?

Upvotes: 0

Views: 3247

Answers (3)

M.M
M.M

Reputation: 141554

It sounds like you want to remove the first 3 elements in-place, using a resizeable array.

A resizeable array in C++ is called std::vector. The constructs std::array, and C-style arrays, are not resizeable.

In generic form the code could be:

template<size_t N, typename T>
void erase_start( std::vector<T> &arr )
{
    if ( arr.size() <= N )
        arr.clear();
    else
        arr.erase( arr.begin(), arr.begin() + N );
}

There isn't a pre-potted vector member function to erase based on count, it only takes iterators. But you could make such a function easily enough.

The invocation could look like:

std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7 };
erase_start<3>(vec);

Upvotes: 1

user5478212
user5478212

Reputation:

use this function it simply delete the old one and return new int array pointer by shifted cells, I want just mention something shiftStartIndex is where you want to shift from until end of array so the new size of array will be (size - shiftStartIndex) this prevent array to get out of index, I hope this be useful.

int *shiftArray(int *, int, int);

int main()
{
    int size = 6;
    int *b = new int[size]{ 1, 2, 3, 4, 5, 6 };

    int shiftStartIndex = 3;

    b = shiftArray(b, size, shiftStartIndex);

    int newSize = size - shiftStartIndex;

    for (int i = 0; i < newSize; i++)
    {
        cout << b[i] << " ";
    }

    cin.ignore();
    return 0;
}

int *shiftArray(int *arr, int size, int stIndex)
{
    int *a = new int[size - stIndex];

    for (int i = 0; i < size - stIndex; i++)
    {
        a[i] = arr[i + stIndex];
    }

    delete[] arr;

    return a;
}

Upvotes: 0

marcinj
marcinj

Reputation: 49986

You can use std::rotate and std::fill:

std::fill( std::rotate(arr, std::begin(arr) + 3, std::end(arr)), std::end(arr), -1);

http://coliru.stacked-crooked.com/a/b3e0557ee0481162

... but, its not as elegant as php :-)

[edit]

above requires C++11 below is compatible with older C++ standard versions:

template<typename T, size_t n>
void left_shift(T (&arr)[n], size_t left_shift){
    assert(left_shift < n);
    std::fill( std::rotate(arr, arr + left_shift, arr + n), arr + n, -1);
}

left_shift(arr, 3);

http://coliru.stacked-crooked.com/a/c09c27e3ebd60952

Upvotes: 3

Related Questions