std::basic_string<CharT,Traits,Allocator>::assign
: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
basic_string& assign( size_type count, CharT ch ); |
(C++20) | |
constexpr basic_string& assign( size_type count, CharT ch ); |
(C++20) | |
| (2) | ||
basic_string& assign( const basic_string& str ); |
(C++20) | |
constexpr basic_string& assign( const basic_string& str ); |
(C++20) | |
| (3) | ||
basic_string& assign( const basic_string& str, size_type pos, size_type count ); |
(C++14) | |
basic_string& assign( const basic_string& str, size_type pos, size_type count = npos); |
(C++14) (C++20) |
|
constexpr basic_string& assign( const basic_string& str, size_type pos, size_type count = npos); |
(C++20) | |
| (4) | ||
basic_string& assign( basic_string&& str ); |
(C++11) (C++17) |
|
basic_string& assign( basic_string&& str ) noexcept(/* see below */); |
(C++17) (C++20) |
|
constexpr basic_string& assign( basic_string&& str ) noexcept(/* see below */); |
(C++20) | |
| (5) | ||
basic_string& assign( const CharT* s, size_type count ); |
(C++20) | |
constexpr basic_string& assign( const CharT* s, size_type count ); |
(C++20) | |
| (6) | ||
basic_string& assign( const CharT* s ); |
(C++20) | |
constexpr basic_string& assign( const CharT* s ); |
(C++20) | |
| (7) | ||
template< class InputIt > basic_string& assign( InputIt first, InputIt last ); |
(C++20) | |
template< class InputIt > constexpr basic_string& assign( InputIt first, InputIt last ); |
(C++20) | |
| (8) | ||
basic_string& assign( std::initializer_list<CharT> ilist ); |
(C++11) (C++20) |
|
constexpr basic_string& assign( std::initializer_list<CharT> ilist ); |
(C++20) | |
| (9) | ||
template < class T > basic_string& assign( const T& t ); |
(C++17) (C++20) |
|
template < class T > constexpr basic_string& assign( const T& t ); |
(C++20) | |
| (10) | ||
template < class T > basic_string& assign( const T& t, size_type pos, size_type count = npos); |
(C++17) (C++20) |
|
template < class T > constexpr basic_string& assign( const T& t, size_type pos, size_type count = npos); |
(C++20) | |
1)
ch count 2)
str *this = str; (C++11)4)
str *this = std::move(str) 5)
[s, s+count) 6)
s Traits::length(s) 8)
ilist 9)
std::basic_string_view<CharT, Traits> sv = t; t sv assign(sv.data(), sv.size()) sv std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false 10)
std::basic_string_view<CharT, Traits> sv = t; t sv sv [pos, pos+count) sv count == npos [pos, sv.size()) pos > sv.size() std::out_of_range std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false | count | - | |
| pos | - | |
| ch | - | |
| first, last | - | |
| str | - | |
| s | - | |
| ilist | - | std::initializer_list |
| t | - | (std::basic_string_view ) |
-InputIt LegacyInputIterator
| ||
*this
1)
count 2)
str 3)
count 4)
alloc alloc != other.get_allocator() 5)
count 6)
s 7)
first last 8)
ilist () (C++11)
size() > max_size() std::length_error
|
4)
noexcept :
noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value) |
(C++17) |
C++
| DR | |||
|---|---|---|---|
| LWG 2063 | C++11 | non-normative note stated that swap is a valid implementation of move-assign | corrected to require move assignment |
| LWG 2579 | C++11 | assign(const basic_string&) doesn't propagate allocators
|
made to propagate allocators if needed |
| LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
Run this code
#include <iostream>
#include <iterator>
#include <string>
int main()
{
std::string s;
// assign(size_type count, CharT ch)
s.assign(4, '=');
std::cout << s << '\n'; // "===="
std::string const c("Exemplary");
// assign(basic_string const& str)
s.assign(c);
std::cout << c << "==" << s <<'\n'; // "Exemplary == Exemplary"
// assign(basic_string const& str, size_type pos, size_type count)
s.assign(c, 0, c.length()-1);
std::cout << s << '\n'; // "Exemplar";
// assign(basic_string&& str)
s.assign(std::string("C++ by ") + "example");
std::cout << s << '\n'; // "C++ by example"
// assign(charT const* s, size_type count)
s.assign("C-style string", 7);
std::cout << s << '\n'; // "C-style"
// assign(charT const* s)
s.assign("C-style\0string");
std::cout << s << '\n'; // "C-style"
char mutable_c_str[] = "C-style string";
// assign(InputIt first, InputIt last)
s.assign(std::begin(mutable_c_str), std::end(mutable_c_str)-1);
std::cout << s << '\n'; // "C-style string"
// assign(std::initializer_list<charT> ilist)
s.assign({ 'C', '-', 's', 't', 'y', 'l', 'e' });
std::cout << s << '\n'; // "C-style"
}
:
====
Exemplary==Exemplary
Exemplar
C++ by example
C-style
C-style
C-style string
C-style
basic_string () | |
| () |