std::basic_string<CharT,Traits,Allocator>::assign
提供: cppreference.com
< cpp | string | basic string
(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, |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string& assign( const T& t, |
(C++20以上) | |
文字列の内容を置き換えます。
1) 内容を文字
ch
のコピー count
個で置き換えます。2) 内容を
str
のコピーで置き換えます。 *this = str; と同等です。 特に、アロケータの伝播が行われる場合があります。 (C++11以上)3) 内容を
str
の部分文字列 [pos, pos+count)
で置き換えます。 要求された部分文字列が文字列の終端を超える場合、また��� count == npos の場合、結果の部分文字列は [pos, str.size())
になります。 pos > str.size() の場合は std::out_of_range が投げられます。4) ムーブセマンティクスを用いて、内容を
str
の内容で置き換えます。 *this = std::move(str) と同等です。 特に、アロケータの伝播が行われる場合があります。5) 内容を範囲
[s, s+count)
の文字のコピーで置き換えます。 この範囲はヌル文字を含むことができます。6) 内容を
s
の指すヌル終端文字列の内容で置き換えます。 文字列の長さは Traits::length(s) を用いて最初のヌル文字によって決定されます。7) 内容を範囲
[first, last)
の文字のコピーで置き換えます。 このオーバーロードは、 InputIt
が LegacyInputIterator を満たさない場合、オーバーロード解決に参加しません。 (C++11以上)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 を構築します (パブリックメンバ関数) | |
文字列に値を代入します (パブリックメンバ関数) |