0

概要: 文字列 (名前) のリストを反復処理して、各文字列の最後の文字を見つけるコードがあります。また、構造体を値の型として受け取る myGraph typedef マップもあります。この構造体には、ベクトル nodenext とベクトル next_cnt が含まれています。

やるべきこと: 新しい文字がマップに挿入されるたびに、ベクター nextwt_vec を空のベクターに初期化する必要があります。

問題: 以下のコードでは、私の nextwt_vec は前の char の古い値を保持します。

    map<char, vector<int> > nextmap;


 for (myGraph::const_iterator j = graph.begin(); j != graph.end(); ++j)
 {
    vector<int> nextwt_vec;
    //populating next map with char and weighted ints
    for (int p=0; p< (int) (*j).second->nodenext.size(); ++p)
    {

        char cn = name[name.length() - 1];

        int wt = (*j).second->next_cnt[p];

        nextwt_vec.insert(nextwt_vec.begin()+p, wt);

        //puts char as key and weighted int as value in nextmap
        n->nextmap[cn] = nextwt_vec;

    }

出力: 私が得るもの:

char: A   vec: 109 
char: C   vec: 109 vec: 48

私が得るべき出力:

char: A   vec: 109
char: C   vec: 48

ご協力いただきありがとうございます!!

4

1 に答える 1

0

ベクトル nextwt_vec をドロップします。関数からの変数。nextmap[cn] を直接操作します。次の 2 行を置き換えます。

    nextwt_vec.insert(nextwt_vec.begin()+p, wt);

    //puts char as key and weighted int as value in nextmap
    n->nextmap[cn] = nextwt_vec;

これとともに:

    //puts char as key and weighted int as value in nextmap
    n->nextmap[cn].insert(nextwt_vec.begin()+p, wt);;
于 2012-10-10T23:54:19.407 に答える