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 to another in C++

1 Answer

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

int main()
{
    // initializing lists 
    std::list<int> source = {1, 2, 3, 4, 5, 6, 7, 8};
    std::list<int> destination = {88, 99, 100};

    // range: from 2nd to 5th element 
    auto start = std::next(source.begin(), 1); // points to element 2
    auto end = std::next(source.begin(), 5);   // points to element 6

    // Transfer range to destination at the beginning
    destination.splice(destination.begin(), source, start, end);

    std::cout << "Source list: ";
    for (int val : source) std::cout << val << " ";
    std::cout << "\nDestination list: ";
    for (int val : destination) std::cout << val << " ";
}



/*
run:

Source list: 1 6 7 8 
Destination list: 2 3 4 5 88 99 100 

*/

 



answered Oct 24, 2025 by avibootz
...