디렉토리
for_each
for_each(InputIterator beg,InputIterator end ,UnaryProc op)
- 간격에 대한
[beg,end]
각 요소 호출 :op(elem)
. - 반환
op
사본을. op()
당신은 요소를 변경할 수 있습니다.- 선형 복잡성
실시 예 1, 단순한 출력
#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;
}
함수 객체 클래스의 사용이 필요, 시간의 일반 기능을 사용하여, 괄호를 추가 괄호를 필요로하지 않으며, 중장비 관련시 것을 발견했다.
실시 예 2 용기의 값을 변경
#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;
}
실시 예 3 용기의 반환 값
#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
전달 된 파라미터 값을 변경하지 않고
count(InputIterator beg,InputIterator end ,const T& value)
컴퓨팅 형태 [beg end]
요소와 동일 value
값의 수.
count_if(InputIterator beg,InputIterator end ,UnaryPredicate op)
단항 술어 결과의 형태는 true
필요 기능을 결정하는 요소의 수.
#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()
그것은 전달 된 매개 변수의 값을 변경하지 않습니다.
find(InputIterator beg,InputIterator end ,const T& value)
수익 [beg end]
요소가 같은지 value
의 첫 번째 요소의 반복자 값.
find_if(InputIterator beg,InputIterator end ,UnaryPredicate op)
단항 형태로 반환 최초의 반복자, 동등한 가치 분석, 반환 찾을 수 없습니다 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()
전달 된 매개 변수 값을 변경하지 않고, 이전의 값 N 개의 연속 일치하는 항목을 찾을 수 있습니다.
search_n(InputIterator beg,InputIterator end ,count,const T& value)
반환 [beg end]
요소는 동일하다 value
반복자 연속적인 동일 요소의 첫 번째 값.
search_n(InputIterator beg,InputIterator end ,count,value,BinaryPredicate op)
제 반복자 연속 동일한 값을 이진 동등의 형태를 돌려 분석 리턴 발견되지 않는다 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;
}