List Assign Function - C++ Programming Tutorial
C++ Course / Dynamic Programming / List Assign Function

List Assign Function

BLUF: Mastering List Assign Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: List Assign Function

C++ is renowned for its efficiency. Learn how List Assign Function enables low-level control and high-performance computing in the tutorial below.

C++ List assign

The assign function in C++ replaces the existing content of a list container with new elements.

Syntax

Example

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

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:

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

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience