Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

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

This topic is 1319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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[].

Edited by Promit

Share this post


Link to post
Share on other sites
Only dozens, because the multi-million line code base I hack on at work uses a different container implementation :-P

Share this post


Link to post
Share on other sites

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 :)
 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!