CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    appending std:list to the end of another std::list

    Hello

    I have 2 std::list l1 & l2 (both holdings integers) and I would like to append l2 to the end of l1,

    Is there a function that implements that other then itereating l2 and using push_back to l1?

    Thanks
    avi123

  2. #2
    Join Date
    Oct 2004
    Posts
    64

    Re: appending std:list to the end of another std::list

    l1.splice(l1.end(),l2);

    note: l2 will be deleted

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,718

    Re: appending std:list to the end of another std::list

    insert() or splice() depending on what you want ...

    Code:
        // append l1 to l2 (l1 unchanged)
    
        l2.insert(l2.end(),l1.begin(),l1.end());
    
        // append l1 to l2 (elements appended to l2 are removed from l1)
        // (general form ... TG gave form that is actually better suited
        //  for your needs)
    
        l2.splice(l2.end(),l1,l1.begin(),l1.end());

  4. #4
    Join Date
    Sep 2003
    Posts
    815

    Re: appending std:list to the end of another std::list

    thank you both
    I used the insert since I needed to keep the second list

    avi123

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