STL函数学习(173)

更多函数讲解在175,这里只放置大众,让人注意的函数。

std::enable_if

源代码:

template<class _Ty>
struct enable_if<true, _Ty>
{    // type is _Ty for _Test
typedef _Ty type;
};

其实它和static_assert一样,都是为了编译期间检查。常用使用在函数前,表面上给个函数返回值,但之前要进行判断。

std::find

find第三个参数是value,如果要找自定义的类,只有重载函数

template <typename T>
bool operator !=(const MyClass<T>& item, T n)
{return ;}

traits编程法

++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1template <class numT>
2: struct float_traits { };
3:
4: struct float_traits<float> {
5: typedef float float_type;
6: enum { max_exponent = FLT_MAX_EXP };
7: static inline float_type epsilon() { return FLT_EPSILON; }
8: ...
9: };
10:
11: struct float_traits<double> {
12: typedef double float_type;
13: enum { max_exponent = DBL_MAX_EXP };
14: static inline float_type epsilon() { return DBL_EPSILON; }
15: ...
16: };
现在不管啥类型,模板的使用者都可以无差别使用max_exponent了。比如这儿有个矩阵类模板:
1: template <class numT>
2: class matrix {
3: public:
4: typedef numT num_type;
5: typedef float_traits<num_type> traits_type;
6: inline num_type epsilon() { return traits_type::epsilon(); }
7: ...
8: };

typename

++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class _InputIter, class _Tp>
typename iterator_traits<_InputIter>::difference_type
count(_InputIter __first, _InputIter __last, const _Tp& __value) {
__STL_REQUIRES(_InputIter, _InputIterator);
__STL_REQUIRES(typename iterator_traits<_InputIter>::value_type,
_EqualityComparable);
__STL_REQUIRES(_Tp, _EqualityComparable);
typename iterator_traits<_InputIter>::difference_type __n = 0;
for ( ; __first != __last; ++__first)
if (*__first == __value)
++__n;
return __n;
}
//1、用在模板中,来声明是类型
//2、用在类型声明,表示下面的是个类型。不然typename T::iterator *iter就会被解析为俩数相乘。

std::bind

这个函数是必须和function一起讲的,但是多个绑定情况就有点特殊,先列出来:

++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int TestFunc(int a, char c, float f)
{
cout << a << endl;
cout << c << endl;
cout << f << endl;
return a;
}
int main()
{
auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
bindFunc1(10);
cout << "=================================\n";
auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
bindFunc2('B', 10);
cout << "=================================\n";
auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, std::placeholders::_3);
bindFunc3('C', 30, 100.1);
return 0;
}//绑定第几个placeholders就按照参数顺序输入第几个。

std::function

看到这个函数(表明是函数其实是对象),我只想说叼到没朋友!!!!!!

代码伺候:

++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
std::function< int(int)> Functional;
// 普通函数
int TestFunc(int a)
{
return a;
}
// Lambda表达式
auto lambda = [](int a)->int{ return a; };
// 仿函数(functor)
class Functor
{
public:
int operator()(int a)
{
return a;
}
};
// 1.类成员函数
// 2.类静态函数
class TestClass
{
public:
virtual int VirtualMember(int a) { return a; }
int ClassMember(int a) { return a; }
static int StaticMember(int a) { return a; }
};
//多态
class DemoClass : public TestClass {
virtual int VirtualMember(int a) { return a + 1; }
};
int main()
{
// 普通函数
Functional = TestFunc;
int result = Functional(10);
cout << "普通函数:"<< result << endl;
// Lambda表达式
Functional = lambda;
result = Functional(20);
cout << "Lambda表达式:"<< result << endl;
// 仿函数
Functor testFunctor;
Functional = testFunctor;
result = Functional(30);
cout << "仿函数:"<< result << endl;
// 类成员函数
TestClass testObj;
Functional = std::bind(&TestClass::ClassMember, testObj, std::placeholders::_1);
result = Functional(40);
cout << "类成员函数:"<< result << endl;
// 类静态函数
Functional = TestClass::StaticMember;
result = Functional(50);
cout << "类静态函数:"<< result << endl;
//多态
TestClass* p = new DemoClass;
Functional = std::bind(&TestClass::VirtualMember, p, 2);
cout <<Functional(5);//3
return 0;
}
//又一个灵活用法
map<char, function<int(int, int)>> binops =
{
{ '+', add },
{ '-', minus<int>() },
{ '*', [](int i, int j){return i - j; } },
{ '/', divide() },
{ '%', mod },
};
cout << binops['+'](10, 5) << endl;
cout << binops['-'](10, 5) << endl;
cout << binops['*'](10, 5) << endl;
cout << binops['/'](10, 5) << endl;
cout << binops['%'](10, 5) << endl;

reserve、resize、capacity、size

内容就不讲了,一一对应的!!!点下就好,vector和string都是一样的,只增长,2*n。

unary_function 一元函数,binary_function 二元函数

binder2nd和函数适配器是必须要他们的

++
1
2
3
4
5
6
7
8
9
10
11
template <class t>
struct negate : public unary_function<t, t> {
t operator() (const t& x) const {return -x;}//此const是必须的
}
template<class t>
struct plus : public binary_funcion<t, t, t> {
t operator() (constt t& x, const t& y) const {return x + y;}//此const是必须的
}

// //