Access to vector<list<string>>?

Hi,

In my progressions of learning C++, i came up to a problem when using push_front to lists (of strings), in a vector. One way is to do something like this:
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
vector<list<string>> arrayToList()
{
	string line;
	list<string> one, two, three;
	string someStrings[] = {"a", "ab", "abc", "abcd"};
	for (int i=0; i < sizeof(someStrings)/32; i++)
	{
		switch (someStrings[i].size())
		{
		case 1:
			one.push_front(someStrings[i]);
			break;
		case 2:
			two.push_front(someStrings[i]);
			break;
		case 3:
			three.push_front(someStrings[i]);
			break;
		default:
			break;
		}
	}

	vector<list<string>> returnVector;
	returnVector.push_back(one);
	returnVector.push_back(two);
	returnVector.push_back(three);

	return returnVector;
}


This ugly and not suitable for arbitrary string lengths. I tried:
1
2
vector<list<string>> returnVector[3];
returnVector[0].push_front(someStrings[0]);

...but this was not allowed. How do I access the lists in the vector directly?
Thanks in advance!
Last edited on
I'm quite sure you need to put a space between the two brockets. I think C++ gets confused and thinks you mean the >> operator if you don't.
1
2
3
vector<list<string>> returnVector;
// should be
vector<list<string> > returnVector;

In your snippet at the bottom: IIRC vectors do not have a push_front() method. returnVector is an array of vectors of lists of strings, and the [0] gets a specific vector.
@Catfish: Thank you! That could also spare me some silly bug tracking.

@Zhuge: OK, thanks.

I mainly wonders though, if there is any way to access the lists in the vectors directly; if i change my last snippet to:
1
2
vector< list<string> > returnVector;
returnVector[0].push_front(someStrings[0]);


My intention is: Go to the 1st element in returnVector (which is a list) and push_front() the 1st element in someStrings. Possible?
Looks alright to me... Does it not work? Give us the errors.
Hm, nevermind - that actually worked as expected. I now battle some out-of-range problems, but I think I can manage that one way or another.
Thanks for your help!
Topic archived. No new replies allowed.