Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C++ STL

Creating a list by assigning the all elements of another list | C++ STL



Here, we are going to learn how to create a list by assigning the all elements of an existing list in C++ STL? It can be possible by direct assigning list and using list::assign() function.
Submitted by IncludeHelp, on November 02, 2018

There are two methods to implement it...

1 ) by assigning the existing list direct to the new list

    list<int> list2 = list1;

2 ) by using list::assign() function

    list<int> list2;
    list2.assign(list1.begin(), list1.end());

Example:

In this example, we have a list list1 with 10 elements and we are creating another list list2, the elements of list2 will be assigning through the list1 by using list:assign() function, which is a library function of "list header". And, we are also creating a list list3 that is going to be assigned directly through the list1.

Program:

#include <iostream>
#include <list>
using namespace std;

//function to display the list
void dispList(list<int> L)
{
	//declaring iterator to the list
	list<int>::iterator l_iter;
	for (l_iter = L.begin(); l_iter != L.end(); l_iter++)
		cout<< *l_iter<< " ";
	cout<<endl;
}

int main()
{
	//list1 declaration
	list<int> list1 = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

	//displaying list1
	cout<<"Size of list1: "<<list1.size()<<endl;
	cout<<"Elements of list1: ";
	dispList(list1);

	//declaring list2 and assigning the Elements
	//of list1
	list<int> list2;
	list2.assign(list1.begin(), list1.end());

	//displaying list2
	cout<<"Size of list2: "<<list2.size()<<endl;
	cout<<"Elements of list2: ";
	dispList(list2);

	//declaring list3 by assigning with list1
	list<int> list3 = list1;
	//displaying list3
	cout<<"Size of list3: "<<list3.size()<<endl;
	cout<<"Elements of list3: ";
	dispList(list3);
	
	return 0;
}

Output

Size of list1: 10
Elements of list1: 10 20 30 40 50 60 70 80 90 100
Size of list2: 10
Elements of list2: 10 20 30 40 50 60 70 80 90 100
Size of list3: 10
Elements of list3: 10 20 30 40 50 60 70 80 90 100  





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates



© https://www.includehelp.com (2015-2018), Some rights reserved.