### increment and decrement

Hey what's the trick for incrementing an array by one and decrementing it by one. I know you can't actually do this to array's but I remember there is a trick maybe a temp. Not sure how to work this out.Thanks
Hi,
What do you mean by "incrementing an array"? Do you mean incrementing each cell´s content, incrementing its size?
Oh sorry, that was vague. I mean incremeting as in insertion of an element into an array. Incrementing the SIZE I meant. As I said I know you can't actually do this will array's but there is a way to acheive this with a temp array or something I can't remember. I would like to know both incrementing and decrementing. By decrementing I mean removal of an element.
Hopefully that explains it ...:)
Yes, it does. I can think of two ways,
1) Use vectors. It´s similar to an array, but it has its own methods for getting bigger if needed (check out push_back).

2) In a more "home made" way, you could create a struct with two fields
a) an array of the maximum size you think you´re gonna need
b) an int called ArraySize or something like that.

and then you can change the array last cell by hand.

I guess second case, without an example, isn´t easy to understand. May be you should go directly with vectors. I mean, there´s something that already does what you need...

http://www.cplusplus.com/reference/vector/vector/
Last edited on
Yeah I would use vectors if I could. I have intro c++ and we didn't cover vectors. I am aware of them. You know it was weird we did not cover them they were in the chapter. I feel that vectors are a better method. Although I'm not aware of all the advantages and disadvantages of array's vs vectors.
Anyways so does anyone know how to handle this with the pain in the a*s method of using vectors?
It might help if you explain exactly what it is you're trying to do with the container. Knowing that will help us clarify which way is better or easier for the situation (assuming you don't have to use arrays for this project).

And I'm not sure if you meant that vectors are a pain in the a\$\$ by what you said in your last sentence, but I can assure you they're quite easy to work with once you understand the concept behind them (which I can explain a bit later once we've delved into what you're trying to do with your program).
Arrays are fixed-size containers, by definition. There are no tricks to them, they simply cannot grow or shrink, ever. All you can do about it is create a new array, of different size, copy over the elements from the old array, and then destroy the old array. Or use a more appropriate container as suggested.
Yes that is the trick how can I do that ?
Nevermind I just figured it out. Thanks though.
Topic archived. No new replies allowed.