C++ deque assign() Function
Example
Replace all elements with 3 copies of the same value:
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars.assign(3, "Tesla");
for (string car : cars) {
cout << car << "\n";
}
Definition and Usage
The assign() function removes all the elements of a deque, and fills it with new values.
You can give it a number and a value, to fill the deque with that many copies of the value. You can also give it two iterators (objects that point to elements), to copy a range of elements from another container.
The assign function is defined in the <deque> header file.
Syntax
deque.assign(size_t amount, <type> value);
deque.assign(iterator start, iterator end);
Use one of the two forms. The size_t data type is a non-negative integer. <type> refers to the type of the data that the deque contains.
Parameter Values
| Parameter | Description |
|---|---|
| amount | Required. The number of elements to put in the deque. |
| value | Required. The value to put in each element. |
| start | Required. An iterator pointing to the first element to copy. |
| end | Required. An iterator pointing to the position after the last element to copy. The element at this position is not copied. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about deques in our Deque Tutorial.