Home
101.
How would I declare a map of integers to be sorted or stored in order of descending magnitude?

map <int> defines a map 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 map <int, less <int>>. To sort in order of descending magnitude, define the map as map <int, greater <int>>.

102.
What would happen if in a map of strings I inserted the string "Jack" twice?

A map is not meant to be used to insert non-unique values. So, the map would still contain only one pair having a key called "Jack".

103.
I have found an element in the map 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 map via an iterator returned by find(). This, however, is not the correct thing to do. An iterator to an element in the map should be used as a const iterator—even when your implementation of STL has not enforced it as such.

104.
What kind of a function object should I use in a call to a function such as remove_if()?

You should use a unary predicate that would take the value to be processed as the initial state via the constructor.

105.
Is it possible that a simple function with no return value can be used as a predicate?

Yes. A function with no return values can still do something useful. For example, it can display input data.

106.
Should I always prefer a lambda over a function object?

Lambdas that span multiple lines as shown in Listing 22.5 might not help increase programming efficiency over function objects that are easily reused.

107.
How are the state parameters of a lambda transferred, by value or by reference?

When a lambda is programmed with a capture list as this:
[Var1, Var2, ... N](Type& Param1, ... ) { …expression ;} the state parameters Var1 and Var2 are copied (not supplied as a reference). If you want to have them as reference parameters, you use this syntax:
[&Var1, &Var2, ... &N](Type& Param1, ... ) { ...expression ;}
In this case, you need to exercise caution as modifications to the state variables supplied within the capture list continue outside the lambda.

108.
Can I use the local variables in a function in a lambda?

You can pass the local variables in a capture list:
[Var1, Var2, ... N](Type& Param1, ... ) { ...expression ;}
If you want to capture all variables, you use this syntax:
[=](Type& Param1, ... ) { ...expression ;}

109.
Would I use a mutating algorithm, such as std::transform(), on an associative container, such as std::set?

Even if it were possible, this should not be done. The contents of an associative container should be treated as constants. This is because associative containers sort their elements on insertion, and the relative positions of the elements play an important role in functions such as find() and also in the efficiency of the container. For this reason, mutating algorithms, such as std::transform(), should not be used on STL sets.

110.
I need to set the content of every element of a sequential container to a particular value. Would I use std::transform()for this activity?

Although std::transform() could be used for this activity, fill() or fill_n() is more suited to the task.