Vectors and stuff.

CC licensed photo by PapaPiper on Flicker
CC licensed photo by PapaPiper on Flicker

Here is the usual definition of vectors, provided by our friends of Cplusplus.com:

“Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.”

It continues:

“Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.”

And believe me, it is much more longer…

So here comes my really easy definition: A vector is a cool magic box that expands and shrinks whenever you want. BUM! that’s it. Why cool? Because yes… Why magic? Because it EXPANDS! and shrinks when you want.

Let me explain: When you create a vector you can say, “hey mister vector I want you to have 5 spots for my ints!” Or, you can say “hey mister vector, just exist there until I decide how many things I will store inside you.”

I´ll upload a video here explaining basic functions of vectors.

I also made a cpp file explainind all the basic functions of vectors that are included in the library. That’s rigth! You have a hole new library to explore with vectors!!

Here it is:

https://github.com/OctavioIRG/TC1017/blob/master/WSQ-s/victor.cpp

If you don’t want to open GitHub then here is a list with the basic functions of the vector library. Have FUN with Mr. Vector.

/*
Format:
vector<DataType> nameOfVector ===> creates a new vector
myVector.push_back(value) ===> adds an element to the END of the vector (also resizes it)
myVector.at(index) ===> returns the value AT the especified index number
myVector.size() ===> returns an unsigned interger equal to the number of elements
myVector.begin() ===> reads vector from first element (index 0)
myVector.insert(myVector.begin() + interger, new value) ===> adds an element BEFORE specified index number
myVector.erase(myVector.begin() + interger) ===> erase an element AT the specified index
myVector.clear() ===> removes all values from the vector
myVector.empty() ===> returns a boolean value if wether vector is empty
*/

CC BY 4.0 Mastery23 by Octavio Rojas is licensed under a Creative Commons Attribution 4.0 International License.