Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to transfer a range of elements from one list starting at a specific index to another list 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, 8 }; 

    std::list<int>::iterator it;
    it = l2.begin();
    
    advance(it, 2); // advance iterator by 2 positions
  
    // transfer of elements from 3rd element to last in l2 at the end of l1
    l1.splice(l1.end(), l2, it, l2.end()); 
  
    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 6 7 8 
list l2 after splice operation:
4 5 

*/

 



answered Oct 24, 2025 by avibootz
...