Deque Assign Function - C++ Programming Tutorial
C++ Course / STL Queue & Stack / Deque Assign Function

Deque Assign Function

BLUF: Mastering Deque 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: Deque Assign Function

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

C++ Deque assign

The assign function in C++ for deque assigns fresh data to the container and adjusts its size accordingly.

Syntax

Example

void assign(InputIterator first, InputIterator last);
void assign(int n,value_type val);

Parameter

The (first,last) parameter specifies the range within which the new elements will be added.

It specifies the updated capacity of the deque data structure.

val : New value to be inserted.

Return value

It does not return any value.

Example 1

Let's see a simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
   deque<int> first={1,2,3,4};
   deque<int> second;
   deque<int>::iterator itr=second.begin();
   second.assign(first.begin(),first.end());
   for(itr=second.begin();itr!=second.end();++itr)
   std::cout <<*itr<<" ";
    return 0;
}

Output:

Output

1 2 3 4

In this instance, the assign method assigns the content of the initial container to the secondary container.

Example 2

Let's see a simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> deq;
  deque<int>::iterator itr;
  deq.assign(5,6);
  for(itr=deq.begin();itr!=deq.end();++itr)
  std::cout << *itr <<" ";
  return 0;
}

Output:

Output

6 6 6 6 6

In this instance, the assign method sets the value '6' repeatedly five times within the deq container.

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