The deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container.
- Syntax:
deque_name.assign(size, val)
Parameters: The function accepts two parameters which are described below:
- size: it specifies the number of values to be assigned to the container.
- val: it specifies the value to be assigned to the container.
Return Value: The function returns nothing.
Below programs illustrate the above function:
Program 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque< int > dq;
dq.assign(5, 10);
cout << "The deque elements: " ;
for ( auto it = dq.begin(); it != dq.end(); it++)
cout << *it << " " ;
dq.assign(10, 15);
cout << "\nThe deque elements: " ;
for ( auto it = dq.begin(); it != dq.end(); it++)
cout << *it << " " ;
return 0;
}
|
Output:
The deque elements: 10 10 10 10 10
The deque elements: 15 15 15 15 15 15 15 15 15 15
- Syntax:
deque1_name.assign(iterator1, iterator2)
Parameters: The function accepts two parameters which are described below:
- iterator1: it specifies the iterator which points to the starting element of container(deque, array, …) whose elements are to be transferred to deque1.
- iterator2: it specifies the iterator which points to the last element of a container(deque, array, …) whose elements are to be transferred to deque1
Return Value: The function returns nothing.
Below programs illustrate the above function:
Program 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque< int > dq;
dq.assign(5, 10);
cout << "The deque elements: " ;
for ( auto it = dq.begin(); it != dq.end(); it++)
cout << *it << " " ;
deque< int > dq1;
dq1.assign(dq.begin() + 1, dq.end());
cout << "\nThe deque1 elements: " ;
for ( auto it = dq1.begin(); it != dq1.end(); it++)
cout << *it << " " ;
return 0;
}
|
Output:
The deque elements: 10 10 10 10 10
The deque1 elements: 10 10 10 10