2016-05-12 50 views
0

在这段代码中,我必须遍历一个充满妖精的地图,它们必须按严格的顺序从i = 0,1,2进行处理, ... 但是,每当我运行我的代码时,我总是收到一个错误,指出它不可递增。我已经检查了这个错误的其他帖子,但所有的提问者都建议从头开始他们的地图,我不能从头开始重新启动它。我想知道你们中的任何一个人是否知道这件事。另外,我必须使用基于地图的数据结构,以便使用二叉搜索树或地图结构。我将不胜感激任何提示,意见,提示或帮助。map/set迭代器不可递增,我不能从头开始重新映射

#include <iostream> 
#include <chrono> 
#include <random> 
#include <map> 
#include <vector> 

using namespace std; 


class RandomNumberGenerator { 
public: 
    RandomNumberGenerator(int x) :generator(x) {}; 
// return a double between -1 and 1 
double randomBetween1and2() { 
    return (2.0*generator())/generator.max() - 1.0; 
} 
private: 
minstd_rand0 generator; 
}; 


int N; 
// Use a constant random number seed so behavior is consistent from run to run. 
int RANDOM_SEED; 

int main() 
{ 

cout << "Enter seed for random number generator: "; 
cin >> RANDOM_SEED; 
RandomNumberGenerator rng(RANDOM_SEED); 

cout << "Enter number of leprechauns: "; 
cin >> N; 

long playtime; 
cout << "Enter game play time (seconds): "; 
cin >> playtime; 
playtime = playtime * 1000; // convert to milliseconds 

double score = 0; 
int nTrapped = 0; 
// CODE FOR INITIALIZING DATA STRUCTURES GOES HERE 
multimap<int, int> DataStructure; 
int gold = 1000000; 
for (int i = 0; i < N; i++) 
{ 
    //Create N-locations based on the amount of leprechauns. 
    //Key: Location Value: What leprechaun is present. 
    double location = i * 1000; 
    DataStructure.insert(make_pair(location, i + 1)); 
} 
//Iterator to traverse around leprechauns: 

//Vector for Gold: 
//Creates N ints with default value of gold(1mil) 
vector<double>LeprechaunGold(N, gold); 

int t = 0; // keep track of number of iterations 
auto start_time0 = chrono::high_resolution_clock::now(); 
auto timeSinceStartMS = 0; 

    multimap<int, int>::iterator it = DataStructure.begin(); 
do { 
    // CODE FOR A SINGLE ITERATION GOES HERE 
    //Iteration - Traverse through the data structure: 
     int vectorIterator = 0; 
     //1 The leprechaun jumps to a new location. 
     //// You can use the random number generator like so: 
     double r = rng.randomBetween1and2(); 
     double x = 0; 
     x = x + r*gold; 
     DataStructure.insert(make_pair(x, it->second)); 
     //Delete old location. 
     DataStructure.erase(it); 
     //2 Determine if Key is inbetween pit. 
     if (x >= -1000 || x <= 1000) 
     { 
      multimap<int, int>::iterator toBeDeleted = DataStructure.find(x); 
      //If it IS between -1000 and 1000(It's in the PIT. "I fell in the pit. You fell in the pit. We all were in the pit.... THE PIT.") 
      //Delete this leprechaun AND put goldVector(of that leprechaun) to 0, and place gold into score. 
      DataStructure.erase(toBeDeleted); 
      score += LeprechaunGold[vectorIterator]; 
      LeprechaunGold[vectorIterator] = 0; 

     } 
     //3 Determine if multiple keys(multiple leprechauns in one spot) 
     //Count X. 
     if (DataStructure.count(x) >= 1) 
     { 
      //If there are greater than one, two leprechauns are occupying the same spot(same key) 
      multimap<int, int>::iterator toBeDeleted = DataStructure.find(x); 
      /*range = DataStructure.equal_range(x); 
      for (it = range.first; it != range.second; ++it) 
      { 

      }*/ 
     } 

     //4 Leprechaun steals half gold from neighbor(s) 
     //Move to next leprechaun in Goldvector: 
     vectorIterator++; 
    t++; 
    it++; 
    // code to measure run time 
    auto end_time = std::chrono::high_resolution_clock::now(); 
    auto timeSinceStart = end_time - start_time0; 
    timeSinceStartMS = chrono::duration_cast<chrono::milliseconds>(timeSinceStart).count(); 
} while (timeSinceStartMS < playtime); 

cout << "Number of iterations = " << t << endl; 
cout << "Number of trapped leprechauns = " << nTrapped << endl; 
cout << "Score = " << (long)score << endl; 
return 0; 

}

+0

你的代码有点难以通过,但它看起来像你永远不会检查你的迭代器是否已经达到最后。 – vu1p3n0x

+1

哪条线哟得到错误? – 4pie0

+2

你不能调用'map.erase(it)'然后'++ it'。擦除项目将其从地图结构中完全删除,所以执行'++'所需的信息不再可用。你必须小心,'toBeDeleted'不会与'it'一致。而是使用'erase'的返回值来获取新的位置 –

回答

1

您不应该修改你的循环内的地图,使迭代器失效。我相信,这条线是你的问题的根源:

DataStructure.erase(it); 

把所有的这些像载体的容器里面,然后循环结束后删除它们。或者,也许可以将迭代器重新分配给由擦除返回的下一个迭代器,如果您使用的是C++ 11。

it=DataStructure.erase(it);