• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

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

Joined
Dec 2, 2009
Messages
3,352 (0.59/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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:
Code:
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!
 
Last edited:
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.
 
So you mean if it was six than copy it into the new array?
Like:
Code:
int main(){
int v[6];
for (a = 1; a <= 4; a++){
v[5] = v[6] - v[a];
}
printf(v[5]);
}
Right?
 
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
Code:
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.
 
If you're going to resize the collection, you should be using a list, not an array.
 
What about a linked list using pointers. Keep an index of the pointers for direct access.
 
It is about vectors.
2 vectors create 1 resultant vector.
It is more about math
 
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?
 
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.
 
Last edited:
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.
 
Last edited:
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.
 
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. :toast:
 
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.

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
 
I am not Alexander the Great

Lol ... give it some time. You'll get there. :D
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". :ohwell:
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 ...
 
Back
Top