Thread: nested stl containers

  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
    28,413
    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>());
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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