局部变量/类变量增量
Local Variable / Class variable increment
所以本质上我的程序所做的是在单词的数据流中读取,并计算每个单词的出现次数,以及唯一单词的总数。它会将它们读入地图。我的程序运行良好,除了一个问题...当我调用p.print()
时,total
的值仍然为0。似乎有一些问题,在 for_each 语句中增加总数,但是当我 p.print 时,它表现得好像从未增加过......我的打印函数定义如下:
void print_words(const map<string,int> &aMap) {
PRN p(aMap.size());
for_each(aMap.begin(), aMap.end(), p);
p.print();
}
我有一个负责处理每个单词的类,这是定义:
//CLASS PRN FUNCTIONS
// constructor
PRN::PRN(const int& s, const int& c, const int& t) {
sz=s;
cnt=c;
total=t;
}
// overloaded operator, where P is defined as
// typedef pair < string, int > P;
void PRN::operator()(const P& p) {
if(cnt%NO_ITEMS == 0 && cnt != 0)
cout << 'n';
cout << setw(ITEM_W) << left << p.first << " : " << setw(NO_W) << left << p.second;
total += p.second;
cnt++;
}
// to printout final value of total
void PRN::print() const {
cout << 'n' << 'n';
cout << "no of words in input stream : " << total << endl;
cout << "no of words in output stream : " << sz << endl;
}
想
通了...我需要有
p = for_each(aMap.begin(), aMap.end(), p);
没有比弄清楚自己的问题更好的感觉了!
相关文章:
- 类与私有变量的其他类之间的线程安全性
- 使用的未初始化局部变量'Quick'
- 类和静态变量
- 修复未初始化的局部变量错误
- 如何在C++子类中访问父类的私有变量
- 局部变量保留函数中的值
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 将引用分配给局部变量,如果局部变量超出范围,它会超出范围吗?
- 从不同C++类的成员变量访问成员函数
- 跨多个类的全局变量而不会出现重定义错误?
- Gnuplot_i.hpp C++接口绘制局部变量而不是文件
- 我创建的类似乎错误地设置了它的局部变量
- 使用局部变量中的类成员类型
- 用C语言创建具有局部变量的类C++模板函数
- 在C++中如何在类方法之间访问静态局部变量的值
- 如何从另一个类调用函数,其中该函数使用局部变量
- C++类设计:成员和局部变量
- 局部变量/类变量增量
- 类局部变量的指针
- 在类范围内声明与类属性同名的局部变量