Generic types recently used in STLAlgorithmRemove_if: I read msdn several times and did not understand the true meaning of this function. Well, my English teacher taught sports --! Later, I checked the STL source code. I have discovered the mysteries. Here I want to take a note to help myself better. Let's take a look at the msdn example below.
int greater6 (INT value) {return value> 6;} int _ tmain (INT argc, _ tchar * argv []) {int array [12] = {, 9, 2,}; vector
V1; vector
:: iterator iter1, iter2, new_end; for (INT I = 0; I <12; I ++) v1.push _ back (array [I]); cout <"vector V1 is \ n ("; for (iter1 = v1.begin (); iter1! = V1.end (); iter1 ++) cout <* iter1 <"; cout <"). "
Remove_if has three parameters. The first two parameters are the iterator intervals to be operated, and parameter 3 is a function pointer, which serves as a condition for selecting which elements to be removed. View the source code of remove_if and find out that it first calls a find_if algorithm to find the first element that meets the removal condition and returns the iterator for this element. then, call the unchecked_remove_copy_if algorithm function. The function is similar to remove_copy_if. In fact, remove_copy_if is also called internally.
The following is a pseudoCode:
Remove_if (itr_begin, itr_end, funptr) {itr_find = find_if (itr_begin, itr_end, funptr); itr_begin = itr_find; then (++ else, itr_end, itr_begin, funptr );}
The pseudocode Implementation of remove_copy_if is given below
Remove_copy_if (_ first, _ last, _ DEST, funptr) {for (; _ first! = _ Last; ++ _ First) {If (! Funptr (* _ First) * _ DEST ++ = * _ first;} return (_ DEST );}
From the implementation of the source code, we can see that from the first element that meets the removal conditions, we start to traverse the tail of the container, if an element does not meet the removal condition, replace it with the first element that meets the condition.
The following is the result of every loop of remove_copy_if in the sample code:
(It seems that the layout is a bit problematic. When the first loop is executed, iterator d points to element 7, and f points to element 9)
11, 7, 9, 2, 0, 7, 7, 3, 4, 6, 8, 5 end D F21, 2, 9, 2, 0, 7, 7, 3, 4, 6, 8, 5 end D f31, 2, 0, 2, 0, 7, 7, 3, 4, 6, 8, 5 end D f41, 2, 0, 2, 0, 7, 7, 3, 4, 6, 8, 5 end D f51, 2, 0, 2, 0, 7, 7, 3, 4, 6, 8, 5 end D f61, 2, 0, 2, 0, 7, 7, 3, 4, 6, 8, 5 end D f 71, 2, 0, 3, 0, 7, 7, 3, 4, 6, 8, 5 end D f81, 2, 0, 3, 4, 7, 7, 3, 4, 6, 8, 5 end D f91, 2, 0, 3, 4, 6, 7, 3, 4, 6, 8, 5 end D f101, 2, 0, 3, 4, 6, 7, 3, 4, 6, 8, 5 end D f111, 2, 0, 3, 4, 6, 5, 3, 4, 6, 8, 5 end d F
The 11-round traversal ends. Now the elements in container V1 are the element sequence in step 1. The elements after 1, 2, 0, 3, 4, 6, and 5 are not viewed by me.Source codeIf you know the source code, you will know where the elements (3, 4, 6, 8, 5) come from. Remove_if does not change the size of the container. Its return value is an iterator that points to the first element in all elements that meet the removal conditions. In this case, it is the second 3, remove_if is usually used together with the erase method of the container. Therefore, after I call the v1.erase (newend, v1.end) method, the remaining elements in V1 are arranged as follows: 1, 2, 0, 3, 4, 6, 5.
Functions similar to the remove_if algorithm include:
The third parameter of the Remove Method is a constant, indicating that the specified constant value is removed.
The remove_copy_if method can pass the removed result to another container using the third parameter. Of course, this container must be large enough to accommodate the next element. Otherwise, the consequence is serious.