In this article we will discuss different techniques to clone the whole list or copy just a sub list.

Suppose we have a std::list of strings i.e.

// List of strings
std::list<std::string> listOfStrs = { "of", "is", "from", "this", "at", "to" };

Now let’s see how to copy this or a sub list from this list,

Copy a std::list using Copy constructor

std::list provides a copy constructor that clones a given to list to new list i.e.

list (const list& x);

We can use it to copy complete list while creating a new list i.e.
// copy all the contents of one list to another
std::list<std::string> listOfStrs_2(listOfStrs);

New list contents will be,
of , is , from , this , at , to ,

Copy a range or sub list to std::list using parameterized constructor

std::list provides a parameterized constructor that accepts a range and copies all the elements in given range to the new list.

list (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());

We can use it to copy complete list or any sub list while creating a new list i.e.
// copy all the contents of one list to another
std::list<std::string> listOfStrs_3(listOfStrs.begin(), listOfStrs.end());

New list contents will be,
of , is , from , this , at , to ,

 Copy a std::list using assignment operator

std::list provides an assignment operator that that copies all the contents of given list to the existing list i.e.

list& operator= (const list& x);

We can use it to copy complete list while creating a new list i.e.
// copy all the contents of one list to another
std::list<std::string> listOfStrsCopy = listOfStrs;

New list contents will be,
of , is , from , this , at , to ,

 Copy a std::list or sub list using list::assign()

std::list provides a member function assign(),

void assign (InputIterator first, InputIterator last);

It accepts a range and copies all the elements in given range to the existing list by replacing it contents.

We can use it to copy complete list to the existing list i.e.

// Create a new list of strings
std::list<std::string> listOfStrs_4;

/*
Copy all list contents using list'a assign member function
It accepts a iteratable range and copy all elements in that range to the list.
*/
listOfStrs_4.assign(listOfStrs.begin(), listOfStrs.end());

New list contents will be,
of , is , from , this , at , to ,

Copying a sub list using list::assign(),

Let’s Copy 2 elements from index 1 and 2 to the new list using std::list::assign()

std::list<std::string> listOfStrs_5;

// Copy 2 elements from index 1 and 2 to the new list
listOfStrs_5.assign(std::next(listOfStrs.begin(), 1), std::next(listOfStrs.begin() , 3));

New list contents will be,
is , from

 Copy a std::list or sub list using std::copy()

We can use STL algorithm to copy a range from a list i.e. a sub list or complete list to the another list i.e.

Let’s Copy 2 elements from index 1 and 2 to the new list using std::copy

std::list<std::string> listOfStrs_5;

// Copy 2 elements from index 1 and 2 to the new list
listOfStrs_5.assign(std::next(listOfStrs.begin(), 1), std::next(listOfStrs.begin() , 3));

New list contents will be,
is , from

 Copy a condition based sub list to std::list using std::copy_if() & Lambda Function

We can use STL algorithm to copy a only selective elements from a list to the another list i.e,

Copy string elements with size 2 from existing list to the new list using std::copy(),

std::list<std::string> listOfStrs_7;

// Copy string elements with size 2  to the new list using std::copy
std::copy_if(listOfStrs.begin(), listOfStrs.end(), std::back_inserter(listOfStrs_7), [](const std::string & str) {
																							if (str.size() == 2)
																								return true;
																							else
																								return false;
																					});

New list contents will be,
of , is , at , to ,

Complete example is as follows,
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <iterator>

// Print the contents of a list
template <typename T>
void print(T  iteratableCont)
{
	for (auto elem : iteratableCont)
		std::cout << elem << " , ";
	std::cout << std::endl;
}


int main()
{
	// List of strings
	std::list<std::string> listOfStrs = { "of", "is", "from", "this", "at", "to" };

	// print the list
	print(listOfStrs);

	/* Copy a list using list's assignment operator */

	// copy all the contents of one list to another
	std::list<std::string> listOfStrs_2(listOfStrs);

	// print the list
	print(listOfStrs_2);

	/* Copy a list using list's copy constructor */

	// copy all the contents of one list to another
	std::list<std::string> listOfStrs_3(listOfStrs.begin(), listOfStrs.end());

	// print the list
	print(listOfStrs_3);

	// Create a new list of strings
	std::list<std::string> listOfStrs_4;
	
	/*
	Copy all list contents using list'a assign member function
	It accepts a iteratable range and copy all elements in that range to the list.
	*/
	listOfStrs_4.assign(listOfStrs.begin(), listOfStrs.end());

	// print the list
	print(listOfStrs_4);

	std::list<std::string> listOfStrs_5;
	
	/*
	Copy a sub list to another list using list's assign member function
	*/
	
	// Copy 2 elements from index 1 and 2 to the new list
	listOfStrs_5.assign(std::next(listOfStrs.begin(), 1), std::next(listOfStrs.begin() , 3));

	// print the list
	print(listOfStrs_5);

	/*
	Copy a sub list to another list using STL Algorithm std::copy
	*/

	std::list<std::string> listOfStrs_6;

	// Copy 2 elements from index 1 and 2 to the new list using std::copy
	std::copy(std::next(listOfStrs.begin(), 1), std::next(listOfStrs.begin(), 3), std::back_inserter(listOfStrs_6));

	// print the list
	print(listOfStrs_6);

	/*
	Copy a some elements from one list to another list based on some condition using STL Algorithm std::copy
	*/

	std::list<std::string> listOfStrs_7;
	
	// Copy string elements with size 2  to the new list using std::copy
	std::copy_if(listOfStrs.begin(), listOfStrs.end(), std::back_inserter(listOfStrs_7), [](const std::string & str) {
																								if (str.size() == 2)
																									return true;
																								else
																									return false;
																						});
	print(listOfStrs_7);

	return 0;

}

Output:
of , is , from , this , at , to , 
of , is , from , this , at , to , 
of , is , from , this , at , to , 
of , is , from , this , at , to , 
is , from , 
is , from , 
of , is , at , to , 

 

 

Join a list of 2000+ Programmers for latest Tips & Tutorials