What's New in the C++11 Standard Template Library?

What's New in the C++11 Standard Template Library?

This is a short article to introduce, in a reference fashion, some of the new algorithms and containers that are now part of the C++11 STL. Highlights include a hash object (unordered_map), a singly-linked list (forward_list) and many algorithms which attempt to "fill in the missing pieces" left by C++03.

Containers

The new containers have essentially been introduced for performance reasons. C++ now has a hash table, a singly-linked list and an array object as part of the STL standard.

  • unordered_set - As with a set, the unordered_set contains at most one of each value and allow fast retrieveal of elements. Allows forward iterators.
  • unordered_multiset - As with a multiset, the unordered_multiset can contain multiple copies of the same value and allows fast retrieval of elements. Allows forward iterators.
  • unordered_map - A hash table! As with a map, the unordered_map can contain as most one of each key with fast retrieval. Allows forward iterators.
  • unordered_multimap - As with multimap, the unordered_multimap can contain multiple copes of the same key. Allows forward iterators.
  • forward_list - This is a singly-linked list. It provides constant time inserts and erases, but no random access to elements.
  • array - C++11 now has an STL array. It stores n elements of type T contiguously in memory. It differs from a vector as it cannot be resized once created.

Algorithms

C++11 has introduced some new algorithms to "fill in the blanks" left by the previous C++03 standard. Other algorithms exist to simplify common programming tasks, either by using a simpler interace or better parameter lists. As of 2013, compiler support for these algorithms is relatively good, but you should consult the documentation of your compiler's STL implementation.

  • all_of - This returns true if all the values in the range satisfy a predicate, or the range is empty.
  • any_of - This returns true if any of the values in the range satisfy a predicate, or the range is empty.
  • none_of - This returns true if none of the values in the range satisfy a predicate, or the range is empty.
  • find_if_not - This returns an iterator to the first value that causes a predicate to be false (similar to partition_point). This uses a linear search mechanism.
  • copy_if - This copies all elements that satisfy a predicate into another range.
  • copy_n - This copies n elements from a range into another range.
  • unitialized_copy_n - Similar to unitialized_copy, except that it works for n elements.
  • move - This moves elements from one range into another.
  • move_backward - This moves elements from one range into another, reversing the order of the move.
  • is_partitioned - This returns true if all of the elements in a range that satisfy a predicate are before all of those that do not. It also returns true if the range is empty.
  • partition_copy - This copies elements from a source range into two separate destination ranges based on whether the elements satisfy a predicate or not.
  • partition_point - This returns an iterator to the first value that causes a predicate to be false (similar to find_if_not). This uses a binary search mechanism.
  • partial_sort_copy - This copies all sorted elements from a source to a range. The number of elements copied is determined by the smaller of the sorted source range and the result range. Can also use an optional sorting comparison operator.
  • is_sorted - This returns true if the range is sorted. Can also use an optional sorting comparison operator.
  • is_sorted_until - This returns an iterator to the last position for which the range is sorted. Can also use an optional sorting comparison operator.
  • is_heap - This returns true if the range is a heap, i.e. the first element is the largest. Can also use an optional sorting comparison operator.
  • is_heap_until - This returns an iterator to the last position for which the range is a heap. Can also use an optional sorting comparison operator.
  • min - Finds the smallest value in the parameter list. Can also use an optional sorting comparison operator.
  • max - Finds the largest value in the parameter list. Can also use an optional sorting comparison operator.
  • minmax - This returns a pair of the smallest and largest elements. Can also use an optional sorting comparison operator.
  • minmax_element - This returns two iterators, one pointing to the smallest element in the range and the other pointing to the largest element in the range. Can also use an optional sorting comparison operator.
  • iota - This creates a range of sequentially increasing values, making use of the pre-increment operator (++i) to create the sequence.

This concludes our tour of the C++ Standard Template Library. Make sure to look at the C++03 articles on containers, iterators and algorithms as most of the standard has not changed.