4

In c++ I took a dynamic array of n elements

int* a = new int[n];

After shifting left all element of that array, the last element, i.e a[n-1] is useless, and I want to delete it. And after shifting right I need to delete the first element of array, and to have a pointer to second element, i.e I need to make an array with length of n-1. How can I do that?

3
  • 4
    C++ has no new/delete equivalent of C's realloc. So the short answer is, you don't. You make a new array, copy, delete [] the old, etc. Or you do it the way you should in the first place: use a std::vector<int>. Of course, you're keeping track of n somewhere. There's nothing stopping you from just decrementing the n cap and ignoring that you have one extra, now-unused slot. Commented Sep 28, 2019 at 10:30
  • 1
    If you compare your approach with std::vector: std::vector keeps two values: size and capacity where size is the number of elements in use while capacity is the number of elements allocated. So, deleting the last element with e.g. std::vector::pop_back() may result in decrementing size only (a quite cheap operation). Of course, size must always be <= capacity. For enlarging reallocation, std::vector might reallocate storage for more than required elements that can reduce the number of necessary reallocations. (Allocation is considered as expensive operation.) Commented Sep 28, 2019 at 10:49
  • the "dynamic" in "dynamic array" only refers to the fact that you choose the size at runtime, however once created the size is fixed. If you want a "dynamic array" as in "size can change dynamically" you need to use a std::vector (or build one yourself, not recommended) Commented Sep 28, 2019 at 10:55

1 Answer 1

1

You need to allocate a new array and copy elements of the original array to the new array.

Here is a demonstrative program

#include <iostream>
#include <utility>
#include <algorithm>

size_t shift_left( int * &a, size_t n )
{
    if ( n )
    {
        int *p = new int [n-1];

        std::copy( a + 1, a + n, p );

        std::swap( a, p );

        delete []p;
    }

    return n == 0 ? n : n - 1;
}

size_t shift_right( int * &a, size_t n )
{
    if ( n )
    {
        int *p = new int [n-1];

        std::copy( a, a + n - 1, p );

        std::swap( a, p );

        delete []p;
    }

    return n == 0 ? n : n - 1;
}

int main() 
{
    size_t n = 10;      
    int *a = new int[n] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for ( const int *p = a; p != a + n; p++ )
    {
        std::cout << *p << ' ';
    }

    std::cout << '\n';

    n = shift_left( a, n );

    for ( const int *p = a; p != a + n; p++ )
    {
        std::cout << *p << ' ';
    }

    std::cout << '\n';

    n = shift_right( a, n );

    for ( const int *p = a; p != a + n; p++ )
    {
        std::cout << *p << ' ';
    }

    std::cout << '\n';

    delete []a;

    return 0;
}

Its output is

0 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 

You can change the functions the following way. When the passed value of n is equal to 1 when just free the original pointer and set it to nullptr.

For example

size_t shift_left( int * &a, size_t n )
{
    if ( n )
    {
        if ( n == 1 )
        {
            delete []a;
            a = nullptr;
        }
        else
        {
            int *p = new int [n-1];

            std::copy( a + 1, a + n, p );

            std::swap( a, p );

            delete []p;
        }               
    }

    return n == 0 ? n : n - 1;
}

As an alternative you can use the standard container std::vector and its member function erase.

Or you can consider using std::valarray.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.