How to transfer elements from one list to another in C++

1 Answer

0 votes
#include <iostream>
#include <list>

int main()
{
    // initializing lists 
    std::list<int> l1 = { 1, 2, 3}; 
    std::list<int> l2 = { 4, 5, 6, 7 }; 

  
    // transfer all the elements of l2 to l1 
    l1.splice(l1.end(), l2);   
    
    std::cout << "list l1 after splice operation:" << std::endl; 
    for (auto i : l1) {
        std::cout << i << " "; 
    }
    std::cout << std::endl; 
    
    std::cout << "list l2 after splice operation:" << std::endl; 
    for (auto i : l2) {
        std::cout << i << " "; 
    }
 }
 


/*
run:

list l1 after splice operation:
1 2 3 4 5 6 7 
list l2 after splice operation:

*/

 



answered Oct 24, 2025 by avibootz
edited Oct 24, 2025 by avibootz
...