分割故障(核心倾倒)在使用地图存储有序数据时



找到了许多带有此错误的帖子,但没有任何帮助。我正在调用一个函数来保存读取客户从交易地图发行的书籍,然后我从bookdetails地图中读取书籍价格,并将数据保存到客户列表地图上,比尔将其乘以-1作为关键,以便它们安排了按顺序下降(负顺序为负)...但是此功能给了我上述错误。该功能是

void getCustomerBills(map<string, string> transactions,map<string, string> bookDetails, map<double, string> &customerBillList)
 {
         map<string, string>::iterator transactionsIterator = transactions.begin();
         for (; transactionsIterator != transactions.end(); ++transactionsIterator)
         {
             double customerBill = 0;
             string customerID = transactionsIterator->first;
             string transactionsString = transactionsIterator->second;
             int length = transactionsString.length();
             for (int i = 0;i < length; ++i)
             {
             string book = "";
             while (transactionsString[i] != ',' and i < length)
             {
                 book = book + transactionsString[i];
                 ++i;
             }
             map<string, string>::iterator iteratorBookDetails = bookDetails.find(book);
             string bookCost = "";
             string bookDetailsString = iteratorBookDetails->second;
             for (int j = 0; j<bookDetailsString.length() and bookDetailsString[j] != ',' ; ++j)
             {
                 bookCost = bookCost + bookDetailsString[j];
             }
             customerBill = customerBill + atof(bookCost.c_str());
             }
             if (customerBillList.count((customerBill)* -1) == 0)
             customerBillList[(customerBill)* -1] = customerID;
             else 
             customerBillList[(customerBill)* -1] = customerBillList[(customerBill)* -1] + "," + customerID;
         }
 }

进行一些戳戳时,我发现每次交易迭代循环时,双重客户bill的价值都会自行改变...

这是地图,以防万一您需要它们交易:

Key    Value
    C12397 P342,P8,P563,P456
    C3452 P546,P8,P673,P675
    C1238 P1
    C12397 P8,P673,P4

书籍尾巴:

key Value
P342 500,Black Holes and Baby Universes
P8   90,Love in the time of Cholera
P675 23,Number Theory and Cryptography
P563 1000,Lord of the Rings ­ Box Set
P456 12,Da Vinci Code
P546 20,Linux Device Drivers
P673 45,The Great Indian Novel
P1   34,Predictably Irrational
P42  44,The Hitchhiker’s Guide to the Galaxy
P99  99,Problems in Physic

有人看到这里有什么问题吗?任何帮助将不胜感激。

您没有检查:

的结果
map<string, string>::iterator iteratorBookDetails = bookDetails.find(book);

我可以在您给出的示例中看到,P4中没有bookDetails中的记录。这意味着上述线将返回bookDetails.end()。稍后,当您尝试访问iteratorBookDetails指向的值时,您一定会崩溃。

最新更新