The multimap contains multimap<int,Questions*> map;. Im trying to get the map printing out using the following method:
Questions* Questions::printQuestion(int level)
{
multimap<int, Questions*>::iterator it;
pair<multimap<int, Questions*>::iterator,multimap<int, Questions*>::iterator> ret;
ret = map.equal_range(level);
if(ret.first != ret.second)
{
size_t sz = distance(ret.first, ret.second);
size_t idx = rand() % sz;
advance(ret.first, idx);
it =ret.first;
return (*it).second;
}
else
{
return NULL;
}
}
But the method does nothing. Can anybody help me with this?
This function doesn’t print anything. It returns a
Questions*that is randomly chosen from all questions with the given level. So if there are three questions with level 5, and you callprintQuestion(5), you will get a pointer to a random question of those three. If you want to print the question that is chosen in this function, you will have to do something like so:Replace
getText()with whatever you need to do to print the question.If you want the client that calls the function to print the question, you’ll do:
By the way, it’s not very common to name classes with the plural form. It’ll be easier to read your code if you class is called
Questioninstead ofQuestions.