c++ - Getting a string from a map of a list of a list of strings -



c++ - Getting a string from a map of a list of a list of strings -

got question on how simplify code in c++.

so we're given lab in cs course of study have generate big sentence based on input file, storing read lines in map of list of list of strings (whew!). have search using recursive function.

so map key string. first list collection of lines, , sec list collection of strings parsed file.

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

i need downwards list of strings reconstruct 1 string. i've set iterators downwards i'm getting "segmentation fault 11" when reaches declaration of iterators.

"grammar" map has been given input , passed reference.

"incomplete" string equal key passed 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(); srand(time(null)); ++lit; advance (lit, rand() % listitem.size()); list<string> stringitem = *lit; while (stringitem.empty() == false){ found = found + " " + stringitem.front(); stringitem.pop_front(); }

to explain need of rand(): our first list has multiple lists , randomly take 1 in order generate sentence. have used correctly?

it stops here. i'm sure there more simple way of getting downwards list can't seem figure out, , i'm guessing i'm consuming lot of memory declare these if "segmentation fault 11" beingness thrown. suggestions?

there may more problems, here 2 obvious ones:

you don't check whether grammar.find(incomplete); finds element. if search fails, returns grammar.end() , rest of can blow up.

there's problem in how seek find random element:

list<list<string> >::iterator lit = listitem.begin();

*lit first list in listitem.

++lit;

*lit sec list in listitem.

advance (lit, rand() % listitem.size());

this may seek advance iterator far. if size of list n can advance @ n-2 steps, otherwise you're overshooting end. rand() % n can n-1.

c++ string list map

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -