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

Thread: list and arrays

  1. #1
    Join Date
    Dec 2013
    Posts
    3

    list and arrays

    using the following header files:

    Code:
    #include <list>
    #include <map>
    #include <iostream>
    #include <algorithm>
    and
    Code:
    std::list
    If there is a creation of a list, how can one find the sizeof the list. and is it possible to copy all the data from the linked list into an array. Assuming that the data is of type
    Code:
    char

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: list and arrays

    Quote Originally Posted by Nair69 View Post
    how can one find the sizeof the list.
    Do you literally mean "sizeof()"?
    Code:
    #include <list>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout << sizeof(std::list<char>) << endl;
    }
    That returns the size of the std::list<char> type in bytes.

    Or do you mean you want to know the number of items in the list? If so, then look at the std::list<T>::size() function.
    and is it possible to copy all the data from the linked list into an array. Assuming that the data is of type
    Code:
    char
    Use std::copy() and make sure the array is sized appropriately so that it can hold all of the elements. To be safer, ditch the array and use a vector for safety.
    Code:
    #include <algorithm>
    #include <vector>
    #include <list>
    #include <iterator>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        std::list<char> list1;
        list1.push_back('a');
        list1.push_back('b');
        list1.push_back('c');
        vector<char> v;
    
        // copy items to the vector
        copy(list1.begin(), list1.end(), back_inserter(v));
    
       // output items in the vector
        copy(v.begin(), v.end(), ostream_iterator<char>(cout, " "));
    }
    http://www.cplusplus.com/reference/algorithm/copy/

    Regards,

    Paul McKenzie

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured