Chapter 10: A First Look at Generic Algorithms

1. Definition: The set of operations of the standard library container is very small, so the standard library defines a set of algorithms that can be used in any container and become generic algorithms.

Overview: Most algorithms are defined in the algorithm header file and the Numeric header file .

These algorithms do not change the size of the container, but only change the elements inside, and the entire size and capacity will not change.

2. Here are some common algorithms for reading containers:

find algorithm , define a vector container of Int, want to know whether the vector contains a specific value, we call the find algorithm, it accepts three parameters, the first two parameters are iterators representing the range, and the third parameter is to find The value of the format is find(word.cbegin(),word.cend(),x) ; returns the second argument if the search fails, otherwise returns the first iterator equal to the given value

②The accumulate algorithm accepts three parameters, the first two indicate the range of elements to be summed (same as the previous function), and the third parameter is the initial value of the sum (note that this initial value is generally 0). Another point to note is that the third parameter must correspond to the elements in the container, that is, the inside is a string, the third parameter is also a string, an integer, and the parameter is also an integer.

③The equal algorithm accepts three parameters and is used to determine whether the two sequences hold the same value. It compares the corresponding elements of the first sequence with the corresponding elements of the second sequence, and returns true if they are equal, and false if they are not equal. You can use Different values ​​from different containers, its format is: equal(word1.cbegin(), word1.cend(), word2.cbegin(). But there is a premise that needs to be noted, the two sequences must be the same length.

3. Let's introduce the algorithm for writing containers (assign new values ​​to elements in the sequence)

, but only if the container has room to write elements)

① The find algorithm accepts three parameters, a pair of iterators to represent a range, and a value as the third parameter. The algorithm assigns the third parameter to each element in the range, in the format fill(vec.begin (),vec.end(),0).

The fill_n algorithm accepts a single iterator, a count value, and a value, and assigns the given value to the specified number of elements starting from the element pointed to by the iterator. The format is: fill_n(vec.begin(),vec.end( ),0), resets all elements to 0 (but note that the container cannot be empty)

③Introduce back_insert (an iterator that adds elements to the container, in the header file iterator )

back_inserter accepts a reference to the container and returns an insert iterator bound to the container. When we assign a value through this iterator, it will call push_back to add a fixed value to the container. E.g:

vector<int>vec;
auto it=back_inserter(vec);
*it=42;

④Copy algorithm

The copy algorithm accepts three iterators, the first two represent an input range, and the third represents the starting position of the destination sequence. This algorithm copies the elements of the input range of the first two parameters from the starting position of the third parameter. The destination sequence passed to copy must contain at least as many elements as the input sequence!

inta[]={0,1,2,3,4,5,6,7,8,9};
int a2[sizeof(a1)/sizeof(*a1)]//a2 is the same size as a1
auto ret = copy (begin (a1), end (a1), a2);
//ret points to the position after the tail element copied to a2

4. Algorithm for rearranging elements

①The sort algorithm accepts a pair of iterators and rearranges the order in the sequence according to the size or the order in the dictionary.

sort(word.begin(),word.end());

The unique standard library algorithm accepts a pair of iterators, rearranges the elements so that the unique elements appear at the beginning of the sequence, and returns an iterator pointing to the last position of the unique position.

auto  end_unique = unique(word.begin(),word.end());

erase accepts a pair of iterators and deletes the elements within the scope of the iterator, which is an operation on the container.

erase(end_unique,word.end());







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324525266&siteId=291194637