size_type pos, size_type count );
basic_string& assign( const StringViewLike& t,
Replaces the contents of the string.
Parameters Return value Complexity Exceptions Example Defect reports See also |
count | - | size of the resulting string |
pos | - | index of the first character to take |
ch | - | value to initialize characters of the string with |
first, last | - | range to copy the characters from |
str | - | string to be used as source to initialize the characters with |
s | - | pointer to a character string to use as source to initialize the string with |
ilist | - | to initialize the characters of the string with |
t | - | object (convertible to ) to initialize the characters of the string with |
Type requirements | ||
- must meet the requirements of . |
[ edit ] complexity, [ edit ] exceptions.
propagate_on_container_move_assignment :: 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 ).
[ edit ] defect reports.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++98 | there was no exception safety guarantee | added strong exception safety guarantee | |
C++11 | non-normative note stated that swap is a valid implementation of move-assign | corrected to require move assignment | |
C++11 | assign(const basic_string&) did not propagate allocators | made to propagate allocators if needed | |
C++17 | overload (9) caused ambiguity in some cases | avoided by making it a template |
assign a range of characters to a string (public member function) | |
constructs a (public member function) | |
assigns values to the string (public member function) |
(C++20) | ||||
(C++20) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++17) | ||||
General | ||||
Members | ||||
pointer | ||||
(C++11) | ||||
specifier | ||||
specifier | ||||
Special member functions | ||||
(C++11) | ||||
(C++11) | ||||
Inheritance | ||||
(C++11) | ||||
(C++11) |
A copy assignment operator of class T is a non-template non-static member function with the name operator = that takes exactly one parameter of type T , T & , const T & , volatile T & , or const volatile T & . For a type to be CopyAssignable , it must have a public copy assignment operator.
class_name class_name ( class_name ) | (1) | ||||||||
class_name class_name ( const class_name ) | (2) | ||||||||
class_name class_name ( const class_name ) = default; | (3) | (since C++11) | |||||||
class_name class_name ( const class_name ) = delete; | (4) | (since C++11) | |||||||
The copy assignment operator is called whenever selected by overload resolution , e.g. when an object appears on the left side of an assignment expression.
If no user-defined copy assignment operators are provided for a class type ( struct , class , or union ), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T & T :: operator = ( const T & ) if all of the following is true:
Otherwise the implicitly-declared copy assignment operator is declared as T & T :: operator = ( T & ) . (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument.)
A class can have multiple copy assignment operators, e.g. both T & T :: operator = ( const T & ) and T & T :: operator = ( T ) . If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default . (since C++11)
The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in dynamic exception specification (until C++17) exception specification (since C++17)
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
A implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true:
Otherwise, it is defined as defaulted.
A defaulted copy assignment operator for class T is defined as deleted if any of the following is true:
The copy assignment operator for class T is trivial if all of the following is true:
has no non-static data members of -qualified type. | (since C++14) |
A trivial copy assignment operator makes a copy of the object representation as if by std::memmove . All data types compatible with the C language (POD types) are trivially copy-assignable.
If the implicitly-declared copy assignment operator is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used . For union types, the implicitly-defined copy assignment copies the object representation (as by std::memmove ). For non-union class types ( class and struct ), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and copy assignment operator for class types.
The generation of the implicitly-defined copy assignment operator is deprecated (since C++11) if T has a user-declared destructor or user-declared copy constructor.
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either a prvalue such as a nameless temporary or an xvalue such as the result of std::move ), and selects the copy assignment if the argument is an lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
It is unspecified whether virtual base class subobjects that are accessible through more than one path in the inheritance lattice, are assigned more than once by the implicitly-defined copy assignment operator (same applies to move assignment ).
See assignment operator overloading for additional detail on the expected behavior of a user-defined copy-assignment operator.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++14 | operator=(X&) = default was non-trivial | made trivial |
IMAGES
VIDEO
COMMENTS
You can use the string literals (string constants) to initalize character arrays. This is why. person p = {"John", "Doe",30}; works in the first example. You cannot assign (in the conventional sense) a string in C. The string literals you have ("John") are loaded into memory when your code executes.
Just call either the data() or c_str() member functions of the std::string class, to get the char* pointer of the string object. The strcpy() function doesn't have overload to accept two std::string objects as parameters.
The easiest way to copy a string is to use the assignment operator (=) of the std::string class to copy the contents of one string to another. Note: There are no inbuilt classes in C, hence ‘=’ operator is only available in C++. Syntax. std::string copy = original; Example: Program to copy the string using std::string class (=) Operator
Copy assignment operator. A copy assignment operator is a non-template non-static member function with the name operator= that can be called with an argument of the same class type and copies the content of the argument without mutating the argument.
The strcpy() function copies the string pointed by source (including the null character) to the destination. The strcpy() function also returns the copied string. The strcpy() function is defined in the string.h header file.
Assigns a new value to the string, replacing its current contents. (1) string Copies str. (2) substring Copies the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either str is too short or if sublen is string::npos). (3) c-string
String assignment. The easiest way to assign a value to a string is to use the overloaded operator= function. There is also an assign () member function that duplicates some of this functionality. string& string::operator= (const string& str) string& string::assign (const string& str)
In this C programming example, you will learn to copy strings without using the strcpy() function.
assign a range of characters to a string (public member function) [edit] (constructor) constructs a basic_string(public member function) [edit] operator=. assigns values to the string (public member function) [edit] Retrieved from " ". Category: conditionally noexcept.
For non-union class types (class and struct), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and copy assignment operator for class types.