Table of Contents
Overview:
- Terms to know: array, vector
- Concepts:
- To use std::array library you need to have
#include <array> - To use std::vector library you need to have
#include <vector> - std::array is still a fixed length.
- std::vectors will resize as needed.
- To use std::array library you need to have
- New code:
- Declare an std::array:
array<TYPE, SIZE> ARRNAME; - Declare an std::vector:
vector<TYPE> VECNAME; - Get the size (of either):
ARRNAME.size() - Add a new item to a vector:
VECNAME.push_back( VALUE );
- Declare an std::array:
1. std::array
C++ provides various handy functionality in it's standard library (abbreviated, unfortunately, as "std"). This includes a smarter array, as well as another structure we'll look at in a moment. The documentation for the std::array (aka array object) is available here:
https://www.cplusplus.com/reference/array/array/
Just like our standard array, we still need to know its size at compile time,
but it keeps track of its own size so we don't need to declare a named constant
like const int ARRAY_SIZE = 10;
If we put #include <array> at the top of our file, we can use an array
object instead of a traditional array to store data. They are basically
interchangable, ecxept the array object gives us access to the .size()
function, making our job slightly easier. However, the array still can't
be resized.
Creating an STL array:
// Provide the <DATATYPE, SIZE>. // Just like the traditional array, size must be constant here. array<string, 5> products;
Setting elements of the array:
products[0] = "Pencil"; products[1] = "Eraser"; products[2] = "Pencil case"; products[3] = "Pencil sharpener"; products[4] = "Ruler";
Iterating over the array:
for ( unsigned int i = 0; i < products.size(); i++ ) { cout << i << ". "; // Display index cout << products[i] << endl; // Display element at that index }
Review 1. The std::array documentation describes the array as…
- ○ associative containers that store elements formed by a combination of a key value and a mapped value.
- ○ fixed-size sequence containers.
- ○ sequence containers representing arrays that can change in size.
- ○ sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.
#+BEGINREVIEW Review 2. On the std::array documentation page, click on the link to the size function (under "Member functions", then under "Capacity").
What does the size function do?
- ○ Returns a bool value indicating whether the array container is empty, i.e. whether its size is 0.
- ○ Returns the number of elements in the array container.
- ○ Returns a reference to the element at position n in the array container.
Review 3. On the std::array documentation page, click on the link to the operator[] function (under "Member functions", then under "Element access").
What does this function do?
- ○ Returns a bool value indicating whether the array container is empty, i.e. whether its size is 0.
- ○ Returns the number of elements in the array container.
- ○ Returns a reference to the element at position n in the array container.
Review 4. On the std::array documentation page, look through the function sub-pages and find some example code. Use that to figure out the following:
How do you declare a std::array of strings of size 5 named students? #+ENDREVIEW
Review 5. Once you've declared a std::array, you can access its elements the same way as a traditional array. The main benefit is using the .size() function.
How could we write a for loop that iterates over all elements of an std::array displaying each item?
(Note that the examples use "unsigned int" because technically size cannot be negative, so unsigned restricts it to 0 and positive numbers.) #+ENDREVIEW
2. std::vector
The C++ Standard Template Library contains a special structure called a vector. A vector is a class (something we'll learn about later) and it is implemented on top of a dynamic array (which we will learn about later with pointers). Basically, it's a resizable array and it has functionality to make managing data a bit more manageable. Its documentation is here:
https://cplusplus.com/reference/vector/vector/
Generally, in Computer Science curriculum, we teach you how to build data structures (structures that store data) like vectors, lists, and other items because it's important to know how they work (thus why we're covering arrays), but for your own projects and in the real world, you would probably use a vector over an array.
Declaring a vector:
Vectors are a type of templated object, meaning it can store any data type - you just have to specify what kind of type the vector stores. The format of a vector declaration looks like this:
// Declaring vectors vector<string> students; vector<float> prices;
Adding data:
You can add data to a vector by using its push_back function, passing the
data to add as the argument:
vector<string> students; students.push_back( "Rai" );
Getting the size of the vector:
The size function will return the amount of elements currently stored in
the vector.
cout << "There are " << students.size() << " students" << endl;
Clearing the vector:
You can erase all the data in a vector with the clear function:
students.clear();
Accessing elements by index:
Accessing an element at some index looks just like it does with an array:
cout << "Student: " << students[0] << endl;
Iterating over a vector:
You can use a for loop to iterate over all the elements of a vector, similar to an array:
cout << "Students:" << endl; for ( int i = 0; i < students.size(); i++ ) { cout << i << "\t" << students[i] << endl; }
You can also use C++11 style range-based for loop if you don't need the
index. It allows you to iterate over all the elements of students, using an
alias of student for each element.
for ( auto & student : students ) { cout << student << endl; }
Review 6. The std::vector documentation describes the vector as…
- ○ associative containers that store elements formed by a combination of a key value and a mapped value.
- ○ fixed-size sequence containers.
- ○ sequence containers representing arrays that can change in size.
- ○ sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.
Review 7. According to the std::vector documentation, a vector is an implementation of what?
- ○ An associative array
- ○ A dynamic array
- ○ A fixed-length array
- ○ A linked list
Review 8. According to the std::vector documentation, how is a vector of strings declared, with the name of students?
- ○
string students[vector]; - ○
string<vector> students; - ○
vector<string> students; - ○
vector<string, 10> students;
Review 9. According to the std::vector documentation, how is a new element added to a vector, assuming the vector hasn't been pre-set to a size? Let's use students as the example vector again.
- ○
students[0] = "Alexandria"; - ○
students.pop_back( "Seongmin" ); - ○
students.push_back( "Paola" );
Review 10. Since the std::array and std::vector both have an
operator[] function, this means…
○ For both structures we can access elements like this:
cout << structureName[i];○ For both structures we can access elements like this:
cout << structureName.operator[](i);○ For both structures we can access elements like this:
cout << structureName.i;
Review 11. We can write a for loop to iterate over all the elements in a vector just like with the array. How do you write a for loop to iterate over all elements of a vector and display each element's index and value?