How do I access this piece of data?

I have a masterList made of pointers to lists of strings, e.g.
master list
-list 1 (string1, string2, string3)
-list 2 (string1)
-list 3 (string1, string2)

etc.

I need to access each individual string and store it in a data structure (such as a tree) but I can't think of the syntax to access it from my main program.

Here is the code I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	list<list<string>> masterList;
	list<list<string>>::iterator mListIt;


	int size = masterList.size();
	multiset<string> stringMultiset; //build a sorted data structure for each string

	for (mListIt = masterList.begin(); mListIt != masterList.end(); mListIt++)
	{
		for (each string in the current list)  ???
			stringMultiset.insert(the current string); ???
	}
	

	return 0;
}


So in that I am trying to visit each item in my masterList (each list of strings) and while at each list of strings I need to add each string value to my data structure.

If you are confused or need clarification please ask! I'm sorry if my question is confusing.

Here is how my masterList was populated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
if (line.length() != 0 && (is_all_spaces(line) == false)) 
	masterList.push_back(*function(line)); 

list<string>* function(string myString) 
{
	list<string> *aList = new list<string>; //creates a new list object for the words

	myString.length();
	string delimiter = " ";
	int current = 0; //initialize current after all leading whitespace...

	while (myString[current] == ' ')
		++current;

	int next = current - 1; //if starting c == 10 then next == 9

	
	 //parse through line (find beginning and end of each word in line) 
	 //extract out each of the words in the phrase
	 //insert each word into the list

	do
	{
		while (myString[current] == ' ')
			++current;
		next = current - 1;

		current = next + 1;
		next = myString.find_first_of( delimiter, current ); 
		aList->push_back(myString.substr( current, next - current )); 
		current = myString.find_first_of( delimiter, current );
	}
	while (next != string::npos); //do while not at end of the string

	return aList; //pass back the pointer to the new list object
}
Last edited on
I never really used a list inside a list before and instead use 2D vectors instead but I spent a few minutes looking up lists and this might be pretty close to what you want.

I haven't tested it though.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	list<list<string> > masterList;
	
	typedef list<string>::iterator strListIt;
	typedef list<list<string> >::iterator listOfStrListIt;
	
	//int size = masterList.size();  this is not needed, just use masterList.size() on the spot
	multiset<string> stringMultiset; 

	for (listOfStrListIt i = masterList.begin(); i != masterList.end(); i++)
	{
		for (strListIt j = i->begin(); j != i->end(); j++)  
			stringMultiset.insert(*j);
	}
	

	return 0;
}
Use a double for loop, like in a two dimensional array:
1
2
3
4
5
for (mListIt = masterList.begin(); mListIt != masterList.end(); mListIt++)
	{
		for (list<string>::iterator i = (*mListIt).begin();i != (*mListIt).end() ;++i)  ???
			stringMultiset.insert(*i);
	}

Note how each item mListIt points to is another list in itself. So we simply create a second iterator and loop through these sub-lists.
Also, this:
1
2
3
list<list<string>> masterList;
//Should have a space between the two '>'s:
list<list<string> > masterList;

The ">>" could be interpreted as an extraction operator without the space.
Thank you to both of you for your replies!

I implemented a version of blacksheep's code with -> notation and it worked perfectly (stepping through in debug mode it actually filled the set with everything)!

Also, thank you for the note on having a space so as to avoid confusion of > > with >>. Great catch!
Good to hear.

Just remember, all STL containers have their own .size() method so there is never really a reason to capture it's value because of the dynamic nature of these containers.

Topic archived. No new replies allowed.