module 5 Flashcards

(18 cards)

1
Q

Sequential Container Types:

A
  • vector - flexible-size array with fast random access and slow insertion/deletion other than at the back
  • deque - double-ended queue with fast random access and fast insertion/deletion at the front and the back
  • list - doubly linked list with only bidirectional sequential access and fast insertion/deletion at any point in the list
  • forward-list - singly linked list with sequential access in only one direction and fast insertion/deletion at any point in the list
  • array - fixed-size array with fast random access but cannot add or remove elements
  • string - a specialized container, similar to a vector, that contains characters, supports fast random access and fast insertion/deletion in the front and back
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

common container operations

A
  • c.size() - number of elements in the c (not valid for forward_list
  • c.max_size() - max number of elements c can hold
  • c.empty() - returns a bool, true if empty, otherwise false
  • c.insert(args) - copy elements as specified by args into c
  • c.emplace(inits) - use inits to construct an element in c
  • c.erase(args) - remove all elements specified by args
  • c.clear() - remove all elements from c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Add elements to the vector using emplace_back():

struct Foo {
    Foo(int en, const string& ess) : n(en), s(ess) {}
    int n;
    string s;
};

int main() {
    vector<Foo> v;
		// add elements here
}
A
v.emplace_back(1, "one");
v.emplace_back(3, "three);
v.emplace_back(5, "five");

the same as v.push_back(Foo(1, "one"));

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

Which functions return a const_iterator to the first and last element?

A

container.cbegin(), and containter.cend()

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

Which functions return a reverse_iterator to the first and last element?

A

container.rbegin() and containter.rend()

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

Which functions return a const_reverse_iterator to the first and last element?

A

container.crbegin() and containter.crend()

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

Assign the values from the vector container to the list container:

list<string> names;
vector<const char*> oldstyle;
A
names.assign(oldstyle.cbegin(), oldstyle.cend());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Specialized Containers

A
  • stack
  • queue
  • priority_queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

stack methods

A
  • s.pop() - removes but does not return the top element of the stack
  • s.push(item) - creates a new top element on the stack by copying or moving item
  • s.emplace(args) - creates a new top item by constructing the element from the args
  • s.top() - returns but does not remove the top element of the stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

queue methods

A
  • q.pop() - removes but does not return the front element of the queue
  • q.front() - returns, but does not remove, the front element of the queue
  • q.back() - returns, but does not remove, the back element of the queue
  • q.push(item) - creates a new element at the back of the queue by copying or moving item
  • q.emplace(args) - creates a new back item by constructing the element from the args
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

priority_queue methods

A
  • q.pop() - removes but does not return the front element of the priority_queue
  • q.front() - returns, but does not remove, the front element of the priority_queue
  • q.top() - returns, but does not remove, the highest-priority element of the priority_queue
  • q.push(item) - creates a new element at the back of the priority_queue by copying or moving item
  • q.emplace(args) - creates a new back item by constructing the element from the args
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Associative Containers with elements ordered by key

A
  • map - associative array, holds key-value pairs
  • set - container in which the key is the value
  • mulitmap - map in which a key can appear multiple times
  • mulitset - set in which a key can appear multiple times

use tree-based storage with O(logn) retrieval

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

Associative Containers with unordered collections

A
  • unordered_map - map organized by hash function
  • unordered_set - set organized by hash function
  • unordered_multimap - hashed map; keys can appear multiple times
  • unordered_multiset - hashed set; keys can appear multiple times

containers are hash tables, O(1) retrieval complexity

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

write a pair (defined in <utility>) that will hold an int and a string, then print the second value

A
#include <utility>
#include <string>
#include <iostream>

using namespace std;

int main() {
    pair<int, string> p1 {10, "ten"};
    cout << p1.second << endl;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Erase Operations

A
  • c.erase(k) - removes every element with key k from c, returns size_t indicating number of elements removed
  • c.erase(p) - removes the element denoted by the iterator p from c. returns an iterator to the element after p or c.end() if p is the last element in c
  • c.erase(b, e) - removes the elements in the range denoted by the iterator pair b, e. returns e
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Unordered Container Operations - Bucket Interface

A
  • c.bucket_count() - number of buckets in use
  • c.max_bucket_count() - largest number of buckets this container can hold
  • c.bucket_size(n) - number of elements in the nth bucket
  • c.bucket(k) - bucket in which elements with key k would be found
17
Q

Unordered Container Operations - Bucket Iteration

A
  • local_iterator - iterator type that can access elements in a bucket
  • const_local_iterator - const version of local_iterator
  • c.begin(n), c.end(n) - Iterator to the first, and one past the last element in bucket n
  • c.cbegin(n), c.cend(n) - returns const_local_iterator
18
Q

Unordered Container Operations - Hash Policy

A
  • c.load_factor() - Average number of elements per bucket, returns float
  • c.max_load_factor() - Average bucket size that c tries to maintain. c addds buckes to keep load_factor < max_load_factor. returns float
  • c.rehash(n) - reorganize storage so that bucket_count > size / max_load_factor
  • c.reserve(n) - reorganize so that c can hold n elements without rehash