C++ List assign
The assign function in C++ replaces the existing content of a list container with new elements.
Syntax
void assign(InputIterator first, OutputIterator last);
void assign(size_type n, value_type val);
Parameter
first,last: It specifies the scope of the elements to be duplicated.
n : It specifies the new size of the container.
val : Fresh value to be inserted into a newly created area.
Return value
It does not return any value.
Example 1
Let's see a simple example
#include <iostream>
#include<list>
using namespace std;
int main()
{
list<int> li={1,2,3,4};
list<int>::iterator itr;
li.assign(3,10);
for(itr=li.begin();itr!=li.end();++itr)
cout<<*itr<<" ";
return 0;
}
Output:
10 10 10
In this instance, the assign method substitutes the existing content with fresh data, setting the value '10' thrice within the list container.
Example 2
Let's see a simple example
#include <iostream>
#include<list>
using namespace std;
int main()
{
list<char> first={'C','+','+'};
list<char> second;
list<char>::iterator itr;
second.assign(first.begin(),first.end());
for(itr=second.begin();itr!=second.end();++itr)
cout<<*itr;
return 0;
}
Output:
In this illustration, the assign method assigns the contents of the initial list to the target list.