View Single Post
Old Apr 1st, 2008, 4:14 PM   #7
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: I kind of need help on my program

Quote:
Originally Posted by verizondude
Well seeing a vector as like being a massive array, wouldn't the user have to count the characters he/she would want to plug in?
For any data type that is a collection of elements, space needs to be allocated for the elements.

As an example, imagine a C-style string (array of char, with the string's end marked by a zero). You need to allocate space for the string plus an additional char element for the zero terminator. However, there are data structures that hide this sort of thing from you. The C++ string class is a good example. It allocates a block behind the scenes- say 20 characters- and tracks the current length of the string. It then allocates a larger block if it needs to, but this is all hidden from the user of the class.

A vector is similar. Say you have a struct defined like so:
C++ Syntax (Toggle Plain Text)
  1. struct g_Personalinfo
  2. {
  3. char firstname[10];
  4. char lastname[15];
  5. int age;
  6. char school[30];
  7. char subject[30];
  8. int grade;
  9. };
This does not actually create an instance. Rather, it only tells compiler what the type is composed of, so you can use it. I'm sure you already know this part. You need to create one or more instances, but you can create them either one at a time, or a bunch at once in an array:
C++ Syntax (Toggle Plain Text)
  1. g_Personalinfo singleItem;
  2. g_Personalinfo arrayOfItems[100]; // room for 100 elements
If you use the array, you have to provide a subscript, just like with any other array access:
C++ Syntax (Toggle Plain Text)
  1. arrayOfItems[7].age = 19;
Now, this all works dandy until you exceed 100 students, or whatever type your array holds. It also means if you use less, you're wasting space. A vector is a better solution here. The syntax looks a bit funny, because vector is actually a template; think of a template as a layout, with an actual type filled in at runtime. It will look something like this:
C++ Syntax (Toggle Plain Text)
  1. // remember to #include <vector>
  2. vector<g_Personalinfo> vectorOfItems(100); // 100 elements
You can then use it with array-style subscript notation, and you can 'push' elements on the end of the vector, or 'insert' them in the middle. If the vector is full when you add an element, it will resize itself. Thus, you don't need to worry so much about the size. There are methods for controlling how much space it reserves, and how it resizes itself, if you're paranoid it'll reserve too much (wasting space) or too little (meaning resizing happens more often, and resizing tends to be a slow operation).

The upshot of all this is that the user will not need to know in advance how many items they're inputting. The vector is sized to a default or specified size, and if this turns out to be too small, it will grow as needed.

Anyways, you can read all about vectors right here (this reference in general is quite helpful and to-the-point, I find).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote