Vectors
Learn C++ in step-by-step lessons.
Vectors
An array has a fixed number of elements, all of the same type. When you declare an array such as int numb[5]; the compiler calculates the amount of memory required: 5 *sizeof(int), and exactly that amount of space is allocated.
A vector does not have a fixed size and can grow larger or smaller as needed. Items are added to the end, as the last element, using the push_back method.
Both arrays and vectors can be traversed using a for loop:
Goals:
- Be able to understand and use vectors
Objectives:
- Explain the difference between arrays and vectors
- Use a vector instead of an array
- Search a vector
- Read afile into a vector
- Sort a vector
Please study the material at each of the links below.
- Comparison of array and vector
- Search and erase from a vector
- Read from a file into a vector
- Read a file and bubble sort
Glossary for vectors lessonFull Glossary