June 2017
Intermediate to advanced
532 pages
12h 59m
English
Let's write a helper that inserts an arbitrary number of variadic parameters into an std::set and returns if all the insertions are successful:
template <typename T, typename ... Ts>bool insert_all(T &set, Ts ... ts){ return (set.insert(ts).second && ...);}
So, how does this work? The insert function of std::set has the following signature:
std::pair<iterator, bool> insert(const value_type& value);
The documentation says that when we try to insert an item, the insert function will return an iterator and a bool variable in a pair. The bool value is true if the insertion is successful. If it is successful, the iterator points to the new element in the set. Otherwise, the iterator points ...
Read now
Unlock full access