14.1

What is STL? How is it different from the C++ Standard Library? Why is it gaining importance among the programmers?

Answer

The collection of generic classes and functions is called the standard template library (STL)

Using STL can save considerable time and effort and lead to high quality programs, thats why it is gaining importance among the programmers.

14.2

List the three types of containers.

Answer
  • 1. sequence containers
  • 2. Associative containers
  • 3. Derived containers.
14.3

What is the major difference between a sequence container and an associative container?

Answer

Sequence container store elements in a linear sequence. Associative containers store data in a structure called tree which facilities fast searching, deletion and insertion.

14.4

What are the best situations for the use of the sequence containers?

Answer

When we need deals with linear data structures such as array, then we should use sequence container.

14.5

What are the best situations for the use of the associative containers?

Answer

When we need non-linear containers that stores sets of values or key/value pair, then we should use associative container.

14.6

What is an iterator? What are its characteristics?

Answer

Iterators is just like a pointer that are used to access container elements.

 

Iterators and their characteristics

Iterator Access Method Direction of movement I/O capability Remark
Input Linear Forward only Read only Can not be saved
Output Linear Forward only Write Only Can not be saved
Forward Linear Forward only Read/write Can be saved
Bidirectional Linear Forward and Backward Read/Write Can be saved
Random Random Forward and backward Read/Write can be saved
14.7

What is an algorithm? How STL algorithms are different from the conventional algorithms?

Answer

Algorithms are functions that can be used generally across a variety of containers for processing their contents.

Conventional algorithms are very lengthy and complex, where STL algorithms are very easy and short that can save a lot of time and effort.

14.8

How are the STL algorithms implemented?

Answer

Step 1 : include < algorithm > in your program.

Step 2 : Many algorithms operate on sequences of elements only indirectly through iterator.

Step 3 : Now you can use STL algorithms. such as find ( ) to find the location of an element. STL provides you approximately 70 standard algorithms to save a lot of time and effort.

14.9

Distinguish between the following:

  • (a) lists and vectors
  • (b) sets and maps
  • (c) maps and multimaps
  • (d) queue and deque
  • (e) arrays and vectors
Answer
  • (a) Vector supports random access, and a list can be accessed sequentially only.
  • (b) set follows rapid lookup, while map follows key-based lookup.
  • (c) A map allows only one key for a given value to be stored while multimap permits multiple keys.
  • (d) In case of deque we can direct access to any element and incase of queue we follow first-in-first out
  • (e) size must be specify at compile time for array but size of vector is dynamically changed.
14.10

Compare the performance characteristics of the three sequence containers.

Answer

Comparison of sequence containers

Container Random access Insertion or deletion in the middle Insertion or deletion at the ends
Vector Fast Slow Fast at back
list Slow Fast Fast at front
deque Fast Slow Fast at both the ends
14.11

Suggest appropriate containers for the following applications:

  • (a) Insertion at the back of a container.
  • (b) Frequent insertions and deletion at both the ends of a container.
  • (c) Frequent insertions and deletions in the middle of a container.
  • (d) Frequent random access of elements.
Answer
  • (a) vector
  • (b) deque
  • (c) list
  • (d) vector
14.12

State whether the following statements are true or false.

  • (a) An iterator is a generalized form of pointer.
  • (b) One purpose of an iterator is to connect algorithms to containers.
  • (c) STL algorithms are member functions of containers.
  • (d) The size of a vector does not change when its elements are removed.
  • (e) STL algorithms can be used with c-like arrays.
  • (f) An iterator can always move forward or backward through a container.
Answer
  • (a) TRUE
  • (b) TRUE
  • (c) FALSE
  • (d) FALSE
  • (f) FALSE
  • (g) TRUE
  • (h) FALSE
  • (i) TRUE
  • (j) FALSE

Next Previous