Home
91.
Are the properties of the deque any different than the vector when it comes to insertion of elements?

No, the properties of the deque are similar to that of the vector when it comes to insertion, which is a constant-time activity for elements added at the end of sequence and a linear-time activity for elements inserted in the middle. However, the vector allows insertion at only one end (the bottom), whereas the deque allows for insertion at both ends (the top and the bottom).

92.
Why does the list provide member functions such as sort() and remove()?

The STL list class is bound to respect the property that iterators pointing to elements in the list should remain valid irrespective of the position of the elements in the list itself. Although STL algorithms work on list too, the list’s member functions ensure that the aforementioned property of the list is withheld and iterators pointing to elements in the list before the sort was done continue to point to the same elements even after the sort.

93.
You are using a list of type CAnimal, which is a class. What operators should CAnimal define for list member functions to be able to work on it accurately?

You must provide the default comparison operator == and the default < operator to any class that can be used in STL containers.

94.
How would you replace keyword auto by an explicit type declaration in the following line:
list<int> linkInts(10); // list of 10 integers
auto firstElement = linkInts.begin();

You would replace auto by the following explicit type declaration:
list<int> linkInts(10); // list of 10 integers
list<int>::iterator firstElement = linkInts.begin();

95.
Is there any loss in performance when inserting items in the middle of the STL list as compared to the beginning or the end?

Elements can be inserted in the middle of the list as they can be at either end. There is no gain or loss in performance due to position.

96.
Two iterators are pointing to two elements in an STL list object, and then an element is inserted between them. Are these iterators invalidated by the insert action?

The specialty of the list is that operations such as these don’t invalidate existing iterators.

97.
Is it possible to insert multiple elements in a list?

Yes, an overloaded version of the insert() function enables you to insert a range from a source collection.

98.
How would I declare a set of integers to be sorted and stored in order of descending magnitude?

set <int> is a set of integers. This takes the default sort predicate std::less <T> to sort items in order of ascending magnitude and can also be expressed as set <int, less <int>>. To sort in order of descending magnitude, define the set as set <int, greater <int>>.

99.
What would happen if, in a set of strings, I inserted the string "Jack" twice?

A set is not meant to be used to contain non-unique values. The set of strings would contain only one instance of "Jack".

100.
I have found an element in the set using the find() function and have an iterator pointing to it. Would I use this iterator to change the value being pointed to?

No. Some STL implementations might allow the user to change the value of an element inside a set via an iterator returned by, for example, find. However, this is not the correct thing to do. An iterator to an element in the set should be used as a const iterator—even when the STL implementation has not enforced it as such.