table of Contents
for_each
for_each(InputIterator beg,InputIterator end ,UnaryProc op)
- For the interval
[beg,end]
of each element call:op(elem)
. - Returns
op
a copy. op()
You can change elements.- Linear complexity
Example 1, a simple output
#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;
void print(int elem)
{
cout << elem << "(普_调用)";
return;
}
class Print {
public:
void operator()(int i) {
cout << i << "(class调用)";
}
};
int main()
{
vector<int> num(10, 1);
for_each(num.begin(),num.end(),print);
cout << endl;
for_each(num.begin(), num.end(), Print());
return 0;
}
Found that the use of function object class when the need to add brackets, using ordinary function of time, does not require parentheses, and heavy-duty related.
Example 2, changing the value of the container
#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;
void Correct(int &elem)
{
elem += 100;
return;
}
class Print {
public:
void operator()(int i) {
cout << i << "(class调用)";
}
};
int main()
{
vector<int> num(10, 1);
for_each(num.begin(),num.end(), Correct);
cout << endl;
for_each(num.begin(), num.end(), Print());
return 0;
}
Example 3, the return value of the container
#include<iostream>
#include<algorithm>
#include <vector>
#include <iterator>
using namespace std;
class Correct {
private:
int sum;
public:
Correct(int _sum):sum(_sum){ }
void operator()(int num) {
sum += num;
}
operator int(){
return sum;
}
};
int main()
{
vector<int> num(10, 1);
int pos = for_each(num.begin(),num.end(), Correct(0));
cout << pos << endl;
copy(num.begin(), num.end(), ostream_iterator<int>(cout," "));
return 0;
}
count() and count_if
Without changing the parameter value passed in
count(InputIterator beg,InputIterator end ,const T& value)
the form of the computing [beg end]
elements is equal to value
the number of values.
count_if(InputIterator beg,InputIterator end ,UnaryPredicate op)
The form of unary predicates result is true
the number of elements that need to determine the function.
#include<iostream>
#include<algorithm>
#include <vector>
#include <iterator>
using namespace std;
bool isEven(int elem) {
return elem % 2 == 0;
}
int main()
{
vector<int> num{1,2,3,4,5,6,7,8,9,0,10};
cout << "equal 3 :" << count(num.begin(), num.end(), 3) << endl;
cout << "even : "<<count_if(num.begin(), num.end(), isEven) << endl;
cout << "原序列:" << endl;
copy(num.begin(), num.end(), ostream_iterator<int>(cout," "));
return 0;
}
find() and find_if()
It does not change the value of the parameter passed.
find(InputIterator beg,InputIterator end ,const T& value)
The Returns [beg end]
the element is equal to value
the iterator value of the first element of.
find_if(InputIterator beg,InputIterator end ,UnaryPredicate op)
Analyzing in unary form returns the first iterator, an equivalent value, is not found is returned end()
.
#include<iostream>
#include<algorithm>
#include <vector>
#include <iterator>
using namespace std;
bool isEven(int elem) {
return elem % 2 == 0;
}
int main()
{
vector<int> num{1,2,3,4,5,6,7,8,9,0,10};
cout << "equal 3 :" << *find(num.begin(), num.end(), 3) << endl;
cout << "even : "<<*find_if(num.begin(), num.end(), isEven) << endl;
cout << "原序列:" << endl;
copy(num.begin(), num.end(), ostream_iterator<int>(cout," "));
return 0;
}
search_n()
Find the value of n consecutive matches before, does not change the passed parameter value.
search_n(InputIterator beg,InputIterator end ,count,const T& value)
Returns the [beg end]
elements is equal to value
the first value of the iterator consecutive equal elements.
search_n(InputIterator beg,InputIterator end ,count,value,BinaryPredicate op)
Analyzing the form of a binary equivalent thereto returns the first iterator consecutive equal values, is not found is returned end()
.
#include<iostream>
#include<algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> num{1,2,3,4,5,6,7,8,9,0,0,10};
cout << "equal 3 :" << *search_n(num.begin(), num.end(),2, 3) << endl;
cout << "even : "<<*search_n(num.begin(), num.end(),2,6,greater<int>() )<< endl;
cout << "原序列:" << endl;
copy(num.begin(), num.end(), ostream_iterator<int>(cout," "));
return 0;
}