Assignment Operator C++ String Find

class

<string>

std::string

typedef basic_string<char> string;

String class

Strings are objects that represent sequences of characters.

The standard class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters.

The class is an instantiation of the basic_string class template that uses (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).

Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).

Member types


Member functions

(constructor)
Construct string object (public member function)
(destructor)
String destructor (public member function)
operator=
String assignment (public member function)

Iterators:
begin
Return iterator to beginning (public member function)
end
Return iterator to end (public member function)
rbegin
Return reverse iterator to reverse beginning (public member function)
rend
Return reverse iterator to reverse end (public member function)
cbegin
Return const_iterator to beginning (public member function)
cend
Return const_iterator to end (public member function)
crbegin
Return const_reverse_iterator to reverse beginning (public member function)
crend
Return const_reverse_iterator to reverse end (public member function)

Capacity:
size
Return length of string (public member function)
length
Return length of string (public member function)
max_size
Return maximum size of string (public member function)
resize
Resize string (public member function)
capacity
Return size of allocated storage (public member function)
reserve
Request a change in capacity (public member function)
clear
Clear string (public member function)
empty
Test if string is empty (public member function)
shrink_to_fit
Shrink to fit (public member function)

Element access:
operator[]
Get character of string (public member function)
at
Get character in string (public member function)
back
Access last character (public member function)
front
Access first character (public member function)

Modifiers:
operator+=
Append to string (public member function)
append
Append to string (public member function)
push_back
Append character to string (public member function)
assign
Assign content to string (public member function)
insert
Insert into string (public member function)
erase
Erase characters from string (public member function)
replace
Replace portion of string (public member function)
swap
Swap string values (public member function)
pop_back
Delete last character (public member function)

String operations:
c_str
Get C string equivalent (public member function)
data
Get string data (public member function)
get_allocator
Get allocator (public member function)
copy
Copy sequence of characters from string (public member function)
find
Find content in string (public member function)
rfind
Find last occurrence of content in string (public member function)
find_first_of
Find character in string (public member function)
find_last_of
Find character in string from the end (public member function)
find_first_not_of
Find absence of character in string (public member function)
find_last_not_of
Find non-matching character in string from the end (public member function)
substr
Generate substring (public member function)
compare
Compare strings (public member function)

Member constants

npos
Maximum value for size_t (public static member constant)

Non-member function overloads

operator+
Concatenate strings (function)
relational operators
Relational operators for string (function)
swap
Exchanges the values of two strings (function)
operator>>
Extract string from stream (function)
operator<<
Insert string into stream (function)
getline
Get line from stream into string (function)

std::string::assign

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
Copies the null-terminated character sequence (C-string) pointed by s.
(4) buffer
Copies the first n characters from the array of characters pointed by s.
(5) fill
Replaces the current value by n consecutive copies of character c.
(6) range
Copies the sequence of characters in the range , in the same order.
(7) initializer list
Copies each of the characters in il, in the same order.
(8) move
Acquires the contents of str.
str is left in an unspecified but valid state.

Parameters

str
Another string object, whose value is either copied or moved.
subpos
Position of the first character in str that is copied to the object as a substring.
If this is greater than str's length, it throws out_of_range.
Note: The first character in str is denoted by a value of (not ).
sublen
Length of the substring to be copied (if the string is shorter, as many characters as possible are copied).
A value of string::npos indicates all characters until the end of str.
s
Pointer to an array of characters (such as a c-string).
n
Number of characters to copy.
c
Character value, repeated n times.
first, last
Input iterators to the initial and final positions in a range. The range used is , which includes all the characters between first and last, including the character pointed by first but not the character pointed by last.
The function template argument shall be an input iterator type that points to elements of a type convertible to .
If is an integral type, the arguments are casted to the proper types so that signature (5) is used instead.
il
An initializer_list object.
These objects are automatically constructed from initializer list declarators.

size_t is an unsigned integral type.

Return Value


Example



Output:

Complexity

Unspecified, but generally linear in the new string length (and constant for the move version).

Iterator validity

Any iterators, pointers and references related to this object may be invalidated.

Data races

The object is modified.
The move assign form (8), modifies .

Exception safety

For the move assign (8), the function does not throw exceptions (no-throw guarantee).
In all other cases, there are no effects in case an exception is thrown (strong guarantee).

If does not point to an array long enough, or if the range specified by is not valid, it causes undefined behavior.

If subpos is greater than str's length, an out_of_range exception is thrown.
If the resulting string length would exceed the max_size, a length_error exception is thrown.
A bad_alloc exception is thrown if the function needs to allocate storage and fails.

See also

string::operator=
String assignment (public member function)
string::append
Append to string (public member function)
string::insert
Insert into string (public member function)
string::replace
Replace portion of string (public member function)
string (1)string& assign (const string& str);
substring (2)string& assign (const string& str, size_t subpos, size_t sublen);
c-string (3)string& assign (const char* s);
buffer (4)string& assign (const char* s, size_t n);
fill (5)string& assign (size_t n, char c);
range (6)template <class InputIterator> string& assign (InputIterator first, InputIterator last);
string (1)string& assign (const string& str);
substring (2)string& assign (const string& str, size_t subpos, size_t sublen);
c-string (3)string& assign (const char* s);
buffer (4)string& assign (const char* s, size_t n);
fill (5)string& assign (size_t n, char c);
range (6)template <class InputIterator> string& assign (InputIterator first, InputIterator last);
initializer list(7)string& assign (initializer_list<char> il);
move (8)string& assign (string&& str) noexcept;
string (1)string& assign (const string& str);
substring (2)string& assign (const string& str, size_t subpos, size_t sublen = npos);
c-string (3)string& assign (const char* s);
buffer (4)string& assign (const char* s, size_t n);
fill (5)string& assign (size_t n, char c);
range (6)template <class InputIterator> string& assign (InputIterator first, InputIterator last);
initializer list(7)string& assign (initializer_list<char> il);
move (8)string& assign (string&& str) noexcept;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *