0

したがって、基本的に私のプログラムは、単語のデータ ストリームを読み取り、各単語の出現回数と一意の単語の総数を数えます。それらをマップに読み込みます。私のプログラムは、1 つの問題を除いて完全に動作します... を呼び出すとp.print()、 の値totalは 0 のままです。決してインクリメントされません...私の印刷機能は次のように定義されています:

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

1 に答える 1

0

それを考え出した...私は持っている必要があります

p = for_each(aMap.begin(), aMap.end(), p);

自分の問題を理解するのに勝るものはありません。

于 2013-09-26T00:38:31.830 に答える