Introduction to C++ Programming, Y. Daniel Liang

Chapter 19 STL Containers


Section 19.2 STL Basics
1  Three components in STL are ____________.

A. containers
B. iterators
C. algorithms
D. linked lists
E. vectors

2  Three types of containers are ____________.

A. sequence containers
B. linear containers
C. associative containers
D. container adapters
E. wrapper containers

3  Which of the following are sequence containers?

A. list
B. queue
C. deque
D. map
E. set

4  Which of the following are associative containers?

A. list
B. queue
C. deque
D. map
E. set

5  Which of the following are container adapters?

A. list
B. queue
C. deque
D. multimap
E. stack

6  Which of the following are the common features for all containers?

A. Each container has a no-arg constructor.
B. Each container has a copy constructor.
C. Each container has the empty() function to check whether a container is empty.
D. Each container has the size() function to return the number of elements in the container.
E. Each container supports the relational operators (<, <=, >, >=, ==, and !=).

7  Which of the following are first-class containers?

A. list
B. queue
C. deque
D. multimap
E. stack

8  Which of the following containers use iterators?

A. list
B. queue
C. deque
D. multimap
E. stack

9  Which of the following are the common features for all first-class containers?

A. Each first-class container has the swap function.
B. Each first-class container has the max_size() function.
C. Each first-class container has the clear() function.
D. Each first-class container has the erase function.
E. Each first-class container has the add function.

Section 19.3 Iterators
10  To obtain an iterator for the first element in a container c, use ____________.

A. c.first()
B. c.head()
C. c.begin()
D. c.rbegin()

11  To obtain an iterator that points to the next element after the last element in a container, use _____.

A. c.last()
B. c.tail()
C. c.end()
D. c.rend()

12  To declare an iterator for a vector v of the int type, use __________.

A. vector::iterator p
B. iterator p
C. vector<int>::iterator p
D. vector<>::iterator p

13  Types of iterators are _________.

A. input iterators
B. output iterators
C. forward iterators
D. bidirectional iterators
E. random access iterators

14  ________ supports random access iterators.

A. vector
B. deque
C. list
D. set
E. map

15  ________ supports bidirectional iterators.

A. vector
B. deque
C. list
D. set
E. map

Section 19.4 Sequence Containers
16  A ______ is efficient if the elements are appended to the end, but it is expensive to insert or delete elements anywhere except at the end.

A. vector
B. deque
C. list

17  It is efficient for insertion at both front and end of a _____, but it is still expensive to insert or delete elements in the middle of it.

A. vector
B. deque
C. list

18  A ________ is good for applications that require frequent insertion and deletion in the middle of it.

A. vector
B. deque
C. list

19  Which of the following are common functions in sequence containers?

A. assign(n, element)
B. push_back(element)
C. pop_back()
D. insert(position, element)

20  To insert an element to a vector v, use ________.

A. v.assign(0, element)
B. v.insert(0, element)
C. v.push_back(element)
D. v.add(element)

21  To remove the last element from a vector v, use ________.

A. v.removeAt(v.size() - 1)
B. v.remove()
C. v.pop_back(element)
D. v.pop_back()

22  To remove a specified element from a list v, use ________.

A. v.removeAt(0)
B. v.remove(element)
C. v.pop_back(element)
D. v.pop_back()

23  You can apply the sort function on ____________.

A. vector
B. deque
C. list

24  You can apply the reverse function on ____________.

A. vector
B. deque
C. list

Section 19.5 Associative Containers
25  A ________ can have duplicate elements.

A. set
B. multiset
C. map
D. multimap

26  __________ are defined in the <set> header file.

A. set
B. multiset
C. map
D. multimap

27  All the elements in ________ are sorted on keys.

A. set
B. multiset
C. map
D. multimap

28  To insert an element to a set s, use ________.

A. s.push_back(element)
B. s.insert(element)
C. s.add(element)
D. s.insert(0, element)

29  To find a specified element in a set s, use _________.

A. s.search(element)
B. s.find(element)
C. search(s, element)
D. find(s, element)

30  To create a map with int key type and string value, use _______.

A. map<string, int> map1
B. map<int, string> map1
C. map<int> map1
D. map<string> map1

31  To insert a int key and string value to map1, use _________.

A. map1.insert(100, "John Smith");
B. map1.insert(map<int, string>::value_type("John Smith", 100));
C. map1.insert(map<int, string>::value_type(100, "John Smith"));
D. map1.insert("John Smith", 100);

Section 19.6 Container Adapters
32  Which of the following functions are in the stack class.

A. pop()
B. top()
C. push(element)
D. size()
E. empty()

33  Which of the following functions are in the deque class.

A. pop()
B. top()
C. push(element)
D. size()
E. empty()

34  Which of the following functions are in the priority_queue class.
A. pop()
B. top()
C. push(element)
D. size()
E. empty()