Describe the differences between a map and a vector.
map
is an associative container whereas vector
is a sequence container
Give an example of when each of list, vector, deque, map, and set might be most useful.
- list : anytime when a doubly-linked list is required.
- vector : anytime when a dynamic array is required.
- deque : An answer from SO.
- map : dictionary.
- set : when to keep elements sorted and unique.
Explain the difference between a map and a set. When might you use one or the other?
Explain the difference between a set and a list. When might you use one or the other?
What are the mapped_type, key_type, and value_type of a map from int to vector< int >?
- mapped_type : vector< int >
- key_type : int
- value_type : std::pair< const int, vector >
Using a map iterator write an expression that assigns a value to an element.
std::map<int, std::string> map;
map[25] = "Alan";
std::map<int, std::string>::iterator it = map.begin();
it->second = "Wang";
Assuming c is a multiset of strings and v is a vector of strings, explain the following calls. Indicate whether each call is legal:
copy(v.begin(), v.end(), inserter(c, c.end())); // legal
copy(v.begin(), v.end(), back_inserter(c)); // illegal, no `push_back` in `set`.
copy(c.begin(), c.end(), inserter(v, v.end())); // legal.
copy(c.begin(), c.end(), back_inserter(v)); // legal.
Write the type of map_it from the loop on page 430 without using auto or decltype.
std::map<std::string, size_t>::const_iterator;
Define a variable that you initialize by calling begin() on the multiset named bookstore from 11.2.2 (p. 425). Write the variable’s type without using auto or decltype.
using compareType = bool (*)(const Sales_data &lhs, const Sales_data &rhs);
std::multiset<Sales_data, compareType> bookstore(compareIsbn);
std::multiset<Sales_data, compareType>::iterator c_it = bookstore.begin();
Assuming word_count is a map from string to size_t and word is a string, explain the following loop:
while (cin >> word)
++word_count.insert({ word, 0 }).first->second;
This code can be explained like this pseudocode:
while reading into word
if word_count has key word:
word_count[word] += 1
else:
word_count[word] = 0
word_count[word] += 1
Given a
map<string, vector<int>>
, write the types used as an argument and as the return value for the version of insert that inserts one element.
std::pair<std::string, std::vector<int>> // argument
std::pair<std::map<std::string, std::vector<int>>::iterator, bool> // return
What would happen if we used the subscript operator instead of find in the transform function?
Say the code has been changed like below:
const string& transform(const string &s, const map<string, string> &m)
{
return m[s];
}
The above code won't compile because the subscript operator might insert an element (when the element with the key s is not found), and we may use subscript only on a map that is not const.
In buildMap, what effect, if any, would there be from rewriting
trans_map[key] = value.substr(1);
astrans_map.insert({ key, value.substr(1) })
?
- use subscript operator: if a word does appear multiple times, our loops will put the last corresponding phrase into trans_map
- use
insert
: if a word does appear multiple times, our loops will put the first corresponding phrase into trans_map
Our program does no checking on the validity of either input file. In particular, it assumes that the rules in the transformation file are all sensible. What would happen if a line in that file has a key, one space, and then the end of the line? Predict the behavior and then check it against your version of the program.
If so, a key-value pair will be {key, " "}
(" ".size() !> 1), which cannot be added into the map. As a result, the key would not be replaced with any string.
What are the advantages of an unordered container as compared to the ordered version of that container? What are the advantages of the ordered version?