Deque Assign Function

C++ Deque assign

C++ Deque assign function assigns new contents to the deque container and the size of the container is modified accordingly.

Syntax

Example

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

Parameter

(first,last) : It defines the range between which the new elements to be inserted.

n : It defines the new size of the deque container.

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 example, assign assigns the content of first container to the second 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 example, assign function assigns the value '6' five times to the deq container.

Input Required

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