Introduction to Container adapters and STL algorithms

Source: Internet
Author: User

Can be implemented in some sort of sequential container.

(Let the existing sequential containers work in a stack/queue mode)

1) Stack: header file <stack> Stack-LIFO

2) Queue: Header file <queue> queue-FIFO

3) priority_queue: Header file <queue> priority queue-the highest priority element is always the first to be out of order

There are 3 member functions:

    • Push: Add a single element;
    • Top: Returns a reference to the top of the stack or to the team head element
    • Pop: Delete an element

There are no iterators on the container adapter

All sorts, lookups, and sequencing algorithms in STL are not suitable for container adapters

Priority_queue is similar to a queue and can be implemented with vectors and deque

By default, vector implementations of priority_queue are usually implemented with heap sorting techniques to ensure that the largest elements are always at the front

    • When performing a pop operation, the largest element is deleted
    • When you perform a top operation, a reference to the largest element is returned
    • The default element comparer is less<t>

STL Algorithm Classification

The algorithms in STL can be broadly divided into the following seven categories:

    • Invariant sequence algorithm
    • Variable Value algorithm
    • Delete algorithm
    • Variable Order algorithm
    • Sorting algorithms
    • Ordered interval algorithm
    • Numerical algorithms

Most overloaded algorithms are available in two versions.

    • Use "= =" to determine whether elements are equal, or to compare sizes with "<".
    • More than one type parameter "Pred" and the function parameter "Pred op": The return value of the expression "OP (x, y)": Ture/false to determine if x is "equal to" y, or if x is "less than" Y.

As below, there are two versions of Min_element

    • Iterator Min_element (iterator first, iterator last);
    • Iterator Min_element (iterator first, iterator last, Pred op);

1. invariant sequence algorithm

    • This class of algorithms does not modify the container or object that the algorithm acts on
    • Applies to sequential containers and associative containers
    • The complexity of Time is O (n)

2. variable Value algorithm

    • Such algorithms modify the values of the source or target interval elements
    • The interval in which the value is modified cannot be a part of the associated container.

3. Deleting an algorithm

    • Remove some elements from a container
    • Delete--does not reduce the element in the container
      • Consider all elements that should be deleted as empty seats
      • Move forward from behind with the left elements, then fill in the blanks.
      • When the element moves forward, its original position is empty.
      • should also be filled in by the elements left behind.
      • Finally, there is no empty space to be filled, and the original value remains unchanged.
    • The delete algorithm should not be used for associative containers
    • The complexity of the algorithm is O (n)

4. variable order algorithm

    • Sequencing algorithm changes the order of elements in a container
    • But does not change the value of the element
    • The variable order algorithm does not apply to associative containers
    • The complexity of the algorithm is O (n)

5. Sorting Algorithms

    • Higher complexity than the previous sequence algorithm, usually O (Nlog (n))
    • Sorting algorithms require support for random-access iterators
    • Not applicable to associative containers and lists

6. ordered interval algorithm

    • Required to operate the range is already from small to large row of good order
    • Support for random-access iterators required
    • Ordered interval algorithm cannot be used for associative containers and lists

Reference Links:

Https://www.coursera.org/learn/cpp-chengxu-sheji

Introduction to Container adapters and STL algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.