1. Briefly explain the three components of STL. 2. Compare the three basic conta
ID: 3682754 • Letter: 1
Question
1. Briefly explain the three components of STL.
2. Compare the three basic containers in STL (list, deque and vector) regarding their operations and efficiency, and briefly discuss which container is more suitable under what situation .
3. Briefly explain the concept of iterator, and use an example to explain how to use iterator.
Standard Template Library
vector container in STL:
1) Write a template function that returns true if the values stored in a vector<T> are in ascending order and false otherwise, wher operator < is assumed to be defined for the type T. You can only iterator to implement this function.
2) Write a template function that returns the range of values stored in a vector<T>, that is, the difference between the largest value and the smallest value, where operator < is assumed to be defined for the type T. You can only iterator to implement this function.
<algorithm> and <numeric> libraries
1) Write a template function EraseAll which
receives a vector<EltType> and an EltType elt
postcondition: all elements in vec which are equal to elt have been erased.
algorithm: uses STL library functions whenever possible.
2) Write a function DiffFromAverage which
receives a list<double> lst
action: computes the average of the elements
outputs the difference between the largest element and the average
outputs the difference between the smallest element and the average
algorithm: does not contain a loop; instead uses STL library functions
Explanation / Answer
Please find the answers below :
1)
The three components of STL are :
a) Containers : They are used to handle the collections of objects of a certain kind. Examples like deque, list, vector, map.
b) Algorithms : They act on comtainers and provide a means by which you will perform initialization, sorting, searching, and transforming of the contents of containers.
c) Iterators : They are used to step through the elements of collections of objects.
2)
Comparision between vector, list and deque :
deque and vector provide random access, list provides only linear accesses. On the other hand, you can insert and remove items anywhere in a list efficiently, and operations in the middle of vector and deque are slow.
deque and vector are very similar. There are only two main differences. First, vector can only efficiently add new items at the end, while deque can add items at either end efficiently.
Unlike deque, vector guarantee that all items will be stored in contiguous memory locations, which makes iterating through them faster in some situations.
3)
Iterators are a pointer-like object that can be incremented with ++, dereferenced with *, and compared against another iterator with !=.
Iterators are generated by STL container member functions, such as begin() and end(). Some containers return iterators that support only the above operations, while others return iterators that can move forward and backward, be compared with <, and so on.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.