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

函数指针数组

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

这里其实可以使用typedef进行优化:

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

使用typedef后这里变得异常清晰,pfunc是一个函数指针,函数的参数是int,返回值类型是const vector
这里我们使用命令c++del验证上面的点:

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

这里由于c++decl命令不支持vector,换成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;
}

这里由于我之前踩过坑,说rhs.push_back(1)之后,后面的it可能就失效了。我们可以通过reserve验证这个问题,执行如下代码,两次输出结果就一致了:

#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

可以继续验证,我们将vector填满,导致capacity不足,触发扩容逻辑,因此迭代器失效:

#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

实现一个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;
}