Vector of lists

• 10-20-2005
Kirmie
Vector of lists
I'm having problems accessing a list inside a vector. Bascially, how would I add a value to a list inside a vector with push_back()? Everything I have tried so far has just given me a segmentation fault.
• 10-20-2005
7stud
Code:

```#include <iostream> #include <vector> #include <list> using namespace std; int main() {         list<int> ListA;         ListA.push_back(10);         ListA.push_back(20);         list<int> ListB;         ListB.push_back(100);         ListB.push_back(200);         vector<list<int> > myVector; //Note space between "> >"         myVector.push_back(ListA);         myVector.push_back(ListB);         cout<<myVector.back().back()<<endl;                 //Using back() to get a reference to a List:         myVector.back().push_back(300); //myVector.back() is the last List in the vector         cout<<myVector.back().back()<<endl;         //Using array notation to get a reference to a List:         myVector[1].push_back(400); //myVector[1] is the second List in the vector         cout<<myVector[1].back()<<endl;     return 0; }```
• 10-20-2005
Kirmie
This is what I have been trying lately and I cant figure out why it is getting a segmentation fault since there is a number in the list and I can get the list to work by itself. The output is just 1 then segmentation fault.

Code:

```#include <list> #include <vector> int main() {         vector<list<int>* > v;         list<int> one;         v.push_back(&one);         one.push_back(1);         cout << one.front() << endl;         cout << v[1]->front() << endl;         return 0; }```
• 10-20-2005
Daved
v[1] should be v[0]. Remember that vectors (and arrays) have zero-based indexes, so a vector with one element has only one valid index - 0.
• 10-20-2005
Kirmie
Yeah, I figured that out right before after I submitted it. Sad thing is that I have been working on that for about 2 hours now.