Skip to content

Latest commit

 

History

History
 
 

Section07-Arrays_and_Vectors

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Arrays and Vectors

What is an Array?

  • Compound data type or data structure
    • Collection of elements
  • All elements are of the same type
  • Each element can be accessed directly

Characterisitcs

  • Fixed size
  • Elements are all the same type
  • Stored contiguously in memeory
  • Individual elements can be accessed by their position or index
    • First element is at index 0
    • Last element is at index size-1
  • No checking to see if you are out of bounds
  • Always initialize arrays
  • Very efficient
  • Iteration (looping) is often used to process

Declaring and Initalizing Arrays

Element_Type array_name [constant number of elements] {init list};

int test_scores [5] {100, 95, 99, 87, 88};    // 5 integers

int high_score_per_level [10] {3, 5};   // init to 3, 5 and remaining to 0

const int days_in_year {365};
double hight_temperatures [days_in_year] {0};   // init all to zero

int another_array [] {1, 2, 3, 4, 5};   // size automatically calculated

Accessing and Modifying Array Elements

Accessing array elements

array_name [element_index]

int test_scores [5] {100, 95, 99, 87, 88};

cout << "First score at index 0:" << test_scores[0] << endl;
cout << "Second score at index 1:" << test_scores[1] << endl;
cout << "Third score at index 2:" << test_scores[2] << endl;
cout << "Fourth score at index 3:" << test_scores[3] << endl;
cout << "Fifth score at index 4:" << test_scores[4] << endl;

// output
First score at index 0: 100
Second score at index 1: 95
Third score at index 2: 99
Fourth score at index 3: 87
Fifth score at index 4: 88

Changing the contents of array elements

array_name [element_index]

int test_scores [5] {100, 95, 99, 87, 88};

cin >> test_scores [0];
cin >> test_scores [1];
cin >> test_scores [2];
cin >> test_scores [3];
cin >> test_scores [4];

// assignment statment
test_scores[0] = 90;

How does Arrays work?

  • The name of the array represent the location of the first element in the array (index 0)
  • The [index] represents the offset from the beginning of the array
  • C++ simply performs a calculation to find the correct element
  • Remember - no bounds checking!

Multidimensional Arrays

Declaring multi-dimensional arrays

Element_Type array_name [dim1_size][dim2_size]

int movie_rating [3][4];    // 3 rows, 4 columns
0 1 2 3
0 0 4 3
1 2 3 3
2 1 4 4
const int rows {3};
const int cols {4};
int movie_rating [rows][cols];

Accessing array elements in multi-dimensional arrays

cin >> movie_rating [1][2];
cout << movie_rating [1][2];

// output
5

Initializing multi-dimensionaly arrays

int movie_rating [3][4] 
{
    {0, 4, 3, 5},
    {2, 3, 3, 5},
    {1, 4, 4, 5}
};

What is a vector

  • Container in the C++ Standard Template Library
  • An array that can grow and shrink in size at execution time
  • Provides dsimilar semantics and syntax as arrays
  • Very efficient
  • Can provide bounds checking
  • Can use lots of cool functions like sort, reverse, find, and more

Characteristics

  • Dynamic size
  • Elements are all the same type
  • Stored contiguously in memory
  • Individual elements can be accessed by their position or index
  • First element is at index 0
  • Last element is at index size-1
  • [] no checking to see if you are out of bounds
  • Provided many useful function taht do bounds check
  • Elements initialized to zero
  • Very efficient
  • Iteration (looping) is often used to process

Declaring and Initializing Vectors

  • Suppose we want to store test scores for my school and I have no way of knowing how many students will register next year...
  • Options:
    • Pick a size that you are not likely to exceed and use static arrays
    • Use a dynamic array such as vector

Declaring

#include <vector>
using namspace std;

vector <char> vowels (5);
vector <int> test_scores (10);  // automatically set to zero

vector <char> vowels {'a', 'e', 'i', 'o', 'u'}; // single quotes
vector <int> test_scores {100, 98, 89, 85, 93};
vector <double> high_temperature (365, 80.0);   // 365 initializes size of vector, 80.0 initializes all the 365 doubles to.

Accessing and Modigying Vector Elements

Array syntax

  • Same as arrays
  • No bounds checking will be done

vector_name [element_index]

vector <int> test_scores {100, 95, 99, 87, 88};

cout << "First score at index 0: " << test_scores[0] << endl;
cout << "Second score at index 1: " << test_scores[1] << endl;
cout << "Third score at index 2: " << test_scores[2] << endl;
cout << "Fourth score at index 3: " << test_scores[3] << endl;
cout << "Fifth score at index 4: " << test_scores[4] << endl;

Vector syntax

  • Created a vector, which is an object
  • Followed by the dot (.) operator
  • Followed by the method name, name of the operation we want performed

vector_name.at(element_index)

vector <int> test_scores {100, 95, 99, 87, 88};

cout << "First score at index 0: " << test_scores.at(0) << endl;
cout << "Second score at index 1: " << test_scores.at(1) << endl;
cout << "Third score at index 2: " << test_scores.at(2) << endl;
cout << "Fourth score at index 3: " << test_scores.at(3) << endl;
cout << "Fifth score at index 4: " << test_scores.at(4) << endl;

Changing the contents of vector elements - vector syntax

  • Similar to arrays

    vector_name.at(element_inded)

    vector <int> test_scores {100, 95, 99, 87, 88};
    
    cin >> test_scores.at(0);
    cin >> test_scores.at(1);
    cin >> test_scores.at(2);
    cin >> test_scores.at(3);
    cin >> test_scores.at(4);
    
    // assignment statment
    test_scores.at(0) = 90;
  • but can change size dynamically

    vector_name.push_back(element)

    vector <int> test_scores {100, 95, 99}; // size is 3
    
    test_scores.push_back(80);  // size of 4: 100, 95, 99, 80
    test_scores.push_back(90);  // size of 5: 100, 95, 99, 80, 90
    // vector will automatically allocate the required space!!!
  • What if you are out of bounds?

    • Arrays never do bounds checking
    • Many vector methods provide bound checking
    • An exception and error message is generated
    vector <int> test_scores {100, 95};
    
    cin >> test_scores.at(5);

    output error...

    terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 5) >= this->size() (which is 2)
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the applications support team for more infromation