1
SomeClass* stuff;
int N = 10;
stuff = new SomeClass[N];

Someclass* objectPtrDelete = null;

int i = 0;
for(Someclass* pointer = begin(); pointer != end(); pointer++)
{
    if(pointer->getSomeAttr() == randomPointer.getSomeAttr()){
        objectPtrDelete = pointer;
        break;
    }
    i++;
}
    
// Shrinking the C-array with this for loop, shifting left
for (int j = i; j < N-1; j++)
    stuff[j] = stuff[j + 1];

Can the last loop be converted into a Pointer loop, if yes, how is this properly done? Note, the names are fictional i.e. something imaginary. I have implemented something similar, but, I would like to understand how can I convert the last loop to a pointer for loop that does the same operation.

Make this:

for (int j = i; j < N-1; j++)
    stuff[j] = stuff[j + 1];

Into a Pointer loop.

  • 1
    What is a "pointer loop"? – UnholySheep Oct 02 '20 at 20:56
  • I do not know, https://stackoverflow.com/questions/33829566/for-loop-with-pointer-in-c. But, is it correct and is that the way you do it? –  Oct 02 '20 at 20:58
  • 1
    If you use pointers instead of indices not much changes. You simply need to figure out what you use as the condition (presumably something like `ptr != a + N` and make sure that you dereference correctly in the body. – UnholySheep Oct 02 '20 at 21:00
  • 2
    @Albin M This int[] a = new int[N] is not a valid C++ record. – Vlad from Moscow Oct 02 '20 at 21:01
  • I have a Pointer array, `SomeClass* stuff;` This is initiated: `stuff = new SomeClass[10];` –  Oct 02 '20 at 21:04
  • @UnholySheep, so you mean: `int* ptr = begin(); ptr != end(); ptr++` ? –  Oct 02 '20 at 21:05
  • You may read this: https://stackoverflow.com/questions/394767/pointer-arithmetic Also for your last comment it should be `std::begin(a)` and `std::end(a) - 1`. – πάντα ῥεῖ Oct 02 '20 at 21:06
  • You should seriously coinsider simply using a `std::vector`. If you initialize with `new` the size information gets lost. – πάντα ῥεῖ Oct 02 '20 at 21:10
  • @πάνταῥεῖ, I am aware about STL and vectors. But, I want to do this with Array[] pure array approach. –  Oct 02 '20 at 21:16
  • _"pure array approach"_ That's `std::array` (with a `const N`) in c++. – πάντα ῥεῖ Oct 02 '20 at 21:17
  • I regular C-array. I heard this can be achieved. Therefore, I wanted to find out by doing. Anyway, I posted this to other forums as well. Just want to know, how I can convert the last for-loop into a pointer one. Which does the exact same things except with pointers. –  Oct 02 '20 at 21:19

2 Answers2

1

For starters this construction (without an ending semicolon)

int[] a = new int[N]

is invalid in C++.

It should look like

int *a = new int[N];

If I have understood your question correctly you mean something like the following

for ( auto prev = a, next = a + 1; next != a + N; ++next )
{
    *prev++ = *next;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

(edited to take into account your changes)

if I well understand what you want, having

SomeClass* stuff;

you can replace

for (int j = i; j < N-1; j++)
   stuff[j] = stuff[j + 1];

by

for (SomeClass * p =  stuff + i; p < stuff + N-1; p++)
    *p = *(p + 1);

or to easily manages changes concerning the type :

for (auto p =  stuff + i; p < stuff + N-1; p++)
    *p = *(p + 1);

etc

bruno
  • 32,421
  • 7
  • 25
  • 37