/*
    Vectors can contain anything you want. Ideal for random access reading.
    Note. valarray are vectors optimized for numerical processing.

    http://www.cplusplus.com/reference/vector/vector/
*/

#include <vector>             // Vector
#include <iostream>           // cin/cout
using namespace std;

int main()
{
    // Some examples of vectors
    vector<int> numbers;                                    // Empty vector.    
    vector<int> someprimes = { 2 3 5 7 9 11 13 17 19 21};   // Prefilled with defined values
    vector<int> someones( 100, 1);                          // Prefilled with 100 elements all having value 1
    vector<int> copyofprimes( someprimes);                  // Prefilled with a copy of some primes.

    // And they can grow as long as there is memory:
    cout << "Please enter some numbers:";
    int n;
    while (cin >> n)
    {
        numbers.push_back(n);
    }

    if (numbers.empty())
    {
        cout << "No numbers entered!" << endl;
        return 1;
    }

    cout << "You entered " << numbers.size() << " numbers" << endl;

    // Random access with range checking   using .at(index) method 
 
    cout << "Random Access:";
    for (int i=0;i<numbers.size();i++)
    {
        cout << numbers.at(i) << " ";                   // We can also change elements say:    numbers.at(i)=0;
    }
    cout << endl;


    // Random access with same semantics as the array, no range checking.
    
    cout << "Random Access:";
    for (int i=0;i<numbers.size();i++)
    {
        cout << numbers[i] << " ";
    }
    cout << endl;


    // Functional style like Python
    cout << "Functional Access:";
    for (auto& it : numbers)
    {
        cout << it << " ";
    }
    cout << endl;


    // Iterator access.   (old fashioned)
 
    cout << "Iterator Access:";
    for (auto it =numbers.begin();it!=numbers.end();it++)
    {
        cout << *it << " ";   // *it gets "value at where the iterator is located.
    }
    cout << endl;


    // Lets calculate the average of the values:
    double average=0.0;
    for (auto& it : someprimes) average+=it;
    average/=numbers.size();    

    cout << "The average value is " << average << endl;

    return 0;
}

