Take the 2-minute tour ×

got a question on how to simplify some code in c++.

So we're given this lab in my CS course where we have to generate a large sentence based on an input file, storing the read lines in a map of a list of a list of strings (whew!). We have to search it using a recursive function.

So the map key is a string. The first list is a collection of lines, and the second list is a collection of strings that were parsed from the file.

map<string, list<list<string> > >

I need to get down to the list of strings to reconstruct it into one string. I've set up some iterators to get down to it but I'm getting a "segmentation fault 11" when it reaches my declaration of iterators.

"Grammar" is a map that has already been given input and is passed by reference.

"Incomplete" is a string equal to a key that is passed to the function.

Code snippet:

string found = "";
map<string, list<list<string> > >::iterator section = grammar.find(incomplete);
list<list<string> > listItem = section->second;
list<list<string> >::iterator lIt = listItem.begin();
advance (lIt, rand() % listItem.size());
list<string> stringItem = *lIt;
while (stringItem.empty() == false){
    found = found + " " + stringItem.front();

To explain the need of rand(): Our first list has multiple lists and we randomly choose one in order to generate the sentence. Have I used it correctly?

It stops about here. I'm sure there is a more simple way of getting down to that list but I can't seem to figure it out, and I'm guessing that I'm consuming a lot of memory to declare all these if "segmentation fault 11" is being thrown. Any suggestions?

share|improve this question
Offhand, how is your list loaded and how is is assigned to the map that contains it (i.e from what source is the key that indexes the map sourced) ? –  WhozCraig Feb 7 '13 at 19:10
The program reads an external file that is formatted. There are several brackets ("{,}") that we read and store into the list. Each bracket can have several lines. Each bracket is given a "definition" where the first line has a word encased in a less-than and greater-than sign (like HTML). We store the definition as the key, then each line is broken into a list and all the lists are stored into the greater list and paired to the key. –  user1993468 Feb 7 '13 at 19:14
The list<list<string> > was the professor's recommendation because we may deal with a very large file. I'm starting to think that it may be easier to use a vector of vectors. Not too sure though. –  user1993468 Feb 7 '13 at 19:19
add comment

1 Answer

There may be more problems, but here are two obvious ones:

  1. You don't check whether grammar.find(incomplete); actually finds an element. If the search fails, it returns grammar.end() and the rest of your can just blow up.

  2. There's a problem in how you try to find a random element:


*lIt is now the first list in listItem.


*lIt is now the second list in listItem.


This may try to advance the iterator too far. If the size of your list is n you can advance at most n-2 steps, otherwise you're overshooting the end. But rand() % n can be n-1.

share|improve this answer
Ok, I'll add in an if- statement to check if it exists. I'll also comment out the ++lIt; –  user1993468 Feb 7 '13 at 19:18
@user1993468 You should also check that listItem is not empty before using it. –  us2012 Feb 7 '13 at 19:36
I'm combing through and checking that I'm storing data into listItem. I'll add another if- statment. Thanks! –  user1993468 Feb 7 '13 at 19:38
add comment

Your Answer


