If you ever used a vector in c++ could you show...

Started by
19 comments, last by DingusKhan 9 years, 6 months ago

Show me what you were doing with this and possibly explain why?

Advertisement
I have dozens of possible examples; what are you hoping to learn?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Show me what you were doing with this and possibly explain why?


Like, everything. Because you want type-safe dynamically-resizable contiguously-allocated containers of data.

Sean Middleditch – Game Systems Engineer – Join my team!

If you're talking about std::vector, then the equivalent is asking what is the use for a closet. You can store lots of things in a closet. Be it clothes, shoes, money, documents. If documents, they can be in alphabetical order, in chronological order, in random order. There's almost no limit.

Vector is the most generic container.

It's the equivalent of a pointer array in C, except you get a few bonuses (don't have to worry about deallocation most of the time, the array can be resized automatically)

It's the equivalent of a pointer array in C

Calling it that could make people incorrectly assume it's literally an array of pointers, when you refer to the equivalent of

ElementType* v = (ElementType*)calloc(n, sizeof(ElementType)); (minus all the tedious fiddling with resizing, freeing, etc.)

The only thing not to do with a vector is to keep pointers to the elements, unless you are absolutely sure that your vector is done growing.

f@dzhttp://festini.device-zero.de

I have dozens of possible examples; what are you hoping to learn?

Dozens?? Try hundreds if not thousands. Nearly everything in our codebase is driven by vectors. If I have one of something, it lives in a variable/object. If I have two or more of something, it goes into a vector. We literally never use operator new[] or delete[].

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Only dozens, because the multi-million line code base I hack on at work uses a different container implementation :-P

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

i had a rigorous background in simpler programming venues that gravitated me towards retaining simple methods.

the reason to use vectors is because they are simpler, eg. if you have two 3d points stored as vectors, you can add them by writing a + b instead of separately adding each dimension. makes code easier to read and write :)

neither a follower nor a leader behttp://www.xoxos.net

i had a rigorous background in simpler programming venues that gravitated me towards retaining simple methods.

the reason to use vectors is because they are simpler, eg. if you have two 3d points stored as vectors, you can add them by writing a + b instead of separately adding each dimension. makes code easier to read and write smile.png

That's a different kind of vector.

LAURENT* could be talking about that kind of vector (math related), or he could be talking about the container type std::vector.

That is:

Vector myVector = {0.0f, 15.3f, -27.0f};

vs

std::vector<int> myVector;

Both types of vectors are used constantly.

Hello to all my stalkers.

If you were writing code for a toaster with 1000 bread slots, you could use "std::vector<bool> slotsWithBread;" to keep track of which slots have bread in them. :)

This topic is closed to new replies.

Advertisement