What is STL? How is it different from the C++ Standard Library? Why is it gaining importance among the programmers?
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.
List the three types of containers.
- 1. sequence containers
- 2. Associative containers
- 3. Derived containers.
What is the major difference between a sequence container and an associative container?
Sequence container store elements in a linear sequence. Associative containers store data in a structure called tree which facilities fast searching, deletion and insertion.
What are the best situations for the use of the sequence containers?
When we need deals with linear data structures such as array, then we should use sequence container.
What are the best situations for the use of the associative containers?
When we need non-linear containers that stores sets of values or key/value pair, then we should use associative container.
What is an iterator? What are its characteristics?
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 |
What is an algorithm? How STL algorithms are different from the conventional algorithms?
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.
How are the STL algorithms implemented?
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.
Distinguish between the following:
- (a) lists and vectors
- (b) sets and maps
- (c) maps and multimaps
- (d) queue and deque
- (e) arrays and vectors
- (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.
Compare the performance characteristics of the three sequence containers.
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 |
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.
- (a) vector
- (b) deque
- (c) list
- (d) vector
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.
- (a) TRUE
- (b) TRUE
- (c) FALSE
- (d) FALSE
- (f) FALSE
- (g) TRUE
- (h) FALSE
- (i) TRUE
- (j) FALSE
Next Previous