List assign() function in C++ STL

C++Server Side ProgrammingProgramming

Given is th e task to show the working of the assign() function in C++.

The list::assign() function is a part of the C++ standard template library. It is used to assign the values to a list and also to copy values from one list to another.

<list> header file should be included to call this function.

Syntax

The syntax for assigning new values is as follows −

List_Name.assign(size,value)

Syntax

The syntax for copying values from one list to another is as follows −

First_List.assign(Second_List.begin(),Second_list.end())

Parameters

The function takes two parameters −

First is size, that represents the size of the list and the second one is value, which represents the data value to be stored inside the list.

Return Value

The function has no return value.

Example

Input: Lt.assign(3,10)
Output: The size of list Lt is 3.
The elements of the list Lt are 10 10 10.

Explanation

The following example shows how we can assign a list its size and values by using the assign() function. The first value that we will pass inside the list function becomes the size of the list, in this case it is 3 and the second element is the value that is assigned to each position of the list and here it is 10.

Example

Input: int array[5] = { 1, 2, 3, 4 }
Lt.assign(array,array+3)
Output: The size of list Lt is 3.
The elements of the list Lt are 1 2 3.

Explanation

The following example shows how we can assign values to a list using an array. The total number of elements that we will assign to the list becomes the size of the list.

The user has to simply pass the name of the array as the first argument inside the assign() function, and the second argument should be the name of the array, then a “+” sign followed by the number of elements the user wants to assign to the list.

In the above case we have written 3, so the first three elements of the array will be assigned to the list.

If we write a number that is bigger than the number of elements present in the array, let us say 6, then the program will not show any error instead the size of the list will become 6 and the extra positions in the list will be assigned with the value zero.

Approach used in the below program as follows

• First create a function ShowList(list<int> L) that will display the elements of the list.
• Create an iterator, let’s say itr that will contain the initial element of the list to be displayed.
• Make the loop run till itr reaches the final element of the list.
• Then inside the main() function create three lists using list<int> let’s say L1, L2 ad L3 so that they accept values of type int and then create an array of type int, let’s say arr[] and assign it some values.
• Then use the assign() function to assign size and some values to the list L1 and then pass the list L1 into the ShowDisplay() function.
• Then use the assign() function to copy elements of list L1 into L2 and also pass the list L2 into the ShowList() function.
• Then use the assign() function to copy elements of the array arr[] into the list L3 and pass the list L3 into the DisplayList() function.

Algorithm

Start
Step 1-> Declare function DisplayList(list<int> L) for showing list elements
Declare iterator itr
Loop For itr=L.begin() and itr!=L.end() and itr++
Print *itr
End
Step 2-> In function main()
Declare lists L1,L2,L3
Initialize array arr[]
Call L1.assign(size,value)
Print L1.size();
Call function DisplayList(L1) to display L1
Call L2.assign(L1.begin(),L1.end())
Print L2.size();
Call function DisplayList(L2) to display L2
Call L3.assign(arr,arr+4)
Print L3.size();
Call function DisplayList(L3) to display L3
Stop

Example

Live Demo

#include<iostream>
#include<list>
using namespace std;
int ShowList(list<int> L) {
cout<<"The elements of the list are ";
list<int>::iterator itr;
for(itr=L.begin(); itr!=L.end(); itr++) {
cout<<*itr<<" ";
}
cout<<"\n";
}
int main() {
list<int> L1;
list<int> L2;
list<int> L3;
int arr[10] = { 6, 7, 2, 4 };
//assigning size and values to list L1
L1.assign(3,20);
cout<<"The size of list L1 is "<<L1.size()<<"\n";
ShowList(L1);
//copying the elements of L1 into L3
L2.assign(L1.begin(),L1.end());
cout<<"The size of list is L2 "<<L2.size()<<"\n";
ShowList(L2);
//copying the elements of arr[] into list L3
L3.assign(arr,arr+4);
cout<<"The size of list is L3 "<<L3.size()<<"\n";
ShowList(L3);
return 0;
}

Output

If we run the above code it will generate the following output −

The size of list L1 is 3
The elements of the list are 20 20 20
The size of list L2 is 3
The elements of the list are 20 20 20
The size of list L3 is 4
The elements of the list are 6 7 2 4

Explanation

For the list L1 we gave size as 3 and value as 20 in the assign() function which had generated the above output.

Then we copied the elements of list L1 into that L2 that gives L2 the same size and value as that of L1 as we can see in the output.

Then we used the assign function to copy all the elements of the array arr[] which made the size of L3 equal to 4 and its elements same as the elements of the array as in the output.

Updated on 20-Jan-2020 07:22:18