List is a collection similar to array but without specific size.
There are couple of things to remember about it:
In order to use it the following line should be added:
Also if there is need of access to element at specific index, the following needs to be included:
type
specifies the values type - int, double, string, char etc...
Let's have a list of ages and print only those who are greater than 20.
Output:
This can be done via the push_back
function. Like in the following code:
Size is the count of elements in collection.
In order to get it the size
function is used.
Example:
Output:
Let's add elements to list and access the element at index 1.
Output:
There are couple of things to notice:
line 6
).line 21
.line 22
.line 24
.Iterator which moves outside ot list bounds results in exception.
Let's look at the following:
The collection has 3 elements but the iterator moves 5 elements ahead. This causes an exception.