Summary of commonly used algorithms in c++ stl

std::sort

  • Sorting container elements will change the order of the container. The default order is from small to large.

    void sort( RandomIt first, RandomIt last );
    
  • comp: returns true if the first argument is less than the second.

    struct {
         bool operator()(int a, int b) const { return a < b; }
     } customLess;
    
  • Algorithm complexity: nlogn

std::partial_sort

  • Sorting the [first, middle) elements in the container will change the order of the container. The default order is from small to large.

    void partial_sort( RandomIt first, RandomIt middle, RandomIt last );
    
  • comp: returns true if the first argument is less than the second.

  • Algorithmic complexity: Approximately (last-first)log(middle-first) applications of cmp.

std::nth_element

  • Finding the nth element in the container that is assumed to be sorted from small to large will change the order of the container and is generally used to find the median.

    void nth_element( RandomIt first, RandomIt nth, RandomIt last );
    
  • comp: returns true if the first argument is less than the second.

  • Algorithm complexity: Linear in std::distance(first, last) on average.

std::partition

  • Put the items in the container that meet the conditions on one side, and the items that do not meet the conditions on the other side. Return the pointer between the two sides before the start of the other side, which will change the order of the container.

  • UnaryPredicate: unary predicate which returns true if the element should be ordered before other elements.

    auto it = std::partition(v.begin(), v.end(), [](int i){return i % 2 == 0;});
    
  • Algorithm complexity: n

std::lower_bound

  • Find the first element in the container that is greater than or equal to a value without changing the order of the container, requiring the container to be ordered.
    Returns an iterator pointing to the first element in the range [first, last) that does not satisfy element < value(or comp(element, value )),
    (ie greater or equal to), or last if no such element is found.

    ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
    
  • comp: binary predicate which returns true if the first argument is less than the second.

    auto prc_info = std::lower_bound(prices.begin(), prices.end(), to_find,
            [](const PriceInfo& info, double value)
            {
                return info.price < value;
            });
    
  • Algorithm complexity: logn

std::upper_bound

  • Finds the first element in the container that is greater than one value, does not change the order of the container, and requires the container to be ordered.
    Returns an iterator pointing to the first element in the range [first, last) such that value < element
    (or comp(value, element)) is true (ie strictly greater), or last if no such element is found.

    ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
    
  • comp: binary predicate which returns true if the first argument is less than the second.

    auto prc_info = std::upper_bound(prices.begin(), prices.end(), to_find,
              [](double value, const PriceInfo& info)
              {
                  return value < info.price;
              });
    
  • Algorithm complexity: logn

std::equal_range

  • Returns a range containing all elements equivalent to value in the range [first, last). Requires the container to be ordered

    Possible implementation:

    template<class ForwardIt, class T>
    std::pair<ForwardIt, ForwardIt>
        equal_range(ForwardIt first, ForwardIt last, const T& value) {
        return std::make_pair(std::lower_bound(first, last, value),
                              std::upper_bound(first, last, value));
    }
    
  • comp: binary predicate which returns true if the first argument is less than the second.

  • Algorithm complexity: 2 * logn

std::binary_search

  • Binary search for element equivalent to value requires the container to be in order and does not change the order of the container.
    Possible implementation:

    template<class ForwardIt, class T>
    bool binary_search(ForwardIt first, ForwardIt last, const T& value)
    {
      first = std::lower_bound(first, last, value);
      return (!(first == last) && !(value < *first));
    }
    
  • comp: binary predicate which returns true if the first argument is less than the second.

  • Algorithm complexity: logn

std::transform

  • Apply a certain algorithm to the elements in one container and apply it to another container

  • unary_op/binary_op:

    std::transform(s.cbegin(), s.cend(),
                    s.begin(), // write to the same location
                    [](unsigned char c) { return std::toupper(c); });
    
    std::transform(s.cbegin(), s.cend(), std::back_inserter(ordinals),
     [](unsigned char c) { return c; });
    
    std::transform(ordinals.cbegin(), ordinals.cend(), ordinals.cbegin(),
                    ordinals.begin(), std::plus<>{});
    
  • Algorithm complexity: n

std::unique

  • One of the two adjacent equivalent elements in the remover. It is usually used in conjunction with erase to complete the true deletion and does not require the container to be ordered.

    left_boundary.erase(std::unique(left_boundary.begin(), left_boundary.end()),
                      left_boundary.end());
    
  • BinaryPredicate: binary predicate which returns true if the elements should be treated as equal.

  • Algorithm complexity: n

std::remove/std::remove_if

  • Remove elements from a container that meet specific conditions. It is usually used in conjunction with erase to complete deletion in the true sense and does not require the container to be in order.

    auto noSpaceEnd = std::remove(str1.begin(), str1.end(), ' ');
    str1.erase(noSpaceEnd, str1.end());
    
  • UnaryPredicate: unary predicate which returns true if the element should be removed.

    str2.erase(std::remove_if(str2.begin(),
                               str2.end(),
                               [](unsigned char x){return std::isspace(x);}),
                str2.end());
    
  • Algorithm complexity: n

std::for_each

  • Call a function in a loop on a container

    std::for_each(nums.begin(), nums.end(), [](int &n){ n++; });
    

    Because C++11 introduces for(auto var : vars)the range-based for loop syntax, the semantic expression of for_each pales in comparison. In most cases, for_each can be unnecessary.

  • Algorithm complexity: n

std::min_element/std::max_element

  • Find the largest/smallest element in the container, using <comparison by default

  • comp: comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second.

    result = std::max_element(v.begin(), v.end(), [](int a, int b) {
         return std::abs(a) < std::abs(b);
     });
    
  • Algorithm complexity: n

Guess you like

Origin blog.csdn.net/zmhzmhzm/article/details/128383876