vectors

closed account (30X1hbRD)
Hi, I'm an amateur C++ user, and I have a pretty good knowledge of all the basics, but I'm a little confused about vectors... I've read the tutorial article on them, and I didn't quite understand (I'm very beginner) how exactly do you use vectors, and how do you use them efficiently?
You use them like an array or list of items they have a dynamic size unlike operator [] arrays which have a static size.

Not sure what you want us to say. I guess I can show you a small sample.
1
2
3
4
5
6
7
std::vector<int> vec1 , vec2( 2 ) , vec3( 3 , 3 );
vec1.push_back( 3 );
vec1.push_back( 3 );
vec1.push_back( 3 );
vec2.push_back( 3 );
vec2.at( 0 ) = 3;
vec2[ 1 ] = 3;


That makes it so all 3 vectors have a size of 3 with every element equaling 3.

*edit
http://www.cplusplus.com/reference/vector/vector/
Last edited on
closed account (30X1hbRD)
Thanks for the example, giblit. I think I get how to use them but what I'd like to know is when would it be a good time to use a vector?
closed account (1v5E3TCk)
I think reading a book would be better than reading articles cuz there are too many exercises about how can you use your knowledge
what I'd like to know is when would it be a good time to use a vector?

Anywhere where you need a collection of objects, when they need to be in a specific order, and when you're likely to be adding elements to the end of the sequence but not inserting them into the middle.

Generally speaking, you can start by using them in places where you're currently using arrays.

Edit: Yeah, this is a gross simplification, but it's a good starting point for a beginner.
Last edited on
When ever you will be dynamically removing and adding elements. If you are going to have the same elements every time or even same size then you should just use an array. I prefer stl containers over the [] arrays.
closed account (30X1hbRD)
So vectors sort of replace arrays?
arrays are only used if you have a static size ex
int arr[ 3 ] = { 1 , 2 , 3 };
where as a vector the size can change when ever you can add or remove elements.
arrays are only used if you have a static size ex

That's not really true - you can dynamically allocate an array using new.

In any case, it's almost always better to use a vector rather than an array, since vectors handle their own memory management, and contain lots of useful methods on them. The only times its really worth using a C-style array is when you desperately need to eke out the last bit of performance or memory efficiency - something which is rarely the case.
closed account (30X1hbRD)
alright, Thanks for the advice! I've found a section in my book on vectors as well, so hopefully I can figure it all out! thanks all!
From the vectors page on this site:
Vectors are sequence containers representing arrays that can change in size.

http://www.cplusplus.com/reference/vector/vector/

It depends on what you mean when you say "replace arrays".

Basically, a vector is an array of variable size. Like said previously but worth noting again, vectors are useful when you have have need of array-like functionality but may need to change the size of the array. To do this with normal arrays, you would need to allocate a new array of the new size and move all of the elements to it. Instead, you can use a vector where its storage capacity is handled automatically by the container.

*EDIT: Well, slow and steady doesn't always end up 'winning' the race... Sorry for the late post.
Last edited on
Topic archived. No new replies allowed.