Rmove prototype:
STD: Remove
template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
Find the location of the first element, then traverse the container from this location, move the following elements forward in sequence, skip the elements with the same value as the value, that is, all elements with the same value as the value will be overwritten, and other elements will be moved forward in turn.
The returned value is a new theoretically supertail iterator, but it is not a real supertail iterator, because the remove function does not change the container size. After the elements are moved, the final elements are retained.
The behavior is similar to the following:
template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val){ ForwardIterator result = first; while (first!=last) { if (!(*first == val)) { *result = move(*first); ++result; } ++first; } return result;}A simple example:
# Include <iostream> # include <algorithm> # include <vector> using namespace STD; void remove2 () {vector <int> VI {1, 2, 3, 4, 5, 6 }; cout <"Vi ="; for (int I: VI) cout <I <""; cout <Endl; cout <"vi. size () = "<VI. size () <Endl; Auto it = remove (VI. begin (), Vi. end (), 5); cout <"after auto it = remove (VI. begin (), Vi. end (), 5); "<Endl; cout <" for (Auto it3 = VI. begin (); it3! = It; ++ it3) "<Endl; cout <" Vi = "; for (Auto it3 = VI. Begin (); it3! = It; ++ it3) cout <* it3 <""; cout <Endl; cout <"for (int I: VI) "<Endl; cout <" Vi = "; for (int I: VI) cout <I <" "; cout <Endl; cout <" vi. size () = "<VI. size () <Endl; If (IT = VI. end () cout <"It = new VI. end () "<Endl; else cout <" it is a theoretical VI. end (), but not actually VI. end () "<Endl ;}Run:
You can see, it! = VI. End (), just a theoretical supertail iterator.
The following content comes from: http://blog.csdn.net/wangwenwen/article/details/7583986
**************************************** **************************************** ***********
Cla32: If you really want to delete something, connect to erase after the remove algorithm.
Only the container member function can remove the container elements: If you want to delete the items, you should connect erase after removing them.
The elements of erase are easy to recognize. They are elements of the original range that continue from the "new logical endpoint" of the interval to the true endpoint of the interval. To remove those elements, all you need to do is to use the two iterators to call the erase interval form (see article 5 ). Because remove itself easily returns the iterator of the new logical endpoint of the interval, this call is straightforward:
Vector <int> V; // as before v. Erase (remove (V. Begin (), V. End (), 99), V. End ());//TrueDelete all the cout elements <v. Size (); // now, 7 is returned.
It is common to pass the return value of remove as the first real parameter in erase format. In fact, remove and erase are close alliances, and these two are integrated into the List member function remove. This is the only function in STL named remove that can remove elements from the container:
List <int> Li; // create a list // put some values into Li. remove (99); // remove all elements equal to 99: // Delete the elements, // so its size may change.
Frankly speaking, calling this remove function is a contradiction in STL. Similar functions in the associated container are called erase, And the remove of list can also be called erase. But it does not, so we must get used to it. The world we are in is not the best in all possibilities, but is where we are. (Additionally, cla44 states that for list, calling the remove member function is more efficient than applying erase-Remove .)
Once you know that remove cannot be "true", it is taken for granted to use it together with erase. The only thing you need to remember is that remove is not the only algorithm in this case. There are also two algorithms similar to remove: remove_if and unique.
The similarity between remove and remove_if is straightforward. So I won't elaborate on it, but the unique behavior is also like remove. It is used to delete items (adjacent duplicate values) from a range without accessing the container holding the range element. If you really want to delete elements from the container, you must call unique and erase in pairs. Unique is similar to remove in the list. Just like list: Remove actually deletes (and is much more efficient than erase-Remove ). List: Unique also deletes adjacent duplicate values (also more efficient than erase-unique ).
---------------------------------------------------
Remove_if prototype:
STD: remove_if
template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
Remove_if is similar to remove, but the removed condition is changed to an element whose Pred return value is true.
The behavior is similar:
template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred){ ForwardIterator result = first; while (first!=last) { if (!pred(*first)) { *result = std::move(*first); ++result; } ++first; } return result;}A simple example:
# Include <iostream> # include <algorithm> # include <vector> using namespace STD; void removeif () {vector <int> VI {1, 2, 4, 5, 6 }; cout <"Vi ="; for (int I: VI) cout <I <""; cout <Endl; cout <"vi. size () = "<VI. size () <Endl; Auto it = remove_if (VI. begin (), Vi. end (), [] (int n) {return n % 2 = 0 ;}); cout <"after auto it = remove_if (VI. begin (), Vi. end (), [] (int n) {return n % 2 = 0 ;}); "<Endl; cout <" for (Auto it3 = VI. begin (); it3! = It; ++ it3) "<Endl; cout <" Vi = "; for (Auto it3 = VI. Begin (); it3! = It; ++ it3) cout <* it3 <""; cout <Endl; cout <"for (int I: VI) "<Endl; cout <" Vi = "; for (int I: VI) cout <I <" "; cout <Endl; cout <" vi. size () = "<VI. size () <Endl; If (IT = VI. end () cout <"It = new VI. end () "<Endl; else cout <" it is a theoretical VI. end (), but not actually VI. end () "<Endl ;}Run:
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: [email protected]
Yu gdut
------------------------------------------------------------------
STL algorithm rmonve, rmove_if (47)