std::basic_string::assign(3)
π std::basic_string::assign β the C++ Standard Library's glow-up button
ok so bestie, welcome to std::basic_string::assign, a page straight out of the C++ Standard Library and honestly? she's that girl. this is the member function that lets you replace the entire contents of a string in one move β no cap, it's giving "new year new me" but for basic_string. π¦
β¨ Synopsis β¨
no literally there are ten overloads, so buckle up, we're doing all of them π
basic_string& assign( size_type count, CharT ch ); // (1) (constexpr since C++20)
basic_string& assign( const basic_string& str ); // (2) (constexpr since C++20)
overload (3) had a glow-up, so here's both versions:
// (until C++14)
basic_string& assign( const basic_string& str,
size_type pos, size_type count );
// (since C++14) (constexpr since C++20)
basic_string& assign( const basic_string& str,
size_type pos, size_type count = npos );
// (since C++11) (constexpr since C++20)
basic_string& assign( basic_string&& str ) // (4)
noexcept(/* see below */);
basic_string& assign( const CharT* s, size_type count ); // (5) (constexpr since C++20)
basic_string& assign( const CharT* s ); // (6) (constexpr since C++20)
// (constexpr since C++20)
template< class InputIt >
basic_string& assign( InputIt first, InputIt last ); // (7)
// (since C++11) (constexpr since C++20)
basic_string& assign( std::initializer_list<CharT> ilist ); // (8)
// (since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& assign( const StringViewLike& t ); // (9)
// (since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& assign( const StringViewLike& t, // (10)
size_type pos, size_type count = npos );
Replaces the contents of the string. that's the whole vibe. π₯
π Description π (yes, all ten, i'm not skipping a single one)
- Replaces the contents with
countcopies of characterch. simple, cute, iconic. - Replaces the contents with a copy of
str. Equivalent to*this = str;. and heads up β in particular, allocator propagation may take place (since C++11). she's bringing her whole entourage, ok? - Replaces the contents with a substring
[pos, pos + count)ofstr. If the requested substring lasts past the end of the string, or ifcount == npos, the resulting substring is[pos, str.size()). Ifpos > str.size(),std::out_of_rangeis thrown. β οΈ bestie, watch thatpos. - Replaces the contents with those of
strusing move semantics. Equivalent to*this = std::move(str). In particular, allocator propagation may take place. she literally took everything and left. - Replaces the contents with copies of the characters in the range
[s, s + count). This range can contain null characters.\0girlies rise π - Replaces the contents with those of the null-terminated character string pointed to by
s. The length of the string is determined by the first null character usingTraits::length(s). - Replaces the contents with copies of the characters in the range
[first, last). This overload does not participate in overload resolution ifInputItdoes not satisfyLegacyInputIterator(since C++11). - Replaces the contents with those of the initializer list
ilist.{'i','c','o','n','i','c'}energy β¨ - Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with those ofsv, as if byassign(sv.data(), sv.size()). This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>istrueandstd::is_convertible_v<const StringViewLike&, const CharT*>isfalse. (iykyk π) - Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with the characters from the subview[pos, pos + count)ofsv. If the requested subview lasts past the end ofsv, or ifcount == npos, the resulting subview is[pos, sv.size()). Ifpos > sv.size(),std::out_of_rangeis thrown. This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>istrueandstd::is_convertible_v<const StringViewLike&, const CharT*>isfalse.
π«Ά Parameters π«Ά
countβ size of the resulting stringposβ index of the first character to takechβ value to initialize characters of the string withfirst,lastβ range to copy the characters fromstrβ string to be used as source to initialize the characters withsβ pointer to a character string to use as source to initialize the string withilistβstd::initializer_listto initialize the characters of the string withtβ object (convertible tostd::basic_string_view) to initialize the characters of the string with
πΈ Type requirements πΈ
InputItmust meet the requirements ofLegacyInputIterator.
π Return value π
*this
that's it. she returns herself. main character energy, honestly π
π Complexity π
- Linear in
count. - Linear in size of
str. - Linear in
count. - Constant. If
allocis given andalloc != other.get_allocator(), then linear. - Linear in
count. - Linear in size of
s. - Linear in distance between
firstandlast. - Linear in size of
ilist. - Linear in size of
t.
π Exceptions π
Overload 4) has this noexcept specification, and it's a whole mood:
noexcept(std::allocator_traits<Allocator>::
propagate_on_container_move_assignment::value ||
std::allocator_traits<Allocator>::is_always_equal::value)
If the operation would result in size() > max_size(), throws std::length_error.
π«Ά If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). yes bestie, she's atomic-ish about it. your string is safe.
π§ Example π§
// 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(const basic_string& str)
s.assign(c);
std::cout << c << " == " << s << '\n'; // "Exemplary == Exemplary"
// assign(const basic_string& 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(const CharT* s, size_type count)
s.assign("C-style string", 7);
std::cout << s << '\n'; // "C-style"
// assign(const CharT* 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"
}
β Output β
====
Exemplary == Exemplary
Exemplar
C++ by example
C-style
C-style
C-style string
C-style
(no bestie i don't know why there's a blank line either, but it's canon, i don't make the rules π )
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (yes, the committee went back and edited the group chat π)
LWG 847β applied to C++98 β behavior as published: there was no exception safety guarantee β correct behavior: added strong exception safety guaranteeLWG 2063β applied to C++11 β behavior as published: non-normative note stated thatswapis a valid implementation of move-assign β correct behavior: corrected to require move assignmentLWG 2579β applied to C++11 β behavior as published:assign(const basic_string&)did not propagate allocators β correct behavior: made to propagate allocators if neededLWG 2946β applied to C++17 β behavior as published: overload (9) caused ambiguity in some cases β correct behavior: avoided by making it a template
π See also π
assign_range(C++23) β assign a range of characters to a string (public member function)constructorβ constructs abasic_string(public member function)operator=β assigns values to the string (public member function)
π Category π
- conditionally
noexcept
that's the whole page bestie, go forth and reassign your strings with confidence π«Άβ¨
πΈ sourced fromhttp://cppreference.com, dated2024.06.10β this wasstd::basic_string::assign(3), and honestly? she ate. periodt. π π
π¦ 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