Question:
What are Vectors in C+?
2012-04-16 11:35:42 UTC
For example:
vector List;

I've searched the web, asked my teacher, and read my textbook, but each time I received answers that were far above my programming level. (Being extremely basic C++.)

Could someone please explain vectors to me as if I'm five?
Three answers:
Brian
2012-04-16 11:57:39 UTC
The std::vector class is a generic container for storing a list of elements, like an array, but it is automatically and dynamically sizable and offers several very useful methods such as clear(), pop_back(), and push_back() for managing a simple ordered list or stack. It's the also the base class for many more complex and more specialized STL container classes.
husoski
2012-04-16 19:32:06 UTC
A vector is just a smart array of doubles.



An array is just a list that you can access efficiently by an index, using List[i] or List.at(i) for some index value i.



An index is just a sequential number assigned to each position in the array or list. Index values start at 0, so List.at(0) is the first element of the List vector, List.at(1) is the second, and so on.



The number of elements in the List vector is List.size().



Finally, I don't think a 5-year-old should be programming in C or C++. Can I interest you in Logo? ;o)



Seriously, you should at least look at the docs and read the descriptions of all member functions of any new class you use. The first Google hit for "C++ vector" is at cplusplus.com, which is a pretty good online reference.



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



They cut a lot of the legalese out of their descriptions without serious sacrifice of either completeness or accuracy. Not all, though. You do need new vocabulary to describe new ideas, and the time you spend understanding what they're talking about, *using the terminology they're using*, will be well spent.



One thing to note is that .at() member used to index the vector. List.at(i) behaves just like List[i], including allowing modifications:



List.at(2) = 3.14; // will change the third element of List to 3.14

List.at(1) += 0.5; // will add 0.5 to the second element of List



...except that at(i) checks to see that i is a valid index. List[i] will try to access memory beyond the allocated end of the vector, or before if i is negative, with unpredictable results. If you make an error in your code, how to you want your program to fail? ...with bizarre computed output, weird exceptions, lockups and such? ...or with an exception that tells you just what was wrong and where?



Get used to using .at() and switch to [] when you need to optimize debugged code. You'll be surprised as how often you don't have to do that.
Prosper Kouassi
2012-04-16 18:48:07 UTC
Vectors are essentially coordinates that describe a point or line. Take (2,4). Picture a graph, with x being 2 and y being 4. That point is the vector. There are also 3D vectors like (2,4,4), which is the same except for the inclusion of a third dimension.



Vectors are very important in graphics programming. They can do many things, but most importantly they tell the program where exactly the screen, and therefore the player, is facing. For example, an FPS needs to be able to determine if the player is facing an enemy, so that it knows to draw that enemy onto the screen. Everything that the player is not facing can be disregarded, graphically at least. If computers had to draw everything the player could be facing, the program would run much too slow.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...