Jump to content
  • Advertisement
Sign in to follow this  
pro-gamer

Whats a vector

This topic is 4357 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

I am currently reading Beginning C++ Game Programming and on Chapter 4 it has a section on vectors that just confuses me. He says a vector points to pice of data like a post it note and you can move it around or something like that but it makes no sense. If anyone has a tutorial on vectors or have read the book or just know what this guy is talking about please help.

Share this post


Link to post
Share on other sites
Advertisement
I'm reading the same book as you, so I can offer some help.

A vector is a part of STL. Just think of STL as extra files built into the compiler to help you solve problems for now. If you want to use STL's vector, you must use '#include <vector>'. Think of a vector as a new type of array. It does the same thing, stores multiple pieces of data into a single variable (the vector). Lets declare a vector using the appropriate syntax shall we(assuming we used 'using namespace std'):

vector<int> myvector;

Lets look at what we just did. 'vector' tells the compiler your declaring a vector. '<int>' tells the compiler your going to store integer values in this vector (just like declaring 'int myarray[5]' which stores integer values). 'myvector' is simply the name I gave to the vector. Ok, so now I have a vector, what's next? Say I want to add the numbers 1,2, and 3 to the vector. I would use '.push_back' to add a number to it. So to add 1,2, and 3 I would simply write:

myvector.push_back(1);
myvector.push_back(2);
myvector.push_back(3);

That's a basic lesson on vectors, but there is so much more you can do with them. I suggest rereading the chapter carefully until you learn how to display and further manipulate data in a vector.

-Kevin

Share this post


Link to post
Share on other sites
To put it another way, a vector is a dynamic array (basically). ie just like the static arrays

int array[2]

the same variable holds a list of values instead of just one.

However, unlike a static array it can grow or shrink as needed by the program. So when you declare the vector it doesn't hold any information, nor does it have any slots in memory reserved for it. (Though I tend to ignore how data structures use memory until I understand how they work, some people work better in the other direction, so which ever way makes sense to you). Once you have assigned a value to the vector (with the push_back function as kevtimc said) the vector then points to a list of values (just one at the moment). Once you add another value to the vector you then have to values in your array, both of which are pointed to by the vector.

Share this post


Link to post
Share on other sites
Just to clarify, the author is actually referring to iterators when he talks about putting post-it notes on pieces of data. An iterator is used to manipulate the elements held within a vector- read the chapter carefully for a full & concise explanation.

Share this post


Link to post
Share on other sites
an iterator is part of a vector right? and whats up with the

vector<string>::const_iterator iter;

whats that supposed to do and i thought that a constant was written const not const_.
and what is this * thing.

Share this post


Link to post
Share on other sites
an 'iterator' is a broad name for things that behave like pointers. Different classes of iterators implement varying amounts of that functionality, but most of them can be used for - well - iteration: i.e., you can compare the value to two endpoints, increment it and dereference it, thus you can set up a for loop where you use the value to access each element of a sequence in turn.

The vector type defines a *corresponding* type of iterator, that is used for iterating over vectors. A specific instance of a vector can create instances of iterators that refer to points within that vector (or, in the case of .end(), one past the end of the vector).

"This * thing" is an invocation of the unary * (dereference) operator. It is exactly like dereferencing a pointer.

'const_iterator' exists because 'const iterator' would mean an iterator that can't be changed - i.e. can't actually iterate, but can only be dereferenced. 'const_iterator' is a separate type: it's an iterator that behaves like a pointer to const objects (as opposed to a const pointer to non-const objects). Because iterators *aren't* pointers (but simply behave like them), there's no extra place in the syntax to put the word 'const' to make the distinction, so a separate type is needed.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!