STL (C/C ++) (STL)

Source: Internet
Author: User

NLP self-dream's STL algorithm learning in Tianya
STLAlgorithmSummary:

Prerequisites:

Download STL Source: http://www.sgi.com/tech/stl/download.html
Open Web: http://www.sgi.com/tech/stl/stl_index.html

One function object: Function objects are used in many algorithms.

Binary Function object. V1 and V2 are input and V3 is result.

Plus <t>:
Transform (v1.begin (), v1.end (), v2.begin (), v3.begin (), plus <double> ());

Other binary function objects: minus, multiples, divieds, and modulus.

Binary asserted function object. bind2nd () or bind1st () is required to bind a comparison object.

Less <t>:
Find_if (L. Begin (), L. End (), bind2nd (less <int> (), 0 ));

Other binary assertions: pai_to, notequal_to, greater, greater_equal, less_equal, logical_and, logical_or

Binary Logical Functions

Binary_negate:
Const char * wptr = find_if (STR, STR + maxlen,
Compose2 (not2 (logical_or <bool> ()),
Bind2nd (performance_to <char> (),''),
Bind2nd (equal_to <char> (), '\ n ')));

Unary function object

Negate:
Transform (v1.begin (), v1.end (), v2.begin (),
Negate <int> ());

One-dimensional function object Determination

Logical_not:
Transform (V. Begin (), V. End (), V. Begin (), logical_not <bool> ());

Unary Logical Functions

Unary_negate:

Binary Function object generator: Mainly used to fill the sequence.

Generate random numbers that are not repeated:
// Generate unique random numbers from 0 to mod:
Class urandgen {
STD: Set <int> used;
Int limit;
Public:
Urandgen (INT Lim): limit (lim ){
Srand (time (0 ));
}
Int operator ()(){
While (true ){
Int I = int (RAND () % limit;
If (used. Find (I) = used. End ()){
Used. insert (I );
Return I;
}
}
}
};

Const int SZ = 10;
Const int max = 50;
Vector <int> X (sz), y (sz), R (sz );
// An integer random number generator:
Urandgen URG (max );
Generate_n (X. Begin (), SZ, URG );

Three Function Object Adapter: convert a function into a function object

Ptr_fun: General function Adapter

One-dollar instance:
Transform (first, last, first,
Compose1 (negate <double>, ptr_fun (FABS )));

Binary instance:
List <char * >:: iterator item =
Find_if (L. Begin (), L. End (),
Not1 (binder2nd (ptr_fun (strcmp), "OK ")));

Not1: the adapter used to reverse the judgment function object of a dollar.

Not2: the adapter used to reverse the result of determining the function object of binary.

Mem_fun and mem_fun_ref: class member function Adapter. The difference is that one needs a pointer, And the other only needs a general object. As follows:
Shape is a pointer variable, then foreach (V. Begin (), V. End (), mem_fun (& shape: Draw ));
However, if shape is a common variable rather than a pointer, foreach (V. Begin (), V. End (), mem_fun_ref (& shape: Draw ));

Four algorithms:

Copy:
Copy ()
Reverse_copy ()
Rotate_copy ()
Remove_copy () copies an element that is not equal to a value to another sequence.
Remove_copy_if () copies the matching sequence to another sequence.

Fill and generate:
Fill ()
Fill_n () fills n elements in the sequence.
Generate () is used to call the Gen () function for each element in the sequence.

Arrangement:
Next to next_permuttion.
Prev_permutation ()

Partition () is used to divide elements that meet the condition into the front of the sequence.
Stable_partition ()

Search and replace:
Find ()
Binary_search () is searched on an ordered sequence.
Find_if ()
Search () checks whether the second sequence appears in the first sequence, and the sequence is the same.

Delete: note that you must call erase () to delete the object.
Remove ()
Unique () is used to delete adjacent duplicate elements. It is best to sort them now.

Merge sequence:
Merge ()

Numeric algorithm:
Accumulate () calculates and sums each element of the sequence.
Transform () can also perform operations on each element.
Count:
Total number of size.
Count () is equal to the number of elements of a value.

The last subtraction in the adjacent_difference sequence is the first adjacent to the adjacent_difference sequence to obtain a new sequence.

Adiacent_find

5. All algorithms:

Accumlate: The sum of the elements in the flag sequence added to an initial value specified by init. The overloaded version is no longer used for addition, but the binary operators passed in are applied to elements.
Adjacent_different: Creates a new sequence. Each new value of this sequence represents the difference between the current element and the previous one. The overloaded version uses the specified binary operation to calculate the difference between adjacent elements.
Adjacent_find: searches for a pair of adjacent duplicate elements within the scope of the iterator's flag element. If a pair of adjacent duplicate elements is found, a forwarditerator is returned, pointing to the first element of the pair. Otherwise, return last. The overloaded version uses the input binary operator instead of the equal judgment.
Binary_search: searches for values in an ordered sequence. If yes, true is returned. The overloaded version uses the specified comparison function object or function pointer to determine the equality.
Copy: Copy Sequence.
Copy_backward: except that the elements are copied in the reverse order, the other elements are the same as those in copy.
Count: Use the equal operator to compare the elements of the Flag range class with the input value and return the number of equal elements.
Count_if: applies the input operator to elements of the Flag range class and returns the number of times the result is true.
Equal: if the two sequences have equal elements in the range, equal returns true. The overloaded version uses the input operator instead of the default equals operator.
Performance_range: returns an iterator. The first iterator represents the iterator returned by lower_bound, and the second represents the iterator value returned by upper_bound.
Fill: copy the input value to each element in the range.
Fill_n: Assign the input value to elements in the first to frist + N range.
Find: Compares the elements in the range with the input values by using the equals operator of the underlying elements. When the match ends, an inputiterator of the element is returned.
Find_if: the input function replaces the equal operator to execute find.
Find_end: In the range, find the last occurrence of "second sequence of the sign of another input iterator. The overload version uses the operator entered by the user to replace the equals operation.
Find_first_of: searches for the first appearance of any element in the second sequence of the sign of another input iterator within the range. The overload version uses User-Defined operators.
For_each: executes input functions for all elements in the range in sequence.
Generate: Fill in the specified range by calling the input function Gen continuously.
Generate_n: Fill n elements.
Includes: determines whether an element of [first1, last1) is included in another sequence. The <= operator of the underlying element is used, and the overloaded version uses the user-input function.
Inner_product: Perform Inner Product (the corresponding elements are multiplied and then summed) on the two sequences, and add inner product to the initial value of an input. The overloaded version uses user-defined operations.
Inner_merge: merges two consecutive chronological sequences. The result sequence covers the range of the two ends. The overloaded versions are sorted using the input operation.
Iter_swap: Exchange two forwarditerator values.
Lexicographical_compare: Compares two sequences. The reload version uses custom comparison operations.
Lower_bound: returns an iterator pointing to the first position in the ordered sequence within the range where a specified value can be inserted without disrupting the container sequence. The overload function uses custom comparison operations.
MAX: returns a larger one of the two elements. The overloaded version uses a custom comparison operation.
Max_element: returns an iterator that specifies the largest element in the sequence. Reload versions use custom comparison operations.
Min: smaller of the two elements. Reload versions use custom comparison operations.
Min_element: similar to max_element, but returns the smallest element.
Merge: merges two ordered sequences and stores them in another sequence. Reload versions use custom comparisons.
Mismatch: Compares two sequences in parallel, indicating the first unmatched position. It returns a pair of iterator, marking the first unmatched element position. If all matches, the last of each container is returned. Reload versions use custom comparison operations.
Next_permutation: retrieves the arrangement in the current range and sorts it into the next one. Reload versions use custom comparison operations.
Nth_element: sorts the sequences in the range, so that all elements smaller than the nth element appear before it, and those greater than it appear behind it, the overloaded version uses custom comparison operations.
Partial_sort: Partial sorting of the entire sequence. The number of elements to be sorted can be placed in the range. Reload versions use custom comparison operations.
Partial_sort_copy: Same as partial_sort, except for copying the sorted sequence to another container.
Partial_sum: Creates a new sequence of elements. The value of each element represents the sum of all elements before the position in the range. The overloaded version uses custom operations instead of addition.
Partition: sorts the elements in a range. Use the input function to place all elements whose calculation result is true before the elements whose results are false.
Prev_permutation: extracts the sequence in the range and sorts it back to the previous sequence. If the previous sequence does not exist, false is returned. Reload versions use custom comparison operations.
Random_shuffle: Randomly adjusts the order of elements in the range. Input a random number to generate a random number for the overloaded version.
Remove: delete all elements within the specified range. Note that this function does not actually Delete the elements. Built-in arrays are not suitable for removing and remove_if functions.
Remove_copy: Copies all mismatched elements to a specified container, and the returned outputiterator points to the next position of the last copied element.
Remove_if: delete all elements whose input results are true.
Remove_copy_if: Copies all unmatched elements to a specified container.
Replace: replace all the elements in the range equal to old_value with new_value.
Replace_copy: similar to replace, but writes the result to another container.
Replace_if: replace all elements whose operation results are true with a new value.
Replace_copy_if: similar to replace_if, but writes the result to another container.
Reverse: sorts the elements in the range in reverse order.
Reverse_copy: similar to reverse, but writes the result to another container.
Rotate: Move the elements in the range to the end of the container. The elements pointed to by middle become the first element of the container.
Rotate_copy: similar to rotate, but the result is written to another container.
Search: returns an iterator in two ranges, pointing to the position where the subsequence appears for the first time in the range. Reload versions use custom comparison operations.
Search_n: searches for subsequences whose values appear n times in a range. Reload versions use custom comparison operations.
Set_difference: Construct an ascending sequence. The elements appear in the first sequence but are not included in the second sequence. Reload versions use custom comparison operations.
Set_intersection: Construct an out-of-order sequence. The elements exist in both sequences. Reload versions use custom comparison operations.
Set_shortric_difference: constructs an out-of-order sequence. The elements appear in the first sequence, but not in the second sequence. Reload versions use custom comparison operations.
Set_union: Construct an out-of-order sequence that contains all non-repeating elements in the two sequences. Reload versions use custom comparison operations.
Sort: sorts the elements in the range in ascending order. The overloaded version uses custom comparison operations.
Stable_partition: similar to partition, but it does not guarantee that the relative sequence in the container is retained.
Stable_sort: similar to sort, but retains the sequential relationship between equal elements.
Swap: swap the values stored in two objects.
Swap_range: swap the elements in the range with the element values of another sequence.
Transform: applies input operations to each element in a range and generates a new sequence. The overloaded version applies the operation to one element, and the other element comes from another input sequence. The result is output to the specified container.
Unique: clears repeated elements in a sequence. Similar to removing, it cannot actually Delete elements. The overloaded version uses custom operations.
Unique_copy: similar to unique, but It outputs the result to another container.
Upper_bound: returns an iterator that points to the last position where values are inserted in the ordered sequence within the range without disrupting the container order. This position indicates a value greater than the value. The overloaded version uses the input comparison operation.
Heap algorithm: The C ++ Standard Library provides max-heap. A total of four generic heap algorithms are provided.
Make_heap: generates a heap for elements in the range. Reload versions use custom comparison operations.
Pop_heap: instead of actually popping up the largest element from the heap, it sorts the heap again. It exchanges first and last-1, and then creates a new heap. You can use the back of the container to access the "pop-up" element or use pop_back to delete it. Reload versions use custom comparison operations.
Push_heap: assume that the first to last-1 is a valid heap, and the elements to be added to the heap are in the last-1 position to regenerate the heap. Before pointing to this function, you must insert the element into the container. The reload version uses the specified comparison.
Sort_heap: sorts a sequence in a range. It is assumed that the sequence is an ordered heap. Reload versions use custom comparison operations.

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.