Arrays, Vectors, Header Files, Palindromes, and Structures

Sometimes there is a need to do the same processing on multiple variables. The most common way to do this is through arrays and vectors. Both arrays and vectors can be accessed through indexes, like students[5]. Even though accessing can be similar, how they are set up are entirely different. Arrays are a fixed size. If more space is needed in an array, a new larger array must be create. The elements of the current array are then copied into the new array, and then any new elements are added into the new array. This is the way programming was done for a long time. Then programmers decided that there needs to be dynamic allocation to a group of elements, thus the introduction of vectors. Both can create multiple copies of any standard variable types like int, double, string, and char. Later in this article I will discuss using vectors on structures that you create on your own. Again, arrays need to specify an initial size, while vectors can have no elements. A sample of forming each is as follows:

string names[4];                          // An array for four strings
int nums[] = {3, 4, 5, 6};                // An array of size four with initial values.
vector<int> numberList;                   // A vector of integers with no initial size

The best way to give an example of how vectors are used is to create a program. The first program allows the user to type in a sentence and the pushes the letters in two vectors, one at a time ignoring special characters. A Palindrome has the same sequence of characters both backwards and forwards. Therefore, I take the second vector and reverse it. I then compare the two vectors to see if each character is equal. As a bonus, the following example also shows how to have an include file which contains a prototype for a function that resides in a different file. Enjoy the video.

Vectors and Structures

The best way to see the difference between Arrays, Vectors, and Structures is to make a small table. Arrays are the easiest to create and use for groups of variables of the same type. Arrays have a fixed size and can be indexed. Vector<type>s can also created arrays of the same data type, but with vectors elements can be added and remove. Structures are a way to group variables that are related to a specific item. Their size is determined when they are created and their variables can be of multiple types, The table is:

Group TypeSize FixedData TypesAccess
ArrayYesSingleIndex
Vector<type>NoSingleIndex
StructureYesMultipleMember Access
Various Variable Groupings

That previous video shows how to create Character (char) vectors and use a special reverse function from #include <algorithm> section of the standard library. The following video and code describes how to cerate a structure (collection of variables in an object that can be accessed through member attributes of an instance of that structure. The structure is a general definition of what variables are needed in that object. An instance is a specific implementation of the structure with specific values given to each member attribute. Structures are usually defined within headers because they are really like prototypes and only contain attributes with no functions or methods. The following is a description of a Student structure containing lastName, firstName, and grade of each student. Each student is then added to vector<Student>, which is just a list of instances of Student with the three attributes.

/* struct Student 
 * structs must be declared in the header file or too many errors occur.
 * includes important information about each student
 * Fields:
 *		string lastName - student last name
 *		string firstName - student first name
 *		int grade - current grade of student (9-12)
 */
struct Student {
	string lastName, firstName;
	int grade;
};

One Response to Arrays, Vectors, Header Files, Palindromes, and Structures

  1. WW Cook says:

    See if this goes through

I would enjoy hearing from you.

This site uses Akismet to reduce spam. Learn how your comment data is processed.