std::deque::assign(3)
π NAME π
okay so bestie, this is std::deque::assign β and yes, the name is literally std::deque::assign. Itβs from the C++ Standard Libary manual (that spelling is right there in the header, donβt come for me), and the page title is std::deque::assign(3). Slay.
β¨ Synopsis β¨
void assign( size_type count, const T& value ); (1)
template< class InputIt > (2)
void assign( InputIt first, InputIt last );
void assign( std::initializer_list<T> ilist ); (3) (since C++11)
It replaces the contents of the container. Periodt. π
- Replaces the contents with
countcopies ofvalue. - Replaces the contents with copies of those in the range
[first, last). The behavior is undefined if either argument is an iterator into*this. π- Until C++11: this overload has the same effect as overload (1) if
InputItis an integral type. - Since C++11: this overload participates in overload resolution only if
InputItsatisfiesLegacyInputIterator.
- Until C++11: this overload has the same effect as overload (1) if
- Replaces the contents with the elements from the initializer list
ilist.
β οΈ warning bestie: all iterators, pointers and references to the elements of the container are invalidated. The past-the-end iterator is also invalidated. Like, do not keep using them after this. π
π Parameters π
countβ the new size of the container.valueβ the value to initialize elements of the container with.first,lastβ the range to copy the elements from.ilistβ initializer list to copy the values from.
π₯ Complexity π₯
- Linear in
count. - Linear in distance between
firstandlast. - Linear in
ilist.size().
π Example π
The following code uses assign to add several characters to a std::deque<char>. The original has a // Run this code marker, so bestie, go run it π
#include <deque>
#include <iostream>
#include <string>
int main()
{
std::deque<char> characters;
auto print_deque = [&]()
{
for (char c : characters)
std::cout << c << ' ';
std::cout << '\n';
};
characters.assign(5, 'a');
print_deque();
const std::string extra(6, 'b');
characters.assign(extra.begin(), extra.end());
print_deque();
characters.assign({'C', '+', '+', '1', '1'});
print_deque();
}
Output:
a a a a a
b b b b b b
C + + 1 1
π See also π
constructorβ constructs the deque (public member function).
made with π by cppreference.com Β· source/version: cppreference.com Β· date: 2024.06.10 Β· your fave std::deque::assign(3) page π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms