I was wondering if there is a way to delete elements of a vector using pointers. In my tutorial class for an Information Structures course, in answer to the highlited part of the following question:
1.Develop a C++ function with a vector called Numbers to store integers. Resize Numbers to hold 10 integers. Insert the integers 1 to 10 into Numbers. Print the integers in forward and reverse order. Delete the first, last and fifth elements and print Numbers again.
gave us this answer:
- Code: Select all
.
.
int *n1, *n2, *n3;
n1 = &Numbers[0];
n2 = &Numbers[9];
n3 = &Numbers[4];
delete n1;
delete n2;
delete n3;
.
.
This looked a little fishy to me so i compiled it in DevC++ and when i printed the vecotor the values of those elements were garbage values (definitely not in the range 1-10). So i did some research online and found out about iterators and this seems like a good way to go about it, but i was wondering if someway somehow this pointers thing could still work?
