본문 바로가기
프로그래밍 기록/c++

함수 객체 for_each, functor, functormem, functorctor

by hominic 2023. 12. 9.
728x90
반응형

for_each 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print(int a)
{
	printf("%d\n",a);
}

void main()
{
	int ari[]={2,8,5,1,9};
	vector<int> vi(&ari[0],&ari[5]);

	sort(vi.begin(),vi.end());
	for_each(vi.begin(),vi.end(),print);
}
반응형

functor

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct print {
	void operator()(int a) const { 
		printf("%d\n",a); 
	}
};

void main()
{
	int ari[]={2,8,5,1,9};
	vector<int> vi(&ari[0],&ari[5]);

	sort(vi.begin(),vi.end());
	for_each(vi.begin(),vi.end(),print());
}

 

functormem

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct accum {
	int sum;
	accum() { sum=0; }
	void operator()(int a) { 
		sum+=a; 
	}
};

void main()
{
	int ari[]={2,8,5,1,9};
	vector<int> vi(&ari[0],&ari[5]);

	sort(vi.begin(),vi.end());
	accum f;
	f=for_each(vi.begin(),vi.end(),f);
	printf("총합 = %d\n",f.sum);
}
728x90

functorctor

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct print {
	string mes;
	print(string &m) : mes(m) { }
	void operator()(int a) const { 
		cout << mes;
		printf("%d\n",a); 
	}
};

void main()
{
	int ari[]={2,8,5,1,9};
	vector<int> vi(&ari[0],&ari[5]);

	sort(vi.begin(),vi.end());
	for_each(vi.begin(),vi.end(),print(string("요소값은 ")));
	for_each(vi.begin(),vi.end(),print(string("다른 메시지 ")));
}

 

 

functorpara

#include <iostream>
using namespace std;

template <typename T>
class SomeClass { };

struct print {
	void operator()(int a) const { 
		printf("%d\n",a); 
	}
};

void func(int a)
{
	printf("%d\n",a);
}

void main()
{
	SomeClass<print> s1;		// 가능
//	SomeClass<func> s2;			// 불가능
}

 

find_if

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct IsKim {
	bool operator()(string name) const {
		return (strncmp(name.c_str(),"김",2)==0);
	}
};

void main()
{
	string names[]={"김유신","이순신","성삼문","장보고","조광조",
		"신숙주","김홍도","정도전","이성계","정몽주"};
	vector<string> vs(&names[0],&names[10]);

	vector<string>::iterator it;
	it=find_if(vs.begin(),vs.end(),IsKim());
	if (it==vs.end()) {
		cout << "없다." << endl;
	} else {
		cout << *it << "이(가) 있다." << endl;
	}
}

 

plus

#include <iostream>
#include <functional>
using namespace std;

void main()
{
	int a=1,b=2;
	int c=plus<int>()(a,b);
	cout << c << endl;
}

 

 

sordesc

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

void main()
{
	string names[]={"STL","MFC","owl","html","pascal","Ada",
		"Delphi","C/C++","Python","basic"};
	vector<string> vs(&names[0],&names[10]);

	//sort(vs.begin(),vs.end());
	sort(vs.begin(),vs.end(),greater<string>());
	
	vector<string>::iterator it;
	for (it=vs.begin();it!=vs.end();it++) {
		cout << *it << endl;
	}
}

 

sortfunctor

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct compare {
	bool operator()(string a,string b) const {
		return stricmp(a.c_str(),b.c_str()) < 0;
	}
};

void main()
{
	string names[]={"STL","MFC","owl","html","pascal","Ada",
		"Delphi","C/C++","Python","basic"};
	vector<string> vs(&names[0],&names[10]);

	//sort(vs.begin(),vs.end());
	sort(vs.begin(),vs.end(),compare());
	vector<string>::iterator it;
	for (it=vs.begin();it!=vs.end();it++) {
		cout << *it << endl;
	}
}
728x90
반응형

'프로그래밍 기록 > c++' 카테고리의 다른 글

c++ 표준 라이브러리 coutradix  (0) 2023.12.13
함수 객체 dualinstance, Predicate, not2, IsMulti, bind2nd, ptr_fun, mem_fun  (0) 2023.12.09
clamp  (0) 2023.12.08
binary_search  (1) 2023.12.08
any_of  (0) 2023.12.08

댓글