5月份迎来了今年最忙的一个时期，看mbp本周屏幕使用时间仅55分钟，今天难得不用跑装修相关的事情，得以坐在书桌前，随手翻几页书。

#### 函数指针数组

``````//seq_array是个数组,内放函数指针
const vector<int *> (*seq_array[])(int) = {
fibon_seq, lucase_seq, pell_seq,
triang_seq, sequare_seq, pent_seq
};``````

``````typedef const vector<unsigned int>* (*pfunc)(int);

pfunc gen_elems[] =
{ 0,
Fibonacci_gen_elems,
Pell_gen_elems,
Lucas_gen_elems,
Triangular_gen_elems,
Square_gen_elems,
Pentagonal_gen_elems
};``````

``````c++decl> explain double* (*seq_array[]) (int)
declare seq_array as array of pointer to function (int) returning pointer to double``````

#### vector迭代器使用的坑

``````#include <iostream>
#include <vector>

using namespace std;

int main()
{
int myint[10] = {1,2,3,4,5,6,7,8,9,0};
vector<int> rhs(myint,myint + sizeof(myint)/myint[0]);
vector<int>::iterator it = rhs.begin();
cout <<" *it "<< *it << endl;

rhs.push_back(1);

cout <<" *it "<< *it << endl;
return 0;
}``````

``````#include <iostream>
#include <vector>

using namespace std;

int main()
{
int myint[10] = {1,2,3,4,5,6,7,8,9,0};
vector<int> rhs(myint,myint + sizeof(myint)/myint[0]);

rhs.reserve(1024);
vector<int>::iterator it = rhs.begin();
cout <<" *it "<< *it << endl;

rhs.push_back(1);

cout <<" *it "<< *it << endl;
return 0;
}``````

`````` *it 1
*it 1``````

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

int main()
{
int myint[10] = {1,2,3,4,5,6,7,8,9,0};
vector<int> rhs(myint,myint + sizeof(myint)/myint[0]);

rhs.reserve(1024);
vector<int>::iterator it = rhs.begin();
cout <<" *it "<< *it << endl;

for(int i = 0;i < 1024;i++)
rhs.push_back(1);

cout <<" *it "<< *it << endl;
return 0;
}``````

``````[root c++]#./a.out
*it 1
*it -917709664``````

#### Essential C++ 实现一个function object

``````#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class LessThan
{
public:
LessThan(int val):_val(val){}
int comp_val()const {return _val;}
void comp_val(int nval) {_val = nval;}
bool operator()(int _value) const;
private:
int _val;
};

inline bool LessThan::operator()(int value) const { return value < _val; }

int count_less_than(const vector<int> &vec,int comp)
{
LessThan lt(comp);
int count = 0;
for(int ix = 0;ix < vec.size();++ix)
{
if(lt(vec[ix]))
++count;
}
return count;
}

void print_less_than(const vector<int> &vec,int comp,ostream &os=cout)
{
LessThan lt(comp);
std::vector<int>::const_iterator iter = vec.begin();
std::vector<int>::const_iterator it_end = vec.end();

os<<"elements less than " << lt.comp_val() << endl;
while((iter = find_if(iter,it_end,lt)) != it_end)
{
os << *iter <<' ';
++iter;
}
}
int main()
{
int ia[16]={17,12,44,9,18,45,6,14,
23,67,9,0,27,55,8,16};
vector<int> vec(ia,ia+16);

int comp_val = 20;
cout << "Number of elements less than "<< comp_val << " are "
<< count_less_than(vec,comp_val)<< endl;

print_less_than(vec,comp_val);
return 0;
}``````