English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Deque assign() Usage and Example

C++ Deque (Double-ended Queue)

C ++ The Deque Assign() function assigns new content to the deque container and modifies the size of the container accordingly.

Syntax

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

Parameters

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

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

val: The new value to be inserted.

Return value

It does not return any value.

Example1

Let's look at a simple 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:

1 2 3 4

In this example, assign() assigns the content of the first container to the second container.

Example2

Let's look at a simple 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:

6 6 6 6 6

In this example, the assign() function will be called five times 6The value is assigned to the deq container.

C++ Deque (Double-ended Queue)