W6: Containers and Iterators Flashcards

1
Q

An array has _________ storage of _______ size

A

contiguous

fixed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A vector has _________ storage of _______ size

A

contiguous

variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A deque has _________ storage of _______ size, and a ___________ queue

A

non-contiguous
variable
double-ended

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A forward_list has _________ storage of _______ size, and a __________ list

A

non-contiguous
variable
singly-linked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A list has _________ storage of _______ size

A

non-contiguous
variable
doubly-linked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

vector(int n);

vector(int n, const T& t) ;

A
  • creates a container with n elements

- creates a container with n elements, each initialized to value t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

vector(const vector& v);
vector& operator=(const vector& v);

vector(vector&& v) noexcept ;
vector& operator=(vector&& v) noexcept;

~vector();

A
  • copies the contents of container v into the current object
  • moves the contents of container v into the current object
  • destroys the container
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

size_t size() const;

size_t capacity() const;

bool empty() const;

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

T& operator;

const T& operator const;

T& at(size_t i);

const T& at(size_t i) const;

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

T* data() noexcept;

const T* data() const noexcept;

A
  • returns a pointer to the underlying array

- returns a pointer to the underlying unmodifiable array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

T& front();

const T& front() const;

T& back();

const T& back() const;

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

void push_back(const T& t);

void pop_back();

void clear();

A
  • adds element t after the last element in the container
  • removes the last element from the container
  • removes all elements from the container
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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;  }
A

prices is empty

  1. 43 20.54 32.43
  2. 11 20.54
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to get the pointer to point to the first element of vector object v

A

we can use either &v[0] or &v.front().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A deque class can change in size from ______ end and have elements ordered in _______

A

either

sequence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What member functions does the deque class include that vector doesn’t?

A

void pop_front() - removes the first element from the container

void push_front(const T& t) - adds element t before the first element in the container

17
Q

What will be the output?

 #include   // for deque template
 #include 
 int main() {
     std::deque prices(3, 10.50), costs; 
 prices.back() = 32.43; // reset last
 prices.pop_front();    // remove first
 for(int i = 0; i < prices.size(); i++) 
     std::cout << prices[i] << "  ";
 std::cout << std::endl;
 costs = prices;
 costs.push_front(5.64);  // add 5.64
 costs.push_front(20.31); // add 20.31
 costs.at(1) += 10.0;     // add 10.0
 for(int i = 0; i < costs.size(); i++)
     std::cout << costs[i] << "  ";
 std::cout << std::endl;  }
A
  1. 5 32.43

20. 31 15.64 10.5 32.43

18
Q

Characteristics of the list class:

A
  • doubly linked sequences that are optimized for insertion and removal of elements anywhere throughout the list
  • sub-optimal for fast random access
19
Q

List class member functions:

iterator insert(iterator position, const T& t);

iterator erase(iterator position, const T& t);

A
  • adds element T at the iterator position

- remove element T at the iterator position

20
Q

What member functions does the list class omit?

A

the subscripting operators and the at(int) member functions

21
Q

Standard library adapters

stack

queue

priority_queue

A
  • last in, first out (LIFO) context
  • first in, first out (FIFO) context
  • first element is always the greatest
22
Q

What is the default container class defined by the stack, queue, and priority_queue adaptors?

A

deque

23
Q

Member functions of the stack class

A
explicit stack() - default - creates a stack with no elements
explicit stack(const Container& c) - creates a stack initialized to a copy of container c
stack& operator=(const stack& s) - copies the contents of s into the current object
~stack() - destroys the stack
size_t size() const - returns the number of elements in the current object
bool empty() const - returns true if the current object has no elements
T& top() - returns a reference to the top element of the stack
const T& top() const - returns an unmodifiable reference to the top element of the stack
void push(const T& t) - adds element t to the top of the stack
void pop() - removes the top element from the stack
24
Q

What will be the output?

 #include 
 #include 
 int main() {
     std::stack prices; // initially empty 
 prices.push(10.43); // add 10.43
 prices.push(20.54); // add 20.54
 prices.push(32.43); // add 32.43
 prices.top() = 5.41;
 while(!prices.empty()) {
     std::cout << prices.top() << "  ";
     prices.pop();
 }
 std::cout << std::endl;  }
A

5.41 20.54 10.43

25
Q

Additional member functions of the queue class

T& front();

const T& front() const;

T& back();

const T& back() const;

A
  • returns a reference to the first element of the queue
  • returns an unmodifiable reference to the first element of the queue
  • returns a reference to the last element of the queue
  • returns an unmodifiable reference to the last element of the queue
26
Q

What will be the output?

 #include 
 #include 
 int main() {
     std::queue tickets; // initially empty 
 tickets.push(10); // add 10
 tickets.push(20); // add 20
 tickets.push(32); // add 32
 tickets.back() = 30;
 while(!tickets.empty()) {
     std::cout << tickets.front() << "  ";
     tickets.pop();
 }
 std::cout << std::endl;  }
A

10 20 30

27
Q

What is an iterator?

A

an object that points to an element in a sequence

28
Q

Which standard library containers require iterators to access their elements?

A

classes that do not implement contiguous storage

29
Q

The definition of an iterator takes the form:

A

Container::iterator identifier;

30
Q

to define an iterator named iter for a vector of doubles, we write

A

std::vector::iterator iter;

31
Q

STL container classes include these member functions that return iterators:

A

iterator begin() noexcept - returns an iterator pointing to the first element in a sequence

iterator end() noexcept - returns an iterator pointing to the element one past the end of a sequence

const_iterator cbegin() const noexcept - returns an iterator pointing to the first element unmodifiable in a sequence

const_iterator cend() noexcept - returns an iterator pointing to the element unmodifiable one past the end of a sequence

32
Q

What do these operators do on iterators?

*

A

point to next element

point to previous element

return value of element

33
Q

What will be the output?

#include 
 #include 

int main() {

 std: :vector prices;  // initially empty
 std: :vector::iterator i;

 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(i = prices.begin(); i != prices.end(); i++) 
     std::cout << *i << "  ";
 std::cout << std::endl;  }
A

10.43 20.54 32.43

34
Q

Iterator functions for inserting and removing elements anywhere within a collection

A

iterator insert(iterator position, const T& t) - inserts t at position p and returns an iterator pointing to the inserted element

void insert(iterator position, size_t n, const T& t) - inserts t n times at position p

void insert(iterator position, InIter f, InIter l) - inserts the range [f,l) at position p

iterator erase(iterator p) - removes the element at position p and returns an iterator to the next element

iterator erase(iterator f, iterator l) - removes the elements in the range [f,l) and returns an iterator to the next element

35
Q

What will be the output?

 #include 
 #include 
 int main() {
     std::list prices;  // initially empty
 prices.push_back(10.43);   // add 10.43
 prices.push_back(20.54);   // add 20.54
 prices.push_back(32.43);   // add 32.43
 prices.insert(--prices.end(), 12.52);
 prices.erase(++prices.begin());
 for(auto i = prices.begin(); i != prices.end(); i++) 
     std::cout << *i << "  ";
 std::cout << std::endl;  }
A

10.43 12.52 32.43

36
Q

What will be the output?

A
  1. 5 32.43

10. 5 15.64 32.43 20.31