Namespaces
Variants
Views
Actions

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

From cppreference.com
m (this)
(Added LWG issue #320 DR.)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | assign}}
+
{{cpp/container/{{{1|}}}/title|assign}}
 
{{cpp/container/{{{1|}}}/navbar}}
 
{{cpp/container/{{{1|}}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{#ifeq:{{{1}}}|vector|
+
{{#ifeq:{{{1}}}
{{dcl rev multi | num=1
+
|vector|
| dcl1=
+
{{dcl rev multi|num=1
 +
|dcl1=
 
void assign( size_type count, const T& value );
 
void assign( size_type count, const T& value );
| since2=c++20 | dcl2=
+
|since2=c++20|dcl2=
 
constexpr void assign( size_type count, const T& value );
 
constexpr void assign( size_type count, const T& value );
 
}}
 
}}
{{dcl rev multi | num=2
+
{{dcl rev multi|num=2
| dcl1=
+
|dcl1=
 
template< class InputIt >
 
template< class InputIt >
 
void assign( InputIt first, InputIt last );
 
void assign( InputIt first, InputIt last );
| since2=c++20 | dcl2=
+
|since2=c++20|dcl2=
 
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 rev multi|num=3
| since1=c++11 | dcl1=
+
|since1=c++11|dcl1=
 
void assign( std::initializer_list<T> ilist );
 
void assign( std::initializer_list<T> ilist );
| since2=c++20 | dcl2=
+
|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|{{{1|}}}}}|
 
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|{{{1|}}}}}|
 
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 40:
 
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 {{tt|[first, last)}}. The behavior is undefined if either argument is an iterator into {{c|*this}}.
Line 46: Line 48:
 
{{cpp/enable if|{{tt|InputIt}} satisfies {{named req|InputIterator}}}}.
 
{{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 the initializer list {{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.}}
 
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.}}
Line 52: Line 54:
 
===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|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()}}
Line 67: Line 69:
 
===Example===
 
===Example===
 
{{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::{{{1}}}<char>}}:
| code=
+
|code=
 
#include <{{{1}}}>
 
#include <{{{1}}}>
 
#include <iostream>
 
#include <iostream>
Line 77: Line 79:
 
     std::{{{1}}}<char> characters;
 
     std::{{{1}}}<char> characters;
  
     auto print_{{{1}}} = [&](){
+
     auto print_{{{1}}} = [&]()
 +
    {
 
         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_{{{1}}}();
Line 89: Line 92:
 
     characters.assign(extra.begin(), extra.end());
 
     characters.assign(extra.begin(), extra.end());
 
     print_{{{1}}}();
 
     print_{{{1}}}();
 
+
   
 
     characters.assign({'C', '+', '+', '1', '1'});
 
     characters.assign({'C', '+', '+', '1', '1'});
 
     print_{{{1}}}();
 
     print_{{{1}}}();
 
}
 
}
| 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
 +
}}
 +
 +
{{#switch: {{{1}}}
 +
|list=
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=320|std=C++98|before=the replacement operation was defined as erasing all<br>existing elements followed by inserting the given elements|after=removed the definition}}
 +
{{dr list end}}
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/container/dsc constructor |{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc constructor|{{{1|}}}}}
 
{{dsc end}}
 
{{dsc end}}

Revision as of 00:04, 11 January 2023

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

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 [first, last). The behavior is undefined if either argument is an iterator into *this.

This overload has the same effect as overload (1) if InputIt is an integral type.

(until C++11)

This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator.

(since C++11)
3) Replaces the contents with the elements from the initializer list ilist.

All iterators, pointers and references to the elements of the container are invalidated.

Contents

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

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

Example

The following code uses assign to add several characters to a std::{{{1}}}<char>:

#include <{{{1}}}>
#include <iostream>
#include <string>
 
int main()
{
    std::{{{1}}}<char> characters;
 
    auto print_{{{1}}} = [&]()
    {
        for (char c : characters)
            std::cout << c << ' ';
        std::cout << '\n';  
    };
 
    characters.assign(5, 'a');
    print_{{{1}}}();
 
    const std::string extra(6, 'b');
    characters.assign(extra.begin(), extra.end());
    print_{{{1}}}();
 
    characters.assign({'C', '+', '+', '1', '1'});
    print_{{{1}}}();
}

Output:

a a a a a 
b b b b b b 
C + + 1 1


See also

constructs the
(public member function of std::{{{1}}}) [edit]