• Home
  • Textbooks
  • Starting out with C++: From Control Structures through Objects
  • The Standard Template Library

Starting out with C++: From Control Structures through Objects

Tony Gaddis

Chapter 17

The Standard Template Library - all with Video Answers

Educators


Chapter Questions

Problem 1

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?

Check back soon!

Problem 2

What is the difference between the vector class’s size() member function and the capacity() member function?

Check back soon!

Problem 3

If you want to store objects of a class that you have written as values in a map, what requirement must the class meet?

Check back soon!

Problem 4

If you want to store objects of a class that you have written as keys in a map, what requirement must the class meet?

Check back soon!

Problem 5

What is the difference between a map and an unordered_map?

Check back soon!
01:36

Problem 6

What is the difference between a map and a multimap?

Rabeya Zahid
Rabeya Zahid
Numerade Educator

Problem 7

What happens if you use the insert () member function to insert a value into a set, and that value already exists in the set?

Check back soon!

Problem 8

If you want to store objects of a class that you have written as keys in a set, what requirement must the class meet?

Check back soon!

Problem 9

What is the difference between a set and a multiset?

Check back soon!

Problem 10

How does the behavior of the count () member function differ between the set class and the multiset class?

Check back soon!

Problem 11

How does the behavior of the equal_range () member function differ between the set class and the multiset class?

Check back soon!

Problem 12

What are two differences between the set and unordered_set classes?

Check back soon!
02:43

Problem 13

How is a tuple different from an array?

Prabhakar Kumar
Prabhakar Kumar
Numerade Educator

Problem 14

What are two different ways to unpack a tuple and assign its values to individual variables?

Check back soon!

Problem 15

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?

Check back soon!

Problem 16

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?

Check back soon!
00:07

Problem 17

What is a function object?

Amy Jiang
Amy Jiang
Numerade Educator

Problem 18

If you want to create function objects from a class, what must the class overload?

Check back soon!

Problem 19

What is an anonymous function object?

Check back soon!
01:33

Problem 20

What is a lambda expression?

Sanchit Jain
Sanchit Jain
Numerade Educator

Problem 21

There are two types of container classes in the STL: ____________ and ___________.

Check back soon!

Problem 22

A(n) __________ container organizes data in a sequential fashion similar to an array.

Check back soon!

Problem 23

A container ______________ class is not itself a container, but a class that adapts a container to a specific use.

Check back soon!

Problem 24

A(n) ________ container stores its data in a nonsequential way that makes it faster to locate elements.

Check back soon!

Problem 25

____________ are pointer-like objects used to access data stored in a container.

Check back soon!
00:34

Problem 26

Each element that is stored in a map has two parts: a _________ and a ________.

Linh Vu
Linh Vu
Numerade Educator

Problem 27

_____________ is a map container that allows duplicate keys.

Check back soon!

Problem 28

The ___________ class is an associative container that stores a collection of unique, sorted values.

Check back soon!

Problem 29

You can use the ________________ function to make a tuple.

Check back soon!

Problem 30

You can use the ______________ function to retrieve a value from a tuple.

Check back soon!

Problem 31

The ______________ header file defines several function templates that implement useful algorithms.

Check back soon!

Problem 32

A _________________ is a pointer to a function’s executable code.

Check back soon!

Problem 33

A _______________ object is an object that can be called, like a function.

Check back soon!
02:05

Problem 34

A _______________ is a function or function object that returns a Boolean value.

Kelsey Dondelinger
Kelsey Dondelinger
Numerade Educator

Problem 35

A _______________ is a predicate that takes one argument.

Check back soon!

Problem 36

A _______________ is a predicate that takes two arguments.

Check back soon!

Problem 37

A ______________ is a compact way of creating a function object without having to write a class declaration.

Check back soon!

Problem 38

True or False
T F The array class is a fixed-size container.

Check back soon!

Problem 39

True or False
T F The vector class is a fixed-size container.

Check back soon!

Problem 40

True or False
T F You use the * operator to dereference an iterator.

Check back soon!

Problem 41

True or False
T F You can use the ++ operator to increment an iterator.

Check back soon!

Problem 42

True or False
T F A container’s end() member function returns an iterator pointing to the last element in the container.

Check back soon!

Problem 43

True or False
T F A container’s rbegin() member function returns a reverse iterator pointing to the first element in a container.

Check back soon!

Problem 44

True or False
T F You do not have to declare the size of a vector when you define it.

Check back soon!

Problem 45

True or False
T F A vector uses an array internally to store its elements.

Check back soon!

Problem 46

True or False
T F A map is a sequence container.

Check back soon!

Problem 47

True or False
T F You can store duplicate keys in a map container.

Check back soon!

Problem 48

True or False
T 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.

Check back soon!

Problem 49

True or False
T F All the elements in a set must be unique.

Check back soon!

Problem 50

True or False
T F The elements in a set are sorted in ascending order.

Check back soon!

Problem 51

True or False
T F If the same value appears more than once in the initialization list of a set definition, an exception will occur at runtime.

Check back soon!

Problem 52

True or False
T F The unordered_set container has better performance than the set container.

Check back soon!

Problem 53

True or False
T F A tuple can hold multiple values of different data types.

Check back soon!

Problem 54

True or False
T F Once a tuple has been created, its contents cannot be modified.

Check back soon!

Problem 55

True or False
T 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.

Check back soon!

Problem 56

True or False
T F You must sort a range of elements before searching it with the binary_search() function.

Check back soon!

Problem 57

True or False
T F Any class that will be used to create function objects must overload the operator[] member function.

Check back soon!

Problem 58

True or False
T F Writing a lambda expression usually requires more code than writing a function object’s class declaration and then instantiating the class.

Check back soon!
02:28

Problem 59

True or False
T F You can assign a lambda expression to a variable, and then use the variable to call the lambda expression’s function object.

Caleb Wood
Caleb Wood
Numerade Educator
01:41

Problem 60

Write a statement that defines an array object named numbers that can hold ten elements of the double data type.

Willis James
Willis James
Numerade Educator

Problem 61

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.

Check back soon!
01:41

Problem 62

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.

Willis James
Willis James
Numerade Educator

Problem 63

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

Check back soon!

Problem 64

Write a range-based for loop that displays all of the elements of the vector that you defined in question 56.

Check back soon!

Problem 65

Write a for loop that uses an iterator to display all of the elements of the vector that you defined in question 56.

Check back soon!

Problem 66

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();

Check back soon!

Problem 67

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

Check back soon!

Problem 68

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

Check back soon!

Problem 69

Write a statement that defines a map named food. The keys should be strings, and the values should be ints.

Check back soon!

Problem 70

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"

Check back soon!

Problem 71

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.

Check back soon!

Problem 72

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.

Check back soon!

Problem 73

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.

Check back soon!
05:29

Problem 74

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.

Willis James
Willis James
Numerade Educator
00:39

Problem 75

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 and
10 as arguments. The result should be assigned to an int variable named product.

Wesley Hines
Wesley Hines
Numerade Educator

Problem 76

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;

Check back soon!

Problem 77

Each of the following code snippets has errors. Find as many as you can.

// This code has an error.
vector<string> strv = {"one", "two", "three"};
vector<string>::iterator it = strv.cbegin();

Check back soon!

Problem 78

Each of the following code snippets has errors. Find as many as you can.

// This code has an error.
vector<int> numbers(10);
for (int index = 0; index < numbers.length(); index++)
numbers[index] = index;

Check back soon!

Problem 79

Each of the following code snippets has errors. Find as many as you can.

// This code has an error.
vector<int> numbers = {1, 2, 3};
numbers.insert(99);

Check back soon!

Problem 80

Each of the following code snippets has errors. Find as many as you can.

// This code has an error.
map<string, string> contacts;
contacts.insert("Beth Young", "555-1212);

Check back soon!

Problem 81

Each of the following code snippets has errors. Find as many as you can.

// This code has an error.
multimap<string, string> phonebook;
phonebook["Megan"] = "555-1212";

Check back soon!

Problem 82

Each of the following code snippets has errors. Find as many as you can.

// 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";

Check back soon!

Problem 83

Each of the following code snippets has errors. Find as many as you can.

// This code has an error.
auto sum = ()[int a, int b] { return a + b; };

Check back soon!