C++ 使用map存储有序数据时出现分段错误(堆芯转储)

C++ 使用map存储有序数据时出现分段错误(堆芯转储),c++,map,type-conversion,C++,Map,Type Conversion,发现许多有此错误的帖子,但没有一篇有帮助。。 我正在调用一个函数来保存从交易地图读取客户发行的书籍,然后从书籍详细信息地图读取书籍价格,并将数据保存到customerBillList地图,其中bill乘以-1作为键,以便按降序排列(升序为负)。。。但是这个函数给了我上面的错误。。 功能是 void getCustomerBills(map<string, string> transactions,map<string, string> bookDetails, map&l

发现许多有此错误的帖子,但没有一篇有帮助。。 我正在调用一个函数来保存从交易地图读取客户发行的书籍,然后从书籍详细信息地图读取书籍价格,并将数据保存到customerBillList地图,其中bill乘以-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;
         }
 }
书籍详情:

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);
map::迭代器迭代器bookDetails=bookDetails.find(book);

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

可能会告诉我们错误发生在哪一行代码中分段错误没有给出行号,我正在使用联机编译器,因此无法真正使用断点。。如果我知道我可能已经解决了这个问题(这就是为什么我们有debuggers@Pundit顺便说一句,多重映射呢?@laune我真的不明白这会有什么不同。你能详细说明一下吗?@Pundit客户ID值的串联,用逗号分隔,不是好的编程风格,除非你真的需要“C47,C567,C99”如果其他映射具有结构化的值部分而不是字符串,并且事务作为多重映射(或映射),那么情况可能会更好书应该是带有成员ID、标题、价格的类书。@laune yeah..这也是一个选项,但逗号分隔的列表是.list文件中的格式,如果是事务,我们需要用户的所有事务或不需要任何事务,在这种情况下,我想这是可以的..不需要?neways谢谢…:)@Pundit如果你一定要摸索CSV字符串,你可能会发现std::getline(istream&is,string&str,char-delim)比字符串上的严格迭代更容易使用。
map<string, string>::iterator iteratorBookDetails = bookDetails.find(book);