C ++ STL_6 알고리즘 (부)

디렉토리
  1. 각각()
  2. () (COUNT) 및 count_if
  3. () (찾기)와 find_if
  4. search_n ()

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;
}

게시 된 222 개 원래 기사 · 원 찬양 48 ·은 20000 +를 볼

추천

출처blog.csdn.net/qq_44116998/article/details/104593830