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

Assign the elements to the list (different methods) - Example of list::assign() | C++ STL



Example of list::assign() in C++ STL: Here, we are going to learn how to assign the elements, replaces the elements to the list by using list::assign() method in C++ STL?
Submitted by IncludeHelp, on November 01, 2018

list::assign() in C++ STL

It assigns or replaces the elements to the list.

Example:

In the given example, there are 3 different methods are used to assign/replace elements to the list.

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

//function to display the list
void dispList(list<int> L)
{
	//declaring interator 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()
{
	list<int> list1;
	list<int> list2;
	list<int> list3;
	list<int> list4;

	//assigning 100 to 5 elements
	list1.assign(5,100);
	cout<<"size of list1: "<<list1.size()<<endl;
	cout<<"Elements of list1: ";
	dispList(list1);

	//assigning all elements of list1 to list2
	list2.assign(list1.begin(), list1.end());
	cout<<"size of list2: "<<list2.size()<<endl;
	cout<<"Elements of list2: ";
	dispList(list2);

	//assigning list by array elements
	int arr []={10, 20, 30, 40};
	list3.assign(arr, arr+4);
	cout<<"size of list3: "<<list3.size()<<endl;
	cout<<"Elements of list3: ";
	dispList(list3);
	return 0;
}

Output

size of list1: 5
Elements of list1: 100 100 100 100 100
size of list2: 5
Elements of list2: 100 100 100 100 100
size of list3: 4
Elements of list3: 10 20 30 40  

Ref: list::assign()






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.