# How to delete a single value from a vector? C and C++



## GSquadron (Aug 30, 2011)

Hi guys!
I was trying to find how to delete a single value from a vector in c and C++ but i couldn't find a way.
Is there a command how to delete it?
I also programmed this for c:

```
int main(){
int v[n];
for (a = 1; a <= n; a++){
v[n] = v[n] - v[a];
}
printf(v[n]);
}
```
Correct me if i am wrong!


----------



## caleb (Aug 30, 2011)

I don't think you can just make an array of 8 to be an array of 7 just by subtraction.
In your code you declare memory space for v[n] before the loop then you substract a value contained in v[n] by value in v[a] and assign this to v[n].
To remove array element you would need to know the new size of array and copy it over to another array.


----------



## GSquadron (Aug 30, 2011)

So you mean if it was six than copy it into the new array?
Like:

```
int main(){
int v[6];
for (a = 1; a <= 4; a++){
v[5] = v[6] - v[a];
}
printf(v[5]);
}
```
Right?


----------



## caleb (Aug 30, 2011)

Almost  You still subtract values INSIDE array elements. 
I understood that in the end if you have array[6] and subtract an element from it you want to have array[5] right ?

If it will always be a single element not a few elements its simple

```
int main(){
int arraySize = 6;
int v[arraySize];
int elementToSubtract = 2;
int v2[arraySize-1];
int indexOffset = 0;

for (a = 0; a <= arraySize; a++){
if(a != elementToSubtract)
 v2[a-indexOffset] = v[a];
else
 indexOffset++;

}
printf(v[5]);
}
```

So having
{4,3,7,4,3,2,1}
will result {4,3,4,3,2,1}

EDIT: I think also pointers would help here but I never coded that much c/c++ so I don't have practical experience to give any leet pointer solution.


----------



## FordGT90Concept (Aug 30, 2011)

If you're going to resize the collection, you should be using a list, not an array.


----------



## twilyth (Aug 30, 2011)

What about a linked list using pointers.  Keep an index of the pointers for direct access.


----------



## GSquadron (Aug 30, 2011)

It is about vectors.
2 vectors create 1 resultant vector.
It is more about math


----------



## twilyth (Aug 30, 2011)

I know what a vector is.  I'm just saying that you can emulate the behavior using pointers.  How do you think C+ does these things behind the scenes?


----------



## Kreij (Aug 30, 2011)

Use vector::resize
Linky

If you want to remove an element (or elements) in the middle of the vector shift everything after it left (overwriting it) and then truncate it to the reduced size.


----------



## ctrain (Aug 31, 2011)

Aleksander Dishnica said:


> It is about vectors.
> 2 vectors create 1 resultant vector.
> It is more about math



are you talking about std::vector or vectors in the 2d / 3d sense?


----------



## Kreij (Aug 31, 2011)

Given that he is adding an arbitrary number of elements (n) to the vector, it does not look like Vector2d or Vector3D (graphic) types to me.


----------



## xbonez (Aug 31, 2011)

Aleksander, math vectors and C++ data structure vectors are very, very different things.

2 vectors create one resultant vector only holds true when you're talking about the mathematical/physics vectors. This concept is used heavily in game development (coding collision etc). Like Kreiji mentioned, Vector2 and vector3 data types are commonly used for that (Unity3d, XNA etc.)
C++ vectors are simply data structures that hold multiple values of the same data type and have absolutely no correlation to the abovementioned vectors.

*Please, I sincerely implore you, pick up a book about object oriented programming and read it through.*


----------



## Kreij (Aug 31, 2011)

A std:vector in C++ is just a dynamic array that can be manipulated using the methods of the vector class.
These vectors can be used for mathematical calculations, but without knowing what you are looking for in the resultant vectors it's hard to say if that is the best type to use in your situation.

Keep asking questions Aleks. I don't know if we will be able to get you where you need to be in your programming pursuits, but by bringing up the basic concepts of programming, and making us think about it, it reinforces our ability to assist people in the fundamentals of programming. That is something that gets lost a lot of times when you have no one to teach.


----------



## GSquadron (Aug 31, 2011)

xbonez said:


> Aleksander, math vectors and C++ data structure vectors are very, very different things.
> 
> 2 vectors create one resultant vector only holds true when you're talking about the mathematical/physics vectors. This concept is used heavily in game development (coding collision etc). Like Kreiji mentioned, Vector2 and vector3 data types are commonly used for that (Unity3d, XNA etc.)
> C++ vectors are simply data structures that hold multiple values of the same data type and have absolutely no correlation to the abovementioned vectors.
> ...



I am not Alexander the Great to be implored. Also, i am reading 3 books right now i am not in the needed chapters so it is really normal to ask people before going if needed in time. Like you are loosing money telling me about a question.
Thank you


----------



## Kreij (Aug 31, 2011)

Aleksander Dishnica said:


> I am not Alexander the Great



Lol ... give it some time. You'll get there. 
Don't worry about it, Aleks, I was forever perstering a friend of mine who had a Masters degree in programming when I was teaching myself to code. We all do it.

Story time !
I asked my friend (the guy above) if he had some source code for a random number generator. He gave me a text file with the code (he wrote it himself because it was more random that than anything else he could find, and he needed it for football pools at work).
When I got back to my office, I realized it was in assembly language and I needed a C version (I had been working mostly in Pascal at the time and was not very good at C).
I went back to him and asked if he had a C version and he said, "No problem". Opened the text file, stared at it for about 5 seconds and then typed the C source over the top of the assembly code. Compiled it (it had one error which was a typo, not a bug), and handed me back the source file. Took him about 1 minute.
He had to write a compiler for a graduate project and refered to it as a "quaint little exercise". 
I think I drove him nuts for a couple of weeks when I was trying to get a firm understanding of pointers and all their little nuances (like pointers to pointers that pointed to functions. lol). Thankfully he was vary patient with me.

After awhile it all finally sunk in and I didn't have to perster him (as much) any more.
Them were the good ol' days ...


----------

