A Vector is a kind of Array List from Java. It can receive hundreds of values indefinitely and it will still know its current size; something that regulars Arrays don’t know.

Here it is an easy to follow example, if you want to know how to add elements into the vector using input from the user, I invite you to check this article : Calculate Income using Lists in C++. Arrays vs Vectors.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (int argc, char * argv[]){

  // Declare and initialize vector to 5 doubles { 25.2, 32.5, 15.7, 17.5, 44.7 }
  vector numbers { 25.2, 32.5, 15.7, 17.5, 44.7};
  // Display the list in its current form.
  // Sort the list of numbers is Descending order.
  // Display the sorted list
  sort(numbers.begin(), numbers.end()); // in case there was a bigger number before the 10.5
  reverse(numbers.begin(), numbers.end());

  vector::iterator it;
  for(it = numbers.begin(); it != numbers.end(); ++it ){
    cout << " " << *it;
  }
   
  return 0;
  
};

Bye-Bye!

Leave a Reply

Back to Top