2012-06-08 129 views
0

我得到這個「地圖/套迭代器不遞增的」在Visual Studio 2010下面的代碼運行時錯誤,不能由我自己看着辦吧..它是一個平臺相關的問題?下面列出了整個代碼段的詳細信息。請幫助我,謝謝!地圖/套迭代器不遞增的

#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; 
} 
+1

太多的代碼讀取...你任何機會修改'map'你遍歷呢? –

+0

解決它..犯了一個愚蠢的錯誤 - - !謝謝! @ K-BALLO –

回答

7

這個循環遞增錯誤的迭代器:

for(itr2 = repToWord.begin(); itr2 != repToWord.end(); itr++) 
               //  ^^^^^ should be itr2 
+0

我做了什麼愚蠢的錯誤......非常感謝您認真審查! –