编写n_gram分布统计程序需要注意

1.map的key如果是个自定义的数据类型,那么这个数据类型需要重载比较函数,

2.通过map的const_iterator指针得到的first也是一个const对象,而const对象只能访问const成员函数。所以要注意这个对象里面对应的成员函数的声明应该是const的。

3.这里的iter->first返回的一个const对象,而const对象只能调用const成员函数,所以get_Bgram()应声明为const的成员函数

4.const_iterator和const iterator的区别需要看以前的c++进阶文章。

5.const_iterator指针必须指向const map<>,也就是在函数形参里面必须加上const map。例如:

const map<BGRAM, int>& operator<<(ofstream outFile, const map<BGRAM, int>& bMap)
{
map<BGRAM, int>::const_iterator iter = bMap.begin();
for(; iter != bMap.end(); iter++)
{
outFile << iter->first.getB_gram() << " " << iter->second << endl;//这里的iter->first返回的一个const对象,而const对象只能调用const成员函数,所以get_Bgram()应声明为const的成员函数
}
return bMap;
}

posted @ 2012-08-10 00:05  springbarley  阅读(131)  评论(0)    收藏  举报