1024programmer Java Functions in the algorithm header file: remove() and remove_if() functions,…

Functions in the algorithm header file: remove() and remove_if() functions,…

  • remove(first, last, value) function removes the value of [first, last) range,returns the tail iterator of the new value range

at the head The declaration in the file is as follows

template
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );//first, last are all iterators,value is a value or object,returns the last iterator of the new value range

 The usage method is as follows

vector a = {11, 0, 2, 3, 10, 0, 0, 8, 0} ;cout <<"Original size : " <

  • remove_if() function

template
ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );
//Remove all elements that meet specific criteria from the range [first, last) , and return Iterator after the end of the new value range.

Use as follows

vector a = {11, 0, 2, 3 , 10, 0, 0, 8, 0};cout <<"Original size : " <// auto itend = remove_if(a.begin(), a.end(), bind2nd(greater( ),9));//Remove numbers greater than 9 auto itend = remove_if(a.begin(), a.end(), [](int i){return i>9;});/ / You can also use lambda expressions or write a bool type function and use the function name as the third parameter. cout <<"after REMOVE, size : " <

In addition,,in the header file,there are count() function and count_if() function usage and the above resemblance. find() and find_if()

  • count(first, last, value)

Where ,first and last are iterators or pointers& #xff0c;Represents the range [first, last),value is a value or object. Returns the number of elements in [first, last) equal to value.

  • count_if(first, last, p)

Where ,first and last are iterators or pointers,representing the range [first , last),p is a unary predicate,can be a function object,returns a bool type function name(that is, a function pointer),lambda expression. Return the number of elements that satisfy p condition

  • find(first, last, value)

Returns the iterator of the first element in the range [first, last) that is equal to value. If , is not found, last is returned.

  • find_if(first, last, p)

Returns the iterator of the first element in the range [first, last) that satisfies the condition of p. If , is not found, last is returned.

p is a unary predicate , can be a function object, returns a bool type function name(that is, a function pointer&# xff09;,lambda expression.

  • replace() and replace_if()

template
void replace( ForwardIt first, ForwardIt last, const T& old_value, const T& new_value ); //Replace all old_value with new_value in [first,last) range,. Return voidtemplate
void replace_if( ForwardIt first, ForwardIt last, UnaryPredicate p, const T& new_value ); // In the [first, last) range , put all the items that satisfy p The elements of the condition are replaced with new_value. Return void

  • reverse(begin,end)

Reverses the order of the elements in the range [first ,last).Reverse the order

std::vector myvector;for (int i=1; i<10; & #43;+i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1

 

Redirect: https://www.cnblogs.com/htj10/p/9296597.html

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/functions-in-the-algorithm-header-file-remove-and-remove_if-functions/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索