pointers, dynamic memory allocation, objects with dynamically allocated members, and vectors Flashcards
subjects for quiz 2
vector
Stores elements like an array but can dynamically change in size. Adding and removing of elements are usually done at the end. Elements can be accessed by index.
stack
Stores elements in a specific order, called LIFO (Last In, First Out), where elements can only be added and removed from the top. Not accessible by index.
queue
Stores elements in a specific order, called FIFO (First In, First Out), where elements are added at the end and removed from the front. Not accessible by index.
what do you need to use vectors?
include<vector></vector>
how do you declare a vector?
vector<type> vectorName;</type>
how do you declare a vector with 5 positions?
vector<type> vectorName (5);</type>
how do you declare vector Cars of type string with entries Volvo, BMW, and Dodge?
vector<string> Cars = {Volvo, BMW, Dodge};</string>
how do you write a for each loop?
for ( type of variable in vector variable name : vector name)
how do you print out vector entries using a for each loop?
for( type of variable in vector variable name: vector name) { cout «_space;variable name; }
iterators
used to access and iterate through elements of data structures (vectors, sets, etc.), by “pointing” to them
algorithms
include functions, like sort() and find(), that perform operations on data structures through iterators
can the type of vector (string, int, double, …) be changed after a vector has been declared?
no
how can you access vector elements using brackets?
vectorName[positionOfElement];
what does .front() do?
accesses the first element in the vector
what does .back() do?
accesses the last element in the vector
what does .push_back() do?
adds an element at the end of the vector
what does pop.back() do?
removes the last element from the vector
what function returns how many elements a vector has?
.size()
how do you check if a vector is empty?
using the .empty() function included in the vector library
what does .empty() return?
1 (true) if the vector is empty
0 (false) if the vector is not empty
begin()
returns an iterator that points to the first element of the data structure
end()
returns an iterator that points to one position after the last element
when should you use an iterator over a for loop?
when you need to add, modify, or remove elements during iteration, iterate in reverse, or skip elements
what do you need to be able to use sort() and find()?
include<algorithm></algorithm>