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:

1
2
3
for(int i=0;i<count;i++) {
   cout<<numbers[i]<<endl;
} //print each number
CODE

Please study the material at each of the links below.

  1. Comparison of array and vector
  2. Search and erase from a vector
  3. Read from a file into a vector
  4. Read a file and bubble sort
Like us on Facebook:


OER: Open Educational Resource
Creative Commons License Author: Janet E. Joy; Publisher: Zebra0.com
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Creative Commons Attribution-NonCommercial 4.0 International License
Last modified: May 18 2017 14:08:28.