Home
111.
Does copy_backward() reverse the contents of the elements in the destination container?

No, it doesn’t. The STL algorithm copy_backward() reverses the order in which elements are copied but not the order in which elements are stored; that is, it starts with the end of the range and reaches the top. To reverse the contents of a collection, you should use std::reverse().

112.
Should I use std::sort() on a list?

std::sort() can be used on a list in the same way it can be used on any sequential container. However, the list needs to maintain a special property that an operation on the list does not invalidate existing iterators—a property that std::sort() cannot guarantee to upkeep. So, for this reason, STL list supplies the sort() algorithm in the form of the member function list::sort(), which should be used because it guarantees that iterators to elements in the list are not invalidated even if their relative positions in the list have changed.

113.
Why is it important to use functions such as lower_bound() or upper_ bound() while inserting into a sorted range?

These functions supply the first and the last positions, respectively, where an element can be inserted into a sorted collection without disturbing the sort.

114.
Can an element in the middle of a stack be modified?

No, for this would contradict the purpose of a stack, which is supposed to be a last-in-first-out container.

115.
Can I iterate through all the elements of a queue?

The queue does not feature iterators, and elements in a queue can be accessed only at its ends.

116.
Can STL algorithms work with adaptive containers?

STL algorithms work using iterators. Because neither the stack nor the queue class supplies iterators that mark the end of the ranges, the use of STL algorithms with these containers would not be possible.

117.
Given a situation where std::bitset and vector<bool> can both be used, which of the two classes would you prefer to hold your binary flags?

The bitset, as it is most suited to this requirement.

118.
I have a std::bitset object called myBitSet that contains a certain number of stored bits. How would I determine the number of bits that are at value 0 (or false)?

bitset::count() supplies the number of bits at value 1. This number, when subtracted from bitset::size() (which indicates the total number of bits stored), would give you the number of 0s in the sequence.

119.
Can I use iterators to access the individual elements in a vector<bool>?

Yes. Because the vector<bool> is a partial specialization of the std::vector, iterators are supported.

120.
Can I specify the number of elements to be held in a vector<bool> at compile time?

Yes, by either specifying the number in the overloaded constructor or using vector<bool>::resize() function at a later instance.