Home
71.
When should I favor macro functions over templates if the functionality in question can be implemented in both?

Ideally, you should always favor templates as the templates allow for generic implementation that is also type safe. Macros don't allow for type-safe implementations and are best avoided.

72.
Do I need to specify template arguments when invoking a template function?

Normally not as the compiler can infer this for you, given the arguments used in the function call.

73.
How many instances of static variables exist for a given template class?

This is entirely dependent on the number of types for which the template class has been instantiated. So, if your class has been instantiated for an int, a string, and a custom type X, you can expect three instances of your static variable to be available-one per template instantiation.

74.
I need to use an array. I don't know the number of elements it needs to contain. What STL container should I use?

A std::vector or a std::deque is perfectly suited to this requirement. Both manage memory and can dynamically scale themselves to an application's increasing requirements.

75.
My application has a requirement that involves frequent searches. What kind of container should I choose?

An associative container like std::map or std::set or the unordered variants thereof are most suited to requirements that involve frequent searches.

76.
I need to store key-value pairs for quick lookup. However, the use-case can result in multiple keys that are not unique. What container should I choose?

An associative container of type std::multimap is suited to this requirement. A multimap can hold nonunique key-value pairs and can offer a quick lookup that is characteristic of associative containers.

77.
An application needs to be ported across platforms and compilers. There is a requirement for a container that helps in a quick lookup based on a key. Should I use std::map or std::hash_map?

Portability is an important constraint and using standard-compliant containers is necessary. hash_map is not part of the C++ standard and therefore may not be supported across all platforms relevant to your application. You may use std::unordered_map if you are using C++11-compliant compilers for all the platforms concerned.

78.
What would be your choice of a container that has to contain an array of objects with insertion possible at the top and at the bottom?

A std::deque. Only a deque simulates a dynamic array and also allows constanttime insertions at the front and at the back of the container. A std::vector does not allow insertions at the beginning and is therefore unsuited.

79.
You need to store elements for quick lookup. What container would you choose?

A std::set or a std::map if you have key-value pairs. If the elements need to be available in duplicates, too, you would choose std::multiset or std::multimap.

80.
You need to store elements in a std::set but still have the storage and lookup criteria altered, based on conditions that are not necessarily the value of the elements. Is this possible?

Yes. When you instantiate a std::set template, you can optionally supply a second template parameter that is a binary predicate that the set class uses as the sort criterion. Program this binary predicate to criteria that are relevant to your requirements. It needs to be strict-weak ordering compliant.