C++ Deque assign
The assign function in C++ for deque assigns fresh data to the container and adjusts its size accordingly.
Syntax
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
#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 instance, the assign method assigns the content of the initial container to the secondary container.
Example 2
Let's see 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 instance, the assign method sets the value '6' repeatedly five times within the deq container.