nested stl containers

This is a discussion on nested stl containers within the C++ Programming forums, part of the General Programming Boards category; I have a vector that contains lists, how do I add a new element list to the vector..without creating it ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    nested stl containers

    I have a vector that contains lists, how do I add a new element list to the vector..without creating it beforehand.. i.e. , why can't I use the 'new' command like in java?

    here is my code, which 'works'
    Code:
    #include<list>
    #include<vector>
    
    using namespace std;
    
    class Graph {
    
    private:
        vector<list<int> > adjacencyList;
    public:
        Graph(int vertices)
        {
            for(int i=0;i<vertices;++i)
            {
                list<int> a;
                adjacencyList.push_back(a);
            }
        }
    };
    why doesnt the following work?

    Code:
    #include<list>
    #include<vector>
    
    using namespace std;
    
    class Graph {
    
    private:
        vector<list<int> > adjacencyList;
    public:
        Graph(int vertices)
        {
            for(int i=0;i<vertices;++i)
            {
                adjacencyList.push_back(new list<int>);
            }
        }
    };
    thanks!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    Quote Originally Posted by rodrigorules
    why can't I use the 'new' command like in java?
    C++ is not Java.

    Quote Originally Posted by rodrigorules
    here is my code, which 'works'
    In this case, the best solution is to use an appropriate constructor, e.g.,
    Code:
    Graph(int vertices) : adjacencyList(vertices) {}
    Quote Originally Posted by rodrigorules
    why doesnt the following work?
    You are trying to insert a pointer into a container of objects. You can accomplish what you want by writing:
    Code:
    for(int i=0;i<vertices;++i)
    {
        adjacencyList.push_back(list<int>());
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob STL question about Containers.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2009, 12:02 PM
  2. Bit arrays with STL containers
    By Mario F. in forum C++ Programming
    Replies: 23
    Last Post: 07-03-2007, 09:50 AM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  4. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08:13 PM
  5. STL: element-wise addition of the contents of two containers
    By geophyzzer in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 06:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21