如何从多地图打印
How to print from a multimap
嘿,我试图从多地图打印出来。 我的多重地图是:multimap<int,Questions*> map;
我想发生的是当我提出问题时:questions.printQuestion(1); 它以随机顺序打印出 3 个问题。 但是到目前为止,当调用 printQuestion 时,我得到的只是一个运行时错误。
Run time error:
Debug Assertion Failed!
Expression: map/set iterator may not be dereferencable
这是我的代码:
#include <iostream>
#include "Questions.h"
using namespace std;
Questions :: Questions()
{
}
Questions::Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3)
{
this->question = question;
this->pAnswers = new string[4];
this->pAnswers[0]=wrongAnswer1;
this->pAnswers[1]=wrongAnswer2;
this->pAnswers[2]=wrongAnswer3;
this->pAnswers[3] =correctAnswer;
this->shuffle(this->pAnswers,4);
this->correctAnswer = correctAnswer;
}
void Questions::shuffle(string *array, int n)
{
random_shuffle(&this->pAnswers[0],&this->pAnswers[4]);
}
string Questions::getQuestion()
{
return this->question;
}
string Questions::getCorrectAnswer()
{
return this->correctAnswer;
}
string* Questions::getAnswers()
{
return this->pAnswers;
}
bool Questions::checkAnswer(string answer)
{
if(this->correctAnswer.compare(answer)==0)
{
return true;
}
return false;
}
void Questions::questionStore()
{
Questions *q1 = new Questions("Whats the oldest known city in the world?", "Sparta" , "Tripoli" , "Rome", "Demascus");
Questions *q2 = new Questions("What sport in the olympics are beards dissallowed?", "Judo", "Table Tennis" , "Volleyball", "Boxing");
Questions *q3 = new Questions("What does an entomologist study?", "People" , "Rocks" , "Plants", "Insects");
Questions *q4 = new Questions("Where would a cowboy wear his chaps?", "Hat" , "Feet" , "Arms", "Legs");
Questions *q5 = new Questions("which of these zodiac signs is represented as an animal that does not grow horns?", "Aries" , "Tauris" , "Capricorn", "Aquarius");
Questions *q6 = new Questions("Former Prime Minister Tony Blair was born in which country?", "Northern Ireland" , "Wales" , "England", "Scotland");
Questions *q7 = new Questions("Duffle coats are named after a town in which country?", "Austria" , "Holland" , "Germany", "Belgium");
Questions *q8 = new Questions("The young of which creature is known as a squab?", "Horse" , "Squid" , "Octopus", "Pigeon");
Questions *q9 = new Questions("The main character in the 2000 movie ""Gladiator"" fights what animal in the arena?", "Panther" , "Leopard" , "Lion", "Tiger");
addQuestion(1,q1);
addQuestion(1,q2);
addQuestion(1,q3);
addQuestion(2,q4);
addQuestion(2,q5);
addQuestion(2,q6);
addQuestion(3,q7);
addQuestion(3,q8);
addQuestion(3,q9);
}
void Questions::addQuestion(int level, Questions *question)
{
map.insert(pair<int,Questions*>(level,question));
}
Questions* Questions::printQuestion(int level)
{
multimap<int, Questions*>::iterator iter;
pair<multimap<int, Questions*>::iterator,multimap<int, Questions*>::iterator> constIter;
for (multimap< int, Questions*, less< int > >::const_iterator iter =map.begin();
iter != map.end(); ++iter )
cout << iter->first << 't' << iter->second << 'n';
/*constIter = map.equal_range(level);
size_t sz = distance(constIter.first, ret.second);
size_t idx = rand();
if(ret.first != ret.second)
advance(ret.first, idx);
it =ret.first;
Questions* question = (*it).second;
return (*it).second;
cout << question->getQuestion() << std::endl;*/
return iter->second;
}
任何人都可以帮我解决这个问题。
当你说你得到一个"运行时错误"时,我假设函数打印和整数以及指针的地址。您可能打算在打印问题之前取消引用指向问题的指针:
*it->second
另一个问题是,当你返回 iter->second
时,你取消引用了过去结束的迭代器:return
语句前面的循环在iter == map.end()
时退出。取消引用过去的结束迭代器是非法的,并导致未定义的行为(这是实际断言的错误)。
当然,您可能应该首先存储Question
对象,而不是将它们分配给堆上:它们可能会泄漏。在"Question*s*"对象中有一个"问题"存储似乎也不是很对:你可能希望必须分开类:一个用于单个问题,另一个用于收集多个问题。
相关文章:
- 如何使用 std::variant 打印地图键/值?
- 将对象放入地图后打印对象
- 如何打印我在地图中使用的数组?
- 为什么当我尝试打印地图内容时,我得到的是地址而不是 C++ 中的值?
- 通过迭代器打印出地图的值
- 如何打印属于地图的一系列元素(std::p air<size_t,std::string>)(不是所有元素)
- 在向量内打印地图
- 通过地图和打印内容迭代时,倒出输出
- 我想反转地图的值并使用基于范围的 for 循环打印它
- 打印出使用地图实现的Trie中的所有单词
- 为什么在地图中打印第二个元素
- GDB打印std ::地图元素
- 如何从多地图打印
- 如何打印包含集合的地图的内容
- C++ 如何打印此地图 std::map<int, pair<vector<pair<int, int>>, int>>
- 如何在 C++ 中打印通用地图
- 如何在控制台上将三张地图一起打印成三个不同的列?C++
- 如何打印地图的索引
- 地图的内容没有以特定的方式打印出来
- 地图不是在C++中用作数组样式打印吗?