Chapter Questions
What two emplacement member functions are provided by the vector class? How are these member functions different from the insert() and push_back() member functions?
What is the difference between the vector class’s size() member function and the capacity() member function?
If you want to store objects of a class that you have written as values in a map, what requirement must the class meet?
If you want to store objects of a class that you have written as keys in a map, what requirement must the class meet?
What is the difference between a map and an unordered_map?
What is the difference between a map and a multimap?
What happens if you use the insert () member function to insert a value into a set, and that value already exists in the set?
If you want to store objects of a class that you have written as keys in a set, what requirement must the class meet?
What is the difference between a set and a multiset?
How does the behavior of the count () member function differ between the set class and the multiset class?
How does the behavior of the equal_range () member function differ between the set class and the multiset class?
What are two differences between the set and unordered_set classes?
How is a tuple different from an array?
What are two different ways to unpack a tuple and assign its values to individual variables?
When using one of the STL algorithm function templates, you typically work with a range of elements that are denoted by two iterators, to what does the first iterator point? To what does the second iterator point?
You have written a class, and you plan to store objects of that class in a vector. If you plan to use the sort () and/or binary_search() functions on the vector's elements, what operator must the class overload?
What is a function object?
If you want to create function objects from a class, what must the class overload?
What is an anonymous function object?
What is a lambda expression?
There are two types of container classes in the STL: ____________ and ___________.
A(n) __________ container organizes data in a sequential fashion similar to an array.
A container ______________ class is not itself a container, but a class that adapts a container to a specific use.
A(n) ________ container stores its data in a nonsequential way that makes it faster to locate elements.
____________ are pointer-like objects used to access data stored in a container.
Each element that is stored in a map has two parts: a _________ and a ________.
_____________ is a map container that allows duplicate keys.
The ___________ class is an associative container that stores a collection of unique, sorted values.
You can use the ________________ function to make a tuple.
You can use the ______________ function to retrieve a value from a tuple.
The ______________ header file defines several function templates that implement useful algorithms.
A _________________ is a pointer to a function’s executable code.
A _______________ object is an object that can be called, like a function.
A _______________ is a function or function object that returns a Boolean value.
A _______________ is a predicate that takes one argument.
A _______________ is a predicate that takes two arguments.
A ______________ is a compact way of creating a function object without having to write a class declaration.
True or FalseT F The array class is a fixed-size container.
True or FalseT F The vector class is a fixed-size container.
True or FalseT F You use the * operator to dereference an iterator.
True or FalseT F You can use the ++ operator to increment an iterator.
True or FalseT F A container’s end() member function returns an iterator pointing to the last element in the container.
True or FalseT F A container’s rbegin() member function returns a reverse iterator pointing to the first element in a container.
True or FalseT F You do not have to declare the size of a vector when you define it.
True or FalseT F A vector uses an array internally to store its elements.
True or FalseT F A map is a sequence container.
True or FalseT F You can store duplicate keys in a map container.
True or FalseT F The multimap class’s erase() member function erases only one element at a time. If you want to erase multiple elements that all have the same key, you will have to call the erase() member function multiple times.
True or FalseT F All the elements in a set must be unique.
True or FalseT F The elements in a set are sorted in ascending order.
True or FalseT F If the same value appears more than once in the initialization list of a set definition, an exception will occur at runtime.
True or FalseT F The unordered_set container has better performance than the set container.
True or FalseT F A tuple can hold multiple values of different data types.
True or FalseT F Once a tuple has been created, its contents cannot be modified.
True or FalseT F If two iterators denote a range of elements that will be processed by an STL algorithm function, the element pointed to by the second iterator is not included in the range.
True or FalseT F You must sort a range of elements before searching it with the binary_search() function.
True or FalseT F Any class that will be used to create function objects must overload the operator[] member function.
True or FalseT F Writing a lambda expression usually requires more code than writing a function object’s class declaration and then instantiating the class.
True or FalseT F You can assign a lambda expression to a variable, and then use the variable to call the lambda expression’s function object.
Write a statement that defines an array object named numbers that can hold ten elements of the double data type.
Write a statement that defines an iterator that can be used with the array object that you defined in question 54. The iterator’s name should be it.
Write a statement that defines a vector named numbers that can hold elements of the int data type. Initialize the vector with the values 10, 20, 30, 40, and 50.
The following statement defines a vector of ints named v. Write a statement that defines another vector, named v2, which is a copy of v.vector<int> v = {1, 2, 3, 4, 5};
Write a range-based for loop that displays all of the elements of the vector that you defined in question 56.
Write a for loop that uses an iterator to display all of the elements of the vector that you defined in question 56.
The following code defines a vector and an iterator. Rewrite the statement that defines the iterator so it uses the auto key word.vector<string> strv = {"one", "two", "three"};vector<string>::iterator it = strv.begin();
The following statement defines a vector of ints named v. Write a statement that defines a const_iterator named cit, and initializes it to point to the first element in v.vector<int> v = {1, 2, 3, 4, 5};
The following statement defines a vector of ints named v. The vector’s initial contents are: 1, 2, 3, 4, 5. Write code to insert the value 999 into the vector, so its contents are: 1, 2, 999, 3, 4, 5.vector<int> v = {1, 2, 3, 4, 5};
Write a statement that defines a map named food. The keys should be strings, and the values should be ints.
Suppose a program defines a map as follows:map<int, string> customers;
Write statements that use the map class’s emplace member function to insert the following elements:
9001, "Jen Williams"9002, "Frank Smith"9003, "Geri Rose"
Look at the following vector definition:vector<int> v = {7, 2, 1, 6, 4, 3, 5};Write code that displays a message indicating whether the value 6 is found in the vector. Use the STL binary_search() algorithm to search the vector for the value 6.
Suppose the following definition appears in a program:tuple<string, int> user;Write a statement that uses the make_tuple function to assign the values "Lizzie Jones" and 199 to the user tuple.
Suppose the following definition appears in a program:tuple<int, string> customer(100, "John Smith");Write a statement that changes the first element to the value 200.
Write a declaration for a class named Display. The class should be written in such a way that it can be used to create a function object. A function object created from the class should accept an int argument, and display that argument on the screen.
Write code that does the following:• Uses a lambda expression that accepts two int arguments, a and b, and returns the value of a * b.• Assigns the lambda expression to a variable named multiply.• Uses the multiply variable to call the lambda expression, passing the values 2 and10 as arguments. The result should be assigned to an int variable named product.
Each of the following code snippets has errors. Find as many as you can.
// This code has an error.array<int, 5> a;a[5] = 99;
// This code has an error.vector<string> strv = {"one", "two", "three"};vector<string>::iterator it = strv.cbegin();
// This code has an error.vector<int> numbers(10);for (int index = 0; index < numbers.length(); index++)numbers[index] = index;
// This code has an error.vector<int> numbers = {1, 2, 3};numbers.insert(99);
// This code has an error.map<string, string> contacts;contacts.insert("Beth Young", "555-1212);
// This code has an error.multimap<string, string> phonebook;phonebook["Megan"] = "555-1212";
// This code has an error.vector<int> v = {6, 5, 4, 2, 3, 1};sort(v);if (binary_search(v, 1))cout << "The value 1 is found in the vector.\n";else"The value 1 is NOT found in the vector.\n";
// This code has an error.auto sum = ()[int a, int b] { return a + b; };