0

how to delete and replace a variable in an array for c++?

17th Mar 2020, 5:23 PM
Rinji silas
3 Réponses
+ 1
I'm not getting what you mean by "replace a variable in an array" can you provide an example? And please tag C++ instead of '#rsv', unless you can explain the relevance of '#rsv' to your question topic.
17th Mar 2020, 5:41 PM
Ipang
+ 1
If what you are trying to say is:How to delete and replace an element in an array for c++. The answer is: You can't delete an element of an array, if that is a regual one: int Array[10]; Though you can replace elements by simply replacing their values. Array[2]=14; If you want to replace a whole variable you can make an array of pointers instead: int a,b,c,d,e,f; int* arr[5]={&a,&b,&c,&d,&e}; Now all you have to do to replace one is: arr[2]=&f; //replacing &c with &f As for deleting using dynamic arrays (std::vector) is the best option
17th Mar 2020, 6:05 PM
xaralampis_
xaralampis_ - avatar
+ 1
Also keep in mind that the only way to delete data stored on stack is only if its lifetime ends for example: int a,b,c; int* Array[4]={&a,&b,&c,nullptr}; { int d; Array[3]=&d; } //Now Array[3] is a dangling pointer, since d was deleted once it exited the scope
17th Mar 2020, 7:15 PM
xaralampis_
xaralampis_ - avatar