Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从多重贴图打印_C++_Printing_Multimap - Fatal编程技术网

C++ 如何从多重贴图打印

C++ 如何从多重贴图打印,c++,printing,multimap,C++,Printing,Multimap,嘿,我正试图从多重地图打印出来。我的多重地图是:多重地图 我想发生的是当我问一个问题时:问题;它以随机顺序打印出3个问题。但是到目前为止,当调用printQuestion时,我得到的只是一个运行时错误 Run time error: Debug Assertion Failed! Expression: map/set iterator may not be dereferencable 这是我的密码: #include <iostream> #include "Questio

嘿,我正试图从多重地图打印出来。我的多重地图是:
多重地图
我想发生的是当我问一个问题时:问题;它以随机顺序打印出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;
}
#包括
#包括“问题.h”
使用名称空间std;
问题::问题()
{
}
问题::问题(字符串问题、字符串更正答案、字符串错误答案1、字符串错误答案2、字符串错误答案3)
{
这个->问题=问题;
此->pAnswers=新字符串[4];
此->pAnswers[0]=错误答案1;
这->pAnswers[1]=错误答案2;
这->pAnswers[2]=错误答案3;
此->pAnswers[3]=正确答案;
这个->洗牌(这个->潘斯沃斯,4);
此->正确答案=正确答案;
}
无效问题::洗牌(字符串*数组,整数n)
{
随机洗牌(&this->pAnswers[0],&this->pAnswers[4]);
}
字符串问题::getQuestion()
{
返回此->问题;
}
字符串问题::getCorrectAnswer()
{
返回此->正确答案;
}
字符串*问题::getAnswers()
{
返回此->pAnswers;
}
bool问题::检查答案(字符串答案)
{
如果(此->正确答案。比较(答案)==0)
{
返回true;
}
返回false;
}
void Questions::questionStore()
{
问题*q1=新问题(“世界上已知最古老的城市是什么?”、“斯巴达”、“的黎波里”、“罗马”、“德马斯库斯”);
问题*q2=新问题(“奥运会允许哪些运动项目留胡子?”、“柔道”、“乒乓球”、“排球”、“拳击”);
问题*q3=新问题(“昆虫学家研究什么?”、“人”、“岩石”、“植物”、“昆虫”);
问题*q4=新问题(“牛仔会在哪里穿牛仔裤?”、“帽子”、“脚”、“胳膊”、“腿”);
问题*q5=新问题(“这些星座中,哪一个代表不长角的动物?”、“白羊座”、“金牛座”、“摩羯座”、“水瓶座”);
问题*q6=新问题(“前首相托尼·布莱尔出生在哪个国家?”、“北爱尔兰”、“威尔士”、“英格兰”、“苏格兰”);
问题*q7=新问题(“粗呢大衣是以哪个国家的城镇命名的?”、“奥地利”、“荷兰”、“德国”、“比利时”);
问题*q8=新问题(“哪种生物的幼体被称为雏鸽?”、“马”、“鱿鱼”、“章鱼”、“鸽子”);
问题*q9=新问题(“2000年电影《角斗士》中的主角在竞技场上与什么动物搏斗?”、“黑豹”、“豹子”、“狮子”、“老虎”);
补充问题(1,q1);
补充问题(1,q2);
补充问题(1,第三季度);
补充问题(2,q4);
补充问题(2,问题5);
补充问题(2,问题6);
补充问题(3,问题7);
补充问题(3,问题8);
补充问题(3,问题9);
}
无效问题::添加问题(整数级,问题*问题)
{
地图插入(成对(水平,问题));
}
问题*问题::打印问题(整数级)
{
迭代器iter;
双便秘;
对于(multimap>::const_iterator iter=map.begin();
iter!=map.end();++iter)
cout first当你说你得到一个“运行时错误”时,我假设函数打印和整数以及指针的地址。你可能是想在打印之前取消对问题指针的引用:

*it->second
另一个问题是,当返回
iter->second
时,取消对结束迭代器的过去的引用:
return
语句前面的循环在
iter==map.end()
时退出。取消对结束迭代器的过去的引用是非法的,并且会导致未定义的行为(这是实际断言的错误)


当然,您应该首先存储
Question
对象,然后将它们分配到堆上:它们可能会泄漏。在“Question*s”中存储“Question”似乎也不太合适对象:您可能需要将类分开:一个用于单个问题,另一个用于收集多个问题。

是的,让我在中编辑它。