テストstl
2612 ワード
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
template<class _T>
inline void PRINT_ELEMENTS(const _T& coll, const char* opt_str = "")
{
typedef _T::value_type TYPE;
struct print{ void operator()(const TYPE& _it){ cout<<_it<<" "; } };
std::cout << opt_str <<endl;
print ret = for_each(coll.begin(), coll.end(), print());
cout<<endl;
}
template<class _T>
inline void INSERT_ELEMENTS(_T& coll, int first, int last)
{
for(int i=first; i<=last; ++i)
coll.insert(coll.end(), i);
}
template<class _Ty, class _Arg>
struct Memfun: public binary_function<_Ty, _Arg, void>
{
public:
Memfun(bool (_Ty::*_memfun)(_Arg)): m_memfun(_memfun){}
void operator()( _Ty _obj, _Arg& _value) const
{
// binary_function<> operator “const ”, binary_function<> ,
// ,
//_value , binary_function<> ,
((_obj.*m_memfun)(_value));
}
private:
bool (_Ty::*m_memfun)(_Arg); // the member function pointer
};
class A
{
public:
bool Show(int _value){ cout<<"A "<<_value<<" "; return true; }
bool ShowEx(int _value){ cout<<"AEx "<<_value<<" "; return true; }
private:
int k;
};
void main()
{
vector<int> coll;
typedef vector<int>::value_type TYPE;
vector<int>::iterator::_Inner_type k = NULL;
vector<int>::value_type c = 5;
INSERT_ELEMENTS(coll, -10, 5);
PRINT_ELEMENTS(coll,"coll value:");
struct IsAbsMore{ bool operator()(const TYPE& _l, const TYPE& _r){ return abs(_l) <= abs(_r); } };
cout<< "min_element ret: " << *min_element(coll.begin(), coll.end(), IsAbsMore()) <<endl;
cout<< "max_element ret: " << *max_element(coll.begin(), coll.end(), IsAbsMore()) <<endl;
A a;
for_each(coll.begin(), coll.end(), bind1st(Memfun<A, int>(&A::Show),a));
cout<<endl;
for_each(coll.begin(), coll.end(), bind1st(Memfun<A, int>(&A::ShowEx),a));
cout<<endl;
system("pause");
}