dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: STL list

  1. #1
    Join Date
    May 2018
    Posts
    84

    STL list

    Code:
    struct dati {
    	int id;
    	string cognome;
    };
    
    struct elem {
    	int cc;
    	list <dati> conto;
    };
    
    class contocorr {
    	vector <elem> vettore;
    	int maxcoll;
    public:
    	contocorr(int N);
    };
    
    // costruttore
    contocorr::contocorr(int N) : maxcoll(0) {
    	vector <elem> vettore(N);
    
    	for(int i=0; i<N; ++i) {
    		vettore[i].cc=-1;
    		vettore[i].conto;
    	}
    }
    I have doubt in this code because in the class constructor I'd like to create empty list (conto) for every element of the <vector> vettore.
    If define in main section an object as 'contocorr XXX(5)' I will get class instance of 'contocorr' which has the vector 'elem' of 5 elements but every empty list of vettore[n].conto is created or no? I'm not able to understand it.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: STL list

    Didn't you try to test your code in the debugger to see what happens in the contocorr ctor?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,764

    Re: STL list

    I think this is what you are after:

    Code:
    contocorr::contocorr(int N) : maxcoll(0) {
        vettore.resize(N);
    }
    
    ...
    
    contocorr XXX(5);
    This will give vettore a size of 5 elements, with each element having an empty list.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.2)

  4. #4
    Join Date
    May 2018
    Posts
    84

    Re: STL list

    Quote Originally Posted by VictorN View Post
    Didn't you try to test your code in the debugger to see what happens in the contocorr ctor?
    Excuse me for my stupid question but I'm teaching myself C++ language in environment by old customized Debian Virtual Machine with g++ version 4.7, which is provided by italian university

  5. #5
    Join Date
    May 2018
    Posts
    84

    Re: STL list

    Quote Originally Posted by 2kaud View Post
    This will give vettore a size of 5 elements, with each element having an empty list.
    Thank you very much, It's best practise to initialise each list to NULL or It's automatically process in STL list library ?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,764

    Re: STL list

    You don't need to initialise each list as the default list constructor will be used - which will initialise the list to no entries (empty list).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.2)

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,032

    Re: STL list

    Quote Originally Posted by zio_mangrovia View Post
    Excuse me for my stupid question but I'm teaching myself C++ language in environment by old customized Debian Virtual Machine with g++ version 4.7, which is provided by italian university
    Does that mean you don't have access to a debugger?

  8. #8
    Join Date
    May 2018
    Posts
    84

    Re: STL list

    Quote Originally Posted by Arjay View Post
    Does that mean you don't have access to a debugger?
    I tried with ddd but It often gives errors so I have to restart it again, so I decide to install new Debian machine to make exercises.
    I found out if I leave STL library and I use this code:


    Code:
    struct elem {
    	int val;
    	elem* next;
    } ** hashtable;
    
    hashtable = new elem* [8];
    in DDD I typed: graph display hashtable[0]@
    It shows that all pointers values of hashtable array[8] are 0, compiler reset these values?
    I though to set every pointer of 8 elements to NULL but I see these values are already 0.
    What do you think?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,764

    Re: STL list

    For new [], if an initializer is absent then each element of the created object is default-initialised. For a type of elem* (pointer), then this is nullptr (0).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)