0

我在visual studio 2010中使用下面的代码得到了这个“map/set iterator not incrementable”运行时错误,我自己无法弄清楚..这是一个平台相关的问题吗?下面详细列出了整个代码部分。请帮助我,谢谢!

#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
void printHighChangeables(const map<string,vector<string>>& , int );
map<string,vector<string>> computeAdjacentWords(const vector<string>& );

int main()
{

    string v1[16]={"wine","dine","fine","line","nine","pine","vine","kine","wind","wing",
        "wins","hoot","loot","soot","root","boot"};
    const vector<string> words (&v1[0], &v1[15]);

    map<string,vector<string>> adjWords = computeAdjacentWords(words);
    printHighChangeables(adjWords,3);
}

void printHighChangeables(const map<string,vector<string>>& adjWords, int minWords=3)
{
    map<string,vector<string>>::const_iterator itr;
    for(itr = adjWords.begin(); itr != adjWords.end(); itr++)
    {
        const pair<string,vector<string>>& entry = *itr;
        const vector<string> & words = entry.second;
        if(words.size() >= minWords)
        {
            cout<<entry.first<<"("<<words.size()<<"):";
            for (int i = 0; i<words.size(); i++)
                cout<<" "<<words[i];
            cout<<endl;
        }
    }
}


//computes a map in which the keys are words and values are vectors of words
//that differ in only one character from the corresponding key
//Uses an efficient algorithm that is O(NlogN) with a map
map<string,vector<string>> computeAdjacentWords(const vector<string>& words)
{
    map<string,vector<string>> adjWords;
    map<int,vector<string>> wordsByLength;
    for(int i=0; i<words.size(); i++)
        wordsByLength[words[i].length()].push_back(words[i]);
    //work on each group separately
    map<int,vector<string>>::const_iterator itr;
    for(itr=wordsByLength.begin(); itr!=wordsByLength.end();itr++)
    {
        const vector<string>& groupsWords = itr->second;
        int groupNum = itr->first;
        //work on each position in each group
        for(int i = 0; i<groupNum; i++)
        {
            //Remove a character in given position, computing representatives
            //Words with same representatives are adjacent; so populate a map
            map<string,vector<string>> repToWord;
            for(int j=0; j<groupsWords.size(); j++)
            {
                string rep = groupsWords[j];
                rep.erase(i,1);
                repToWord[rep].push_back(groupsWords[j]);
            }
            map<string,vector<string>>::const_iterator itr2;
            for(itr2 = repToWord.begin(); itr2 != repToWord.end(); itr++)
            {
                const vector<string> & clique = itr2->second;
                if(clique.size() >= 2)
                    for(int p=clique.size(); p<clique.size(); p++)
                        for(int q=p+1; q<clique.size(); q++)
                        {
                            adjWords[clique[p]].push_back(clique[q]);
                            adjWords[clique[q]].push_back(clique[p]);
                        }
            }
        }
    }
    return adjWords;
}
4

1 回答 1

7

这个循环增加了错误的迭代器:

for(itr2 = repToWord.begin(); itr2 != repToWord.end(); itr++)
                                              //       ^^^^^  should be itr2
于 2012-06-08T17:54:59.563 回答