1

同じ問題を再度投稿して申し訳ありませんが、提案された解決策を使用しましたが、部分的に機能しました...問題を明確にしたい:

それで:

次のように、マップのベクトルを定義しました。

typedef vector<map<string,unsigned int> > myvec;

そして、次のようなベクトルのマップ(私はそれをインデックスと呼びました):

typedef map<string,vector<unsigned int> > index;

それから私は次のことをしました:

myCoogleと呼ばれる私のクラスで、私は宣言しましたmyvec maps_vec;

そして私はそれを地図で埋めました...

各マップには、単語(文字列)と数字(未使用の整数)があります。

ここまでは順調ですね。

私も宣言しましたindex the_index;

次に、すべての異なる単語をthe_vecからthe_indexにコピーします。

言葉は文字列になります...

そして、各ベクトルについて、マップのベクトルに格納されている数値を追加します。

例えば:

the_vecには3つのマップがあります。

1つ目は:チキン、1 | 人、1 | エレベーター、5 | is、2 | ..。

2番目は:person、2 | アイスクリーム、3 | is、3 | ..。

3番目は:エレベーター、1 | クマ、1 | is、4 | チキン、3 | ..。

したがって、the_indexは次のようになります。

単語、[intのベクトル]

チキン[1,0,3]

人、[1,2,0]

エレベーター[5,0,1]

is [2,3,4]

アイスクリーム[0,3,0]

クマ[0,0,1]

OKこれが私の関数です:

void Coogle::make_index()
{
    //SCAN THE FIRST MAP
    myvec::iterator myvec_iter;
    map<string,unsigned int>::iterator map_iter;
    index::iterator idx_iter = the_index.begin();
    for(map_iter=maps_vec[0].begin(); map_iter!=maps_vec[0].end(); ++map_iter)
    {
        the_index[map_iter->first].push_back(map_iter->second);
    }


    //SCAN THE OTHER MAPS
    myvec_iter=maps_vec.begin();
    myvec_iter++;
    int i=0; //FILE #
    while(myvec_iter!=maps_vec.end())
    {
        i++;
        for(map_iter=maps_vec[i].begin(); map_iter!=maps_vec[i].end(); ++map_iter)
        {
            string word=map_iter->first;
            cout << "DEALING WITH WORD \"" << word << "\"" << endl;
            index::iterator location;
            location=the_index.find(word);
            if(location!=the_index.end()) //if word found in the index
            {
                cout << "WORD EXISTS!" << endl;
                location->second[i]=map_iter->second;
            }
            else //if not found
            {
                cout << "WORD DOES NOT EXIST! NEW WORD." << endl;
                the_index[word].push_back(map_iter->second);
            }
        }
        cout << endl;
        ++myvec_iter;
    }
}

明確化:FILE#はマップ番号です...ファイル(* .txtファイル)を使用しています。

さて、最初のマップをスキャンした後、the_indexを印刷しようとしましたが、すべて問題ありませんでした。しかし、他のマップもスキャンした後に印刷しようとすると、これが発生します。

不具合

ただし、「正常にビルド」します。

そして、プログラムを実行すると、このウィンドウがポップアップします。

したがって、2番目の'for'ループに問題があると思います。

誰でも助けてくれますか?

非常に長い投稿をお詫びします...

どうもありがとうございます !!!

編集:the_indexを印刷しようとしない場合、プログラムはコンパイルされ、正常に実行されます。しかし、もちろんそれだけでは十分ではありません。しかし、私の印刷機能は大丈夫です...ここに:

void Coogle::print_index() const
{
    index::const_iterator iter;
    for(iter=the_index.begin();iter!=the_index.end();++iter)
    {
        cout << "Word: " << iter->first << endl;
        //cout << "Files number is: " << files_number << endl; //prints: 3
        for(int i=0; i<files_number;i++)
            cout << "file #" << i+1 << ": " << iter->second[i] << " , " << endl;
    }
}

編集:

これは、1つのマップのみを印刷する場合と3つのマップを印刷する場合のスクリーンショットです。

1マップvs3

4

2 に答える 2

0

これがC++11の答えです。

myvec v = /* ... */;

index q;

for (auto const & m : v)
    for (auto const & p : m)
        q[p->first].emplace_back(p->second);

これは@Antimonyのソリューションと同じですが、見た目は少し簡単です。

于 2012-08-17T23:25:25.037 に答える
0

編集:わかりました、うまくいけば、私はあなたがやろうとしていることをようやく理解しました。

typdef myvec::iter vmiter_t;
typdef map<string,unsigned int>::iter miter_t;
typdef set<string>::iter siter_t;

set<string> words;

for(vmiter_t vmiter=maps_vec.begin(); vmiter != maps_vec.end(); ++vmiter)
{
    for(miter_t miter = vmiter->begin(); miter != vmiter->.end(); ++miter)
    {
        words.insert(miter->first);
    }
}

for (siter_t iter=words.begin(); iter!=words.end(); ++iter){
    const string& word = *iter;
    for(vmiter_t vmiter=maps_vec.begin(); vmiter != maps_vec.end(); ++vmiter)
    {
        map<string,unsigned int>& temp = *vmiter;
        the_index[word].push_back(temp[word]);
    }
}

変数の宣言を提供しなかったので(そしてそれらのいくつかは間違った名前が付けられているように見えるので)わかりにくいですが、問題は行にあると思いますlocation->second[i]=map_iter->second;。ベクター内のインデックスiにアクセスしようとしていますが、ベクターは実際にはそれほど長くありません。

編集:あなたが実際に何をしようとしているのかがわかるようになったので、あなたは物事を非常に複雑にしている。他の要素のために個別のループはまったく必要ありません。

于 2012-08-17T22:23:17.103 に答える