Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/assign"

From cppreference.com
m (this)
(Added LWG issue #2209 DR.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | assign}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|assign}}
 +
{{cpp/container/{{#var:cont}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{#ifeq:{{{1}}}|vector|
+
{{#switch:{{#var:cont}}
{{dcl rev multi | num=1
+
|vector=
| dcl1=
+
{{dcla|num=1|constexpr=c++20|
 
void assign( size_type count, const T& value );
 
void assign( size_type count, const T& value );
| since2=c++20 | dcl2=
 
constexpr void assign( size_type count, const T& value );
 
 
}}
 
}}
{{dcl rev multi | num=2
+
{{dcla|num=2|constexpr=c++20|
| dcl1=
+
 
template< class InputIt >
 
template< class InputIt >
 
void assign( InputIt first, InputIt last );
 
void assign( InputIt first, InputIt last );
| since2=c++20 | dcl2=
+
}}
 +
{{dcla|num=3|since=c++11|constexpr=c++20|
 +
void assign( std::initializer_list<T> ilist );
 +
}}
 +
|inplace_vector=
 +
{{dcl|num=1|since={{cpp/std|{{#var:cont}}}}|
 +
constexpr void assign( size_type count, const T& value );
 +
}}
 +
{{dcl|num=2|since={{cpp/std|{{#var:cont}}}}|
 
template< class InputIt >
 
template< class InputIt >
 
constexpr void assign( InputIt first, InputIt last );
 
constexpr void assign( InputIt first, InputIt last );
 
}}
 
}}
{{dcl rev multi | num=3
+
{{dcl|num=3|since={{cpp/std|{{#var:cont}}}}|
| since1=c++11 | dcl1=
+
void assign( std::initializer_list<T> ilist );
+
| since2=c++20 | dcl2=
+
 
constexpr void assign( std::initializer_list<T> ilist );
 
constexpr void assign( std::initializer_list<T> ilist );
 
}}
 
}}
|{{dcl | num=1 | since={{cpp/std|{{{1|}}}}} |
+
|
 +
{{dcl|num=1|since={{cpp/std|{{#var:cont}}}}|
 
void assign( size_type count, const T& value );
 
void assign( size_type count, const T& value );
 
}}
 
}}
{{dcl | num=2 | since={{cpp/std|{{{1|}}}}} |
+
{{dcl|num=2|since={{cpp/std|{{#var:cont}}}}|
 
template< class InputIt >
 
template< class InputIt >
 
void assign( InputIt first, InputIt last );
 
void assign( InputIt first, InputIt last );
 
}}
 
}}
{{dcl | num=3 | since=c++11 |
+
{{dcl|num=3|since=c++11|
 
void assign( std::initializer_list<T> ilist );
 
void assign( std::initializer_list<T> ilist );
 
}}
 
}}
Line 38: Line 42:
 
Replaces the contents of the container.
 
Replaces the contents of the container.
  
@1@ Replaces the contents with {{tt|count}} copies of value {{tt|value}}
+
@1@ Replaces the contents with {{c|count}} copies of value {{c|value}}.
  
@2@ Replaces the contents with copies of those in the range {{tt|[first, last)}}. The behavior is undefined if either argument is an iterator into {{c|*this}}.
+
@2@ Replaces the contents with copies of those in the range {{range|first|last}}.
{{rrev multi|until1=c++11|rev1=
+
@@ If either argument is an iterator into {{c|*this}}, the behavior is undefined.
This overload has the same effect as overload {{v|1}} if {{tt|InputIt}} is an integral type.
+
{{#switch:{{#var:cont}}
|rev2=
+
|inplace_vector=
 
{{cpp/enable if|{{tt|InputIt}} satisfies {{named req|InputIterator}}}}.
 
{{cpp/enable if|{{tt|InputIt}} satisfies {{named req|InputIterator}}}}.
 +
|
 +
{{rrev multi|until1=c++11
 +
|rev1=This overload has the same effect as overload {{v|1}} if {{tt|InputIt}} is an integral type.
 +
|rev2={{cpp/enable if|{{tt|InputIt}} satisfies {{named req|InputIterator}}}}.
 
}}
 
}}
@3@ Replaces the contents with the elements from the initializer list {{tt|ilist}}.
+
}}
 +
@3@ Replaces the contents with the elements from {{c|ilist}}.
  
All iterators, pointers and references to the elements of the container are invalidated. {{#switch:{{{1|}}}|vector|deque=The past-the-end iterator is also invalidated.}}
+
{{cpp/container/note iterator invalidation|{{#var:cont}}|assign}}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | count | the new size of the container}}
+
{{par|count|the new size of the container}}
{{par | value | the value to initialize elements of the container with}}
+
{{par|value|the value to initialize elements of the container with}}
{{par | first, last | the range to copy the elements from}}
+
{{par|first, last|the range to copy the elements from}}
{{par | ilist | initializer list to copy the values from}}
+
{{par|ilist|{{lc|std::initializer_list}} to copy the values from}}
{{par end}}  
+
{{par end}}
  
 
===Complexity===
 
===Complexity===
@1@ Linear in {{tt|count}}
+
@1@ Linear in {{c|count}}.
  
@2@ Linear in distance between {{tt|first}} and {{tt|last}}
+
@2@ Linear in distance between {{c|first}} and {{c|last}}.
  
@3@ Linear in {{c|ilist.size()}}
+
@3@ Linear in {{c|ilist.size()}}.
 +
<!---->
 +
{{#switch:{{#var:cont}}
 +
|inplace_vector=
 +
===Exceptions===
 +
@1@ {{lc|std::bad_alloc}}, if {{c|1=count > capacity()}}.
  
 +
@2@ {{lc|std::bad_alloc}}, if {{c|1=std::ranges::distance(first, last) > capacity()}}.
 +
 +
@3@ {{lc|std::bad_alloc}}, if {{c|1=ilist.size() > capacity()}}.
 +
 +
@1-3@ Any exception thrown by initialization of inserted elements.
 +
}}
 +
<!---->
 
===Example===
 
===Example===
 +
{{#switch:{{#var:cont}}
 +
|inplace_vector=
 
{{example
 
{{example
| The following code uses {{tt|assign}} to add several characters to a {{c|std::{{{1}}}<char>}}:
+
|The following code uses {{tt|assign}} to add several characters to a {{c/core|std::{{#var:cont}}<char, 5>}}:
| code=
+
|code=
#include <{{{1}}}>
+
#include <inplace_vector>
 +
#include <iterator>
 +
#include <new>
 +
#include <print>
 +
 
 +
int main()
 +
{
 +
    std::inplace_vector<char, 5> chars;
 +
   
 +
    chars.assign(4, 'a'); // overload (1)
 +
    std::println("{}", chars);
 +
   
 +
    const char extra[3]{'a', 'b', 'c'};
 +
    chars.assign(std::cbegin(extra), std::cend(extra)); // overload (2)
 +
    std::println("{}", chars);
 +
   
 +
    chars.assign({'C', '+', '+', '2', '6'}); // overload (3)
 +
    std::println("{}", chars);
 +
   
 +
    try
 +
    {
 +
        chars.assign(8, 'x'); // throws: count > chars.capacity()
 +
    }
 +
    catch(const std::bad_alloc&) { std::println("std::bad_alloc #1"); }
 +
   
 +
    try
 +
    {
 +
        const char bad[8]{'?'}; // ranges::distance(bad) > chars.capacity()
 +
        chars.assign(std::cbegin(bad), std::cend(bad)); // throws
 +
    }
 +
    catch(const std::bad_alloc&) { std::println("std::bad_alloc #2"); }
 +
   
 +
    try
 +
    {
 +
        const auto l = {'1', '2', '3', '4', '5', '6'};
 +
        chars.assign(l); // throws: l.size() > chars.capacity()
 +
    }
 +
    catch(const std::bad_alloc&) { std::println("std::bad_alloc #3"); }
 +
}
 +
|output=
 +
['a', 'a', 'a', 'a']
 +
['a', 'b', 'c']
 +
['C', '+', '+', '2', '6']
 +
std::bad_alloc #1
 +
std::bad_alloc #2
 +
std::bad_alloc #3
 +
}}
 +
|
 +
{{example
 +
|The following code uses {{tt|assign}} to add several characters to a {{c/core|std::{{#var:cont}}<char>}}:
 +
|code=
 +
#include <{{#var:cont}}>
 
#include <iostream>
 
#include <iostream>
 
#include <string>
 
#include <string>
+
 
 
int main()
 
int main()
 
{
 
{
     std::{{{1}}}<char> characters;
+
     std::{{#var:cont}}<char> characters;
 
+
   
     auto print_{{{1}}} = [&](){
+
     auto print_{{#var:cont}} = [&]()
 +
    {
 
         for (char c : characters)
 
         for (char c : characters)
 
             std::cout << c << ' ';
 
             std::cout << c << ' ';
         std::cout << '\n';
+
         std::cout << '\n';
 
     };
 
     };
+
   
 
     characters.assign(5, 'a');
 
     characters.assign(5, 'a');
     print_{{{1}}}();
+
     print_{{#var:cont}}();
 
      
 
      
 
     const std::string extra(6, 'b');
 
     const std::string extra(6, 'b');
 
     characters.assign(extra.begin(), extra.end());
 
     characters.assign(extra.begin(), extra.end());
     print_{{{1}}}();
+
     print_{{#var:cont}}();
 
+
   
 
     characters.assign({'C', '+', '+', '1', '1'});
 
     characters.assign({'C', '+', '+', '1', '1'});
     print_{{{1}}}();
+
     print_{{#var:cont}}();
 
}
 
}
| output=
+
|output=
a a a a a  
+
a a a a a
b b b b b b  
+
b b b b b b
 
C + + 1 1
 
C + + 1 1
 
}}
 
}}
 
+
}}
 +
<!-- Keep these two comments to avoid extra new lines for inplace_vector -->
 +
{{#ifeq:{{#var:cont}}|inplace_vector||
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr={{#ifeq:{{#var:cont}}|list|320|2209}}|std={{#ifeq:{{#var:cont}}
 +
|forward_list|C++11|C++98}}|before=the replacement operation was required to be implemented as<br>erasing all existing elements followed by inserting the given elements|after=removed the<br>requirement}}
 +
{{dr list end}}
 +
}}
 +
<!---->
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/container/dsc constructor |{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc assign_range|{{#var:cont}}}}
 +
{{dsc inc|cpp/container/dsc operator{{=}}|{{#var:cont}}}}
 
{{dsc end}}
 
{{dsc end}}

Latest revision as of 22:28, 14 November 2024

 
 
 
 
constexpr void assign( size_type count, const T& value );
(1) (since C++26)
template< class InputIt >
constexpr void assign( InputIt first, InputIt last );
(2) (since C++26)
constexpr void assign( std::initializer_list<T> ilist );
(3) (since C++26)

Replaces the contents of the container.

1) Replaces the contents with count copies of value value.
2) Replaces the contents with copies of those in the range [firstlast).
If either argument is an iterator into *this, the behavior is undefined. This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator.
3) Replaces the contents with the elements from ilist.

Contents

[edit] 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 - std::initializer_list to copy the values from

[edit] Complexity

1) Linear in count.
2) Linear in distance between first and last.
3) Linear in ilist.size().

Exceptions

1) std::bad_alloc, if count > capacity().
2) std::bad_alloc, if std::ranges::distance(first, last) > capacity().
3) std::bad_alloc, if ilist.size() > capacity().
1-3) Any exception thrown by initialization of inserted elements.

[edit] Example

The following code uses assign to add several characters to a std::inplace_vector<char, 5>:

#include <inplace_vector>
#include <iterator>
#include <new>
#include <print>
 
int main()
{
    std::inplace_vector<char, 5> chars;
 
    chars.assign(4, 'a'); // overload (1)
    std::println("{}", chars);
 
    const char extra[3]{'a', 'b', 'c'};
    chars.assign(std::cbegin(extra), std::cend(extra)); // overload (2)
    std::println("{}", chars);
 
    chars.assign({'C', '+', '+', '2', '6'}); // overload (3)
    std::println("{}", chars);
 
    try
    {
        chars.assign(8, 'x'); // throws: count > chars.capacity()
    }
    catch(const std::bad_alloc&) { std::println("std::bad_alloc #1"); }
 
    try
    {
        const char bad[8]{'?'}; // ranges::distance(bad) > chars.capacity()
        chars.assign(std::cbegin(bad), std::cend(bad)); // throws
    }
    catch(const std::bad_alloc&) { std::println("std::bad_alloc #2"); }
 
    try
    {
        const auto l = {'1', '2', '3', '4', '5', '6'};
        chars.assign(l); // throws: l.size() > chars.capacity()
    }
    catch(const std::bad_alloc&) { std::println("std::bad_alloc #3"); }
}

Output:

['a', 'a', 'a', 'a']
['a', 'b', 'c']
['C', '+', '+', '2', '6']
std::bad_alloc #1
std::bad_alloc #2
std::bad_alloc #3

[edit] See also

assigns a range of values to the container
(public member function of std::inplace_vector<T,N>) [edit]
assigns values to the container
(public member function of std::inplace_vector<T,N>) [edit]