Thread: Vector of lists

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    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.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;
    }
    Last edited by 7stud; 10-20-2005 at 05:11 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    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;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM