W6: Containers and Iterators Flashcards
An array has _________ storage of _______ size
contiguous
fixed
A vector has _________ storage of _______ size
contiguous
variable
A deque has _________ storage of _______ size, and a ___________ queue
non-contiguous
variable
double-ended
A forward_list has _________ storage of _______ size, and a __________ list
non-contiguous
variable
singly-linked
A list has _________ storage of _______ size
non-contiguous
variable
doubly-linked
vector(int n);
vector(int n, const T& t) ;
- creates a container with n elements
- creates a container with n elements, each initialized to value t
vector(const vector& v);
vector& operator=(const vector& v);
vector(vector&& v) noexcept ;
vector& operator=(vector&& v) noexcept;
~vector();
- copies the contents of container v into the current object
- moves the contents of container v into the current object
- destroys the container
size_t size() const;
size_t capacity() const;
bool empty() const;
- returns the number of elements in the current object
- returns the current capacity of the current object
- returns true if the current object has no elements
- returns a reference to element i
- returns an unmodifiable reference to element i
- returns a reference to element i and checks bounds - throwing an exception
- returns an unmodifiable reference to element i and checks bounds - throwing an exception
T* data() noexcept;
const T* data() const noexcept;
- returns a pointer to the underlying array
- returns a pointer to the underlying unmodifiable array
T& front();
const T& front() const;
T& back();
const T& back() const;
- returns a reference to the first element
- returns an unmodifiable reference to the first element
- returns a reference to the last element
- returns an unmodifiable reference to the last element
void push_back(const T& t);
void pop_back();
void clear();
- adds element t after the last element in the container
- removes the last element from the container
- removes all elements from the container
What will be the output?
#include #include
int main() { std::vector prices; // initially empty
if(prices.empty()) // is prices empty? std::cout << "prices is empty" << std::endl; prices.push_back(10.43); // add 10.43 prices.push_back(20.54); // add 20.54 prices.push_back(32.43); // add 32.43 for(int i = 0; i < prices.size(); i++) std::cout << prices[i] << " "; std::cout << std::endl; prices.front() = 54.11; // change 1st element prices.pop_back(); // remove last element for(int i = 0; i < prices.size(); i++) std::cout << prices[i] << " "; std::cout << std::endl; }
prices is empty
- 43 20.54 32.43
- 11 20.54
How to get the pointer to point to the first element of vector object v
we can use either &v[0] or &v.front().
A deque class can change in size from ______ end and have elements ordered in _______
either
sequence