std::basic_string::assign
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
basic_string& assign( size_type count, CharT ch ); |
(1) | |
basic_string& assign( const basic_string& str ); |
(2) | |
| (3) | ||
basic_string& assign( const basic_string& str, size_type pos, size_type count ); |
(until C++14) | |
basic_string& assign( const basic_string& str, size_type pos, size_type count = npos); |
(since C++14) | |
basic_string& assign( basic_string&& str ); |
(4) | (since C++11) |
basic_string& assign( const CharT* s, size_type count ); |
(5) | |
basic_string& assign( const CharT* s ); |
(6) | |
template< class InputIt > basic_string& assign( InputIt first, InputIt last ); |
(7) | |
basic_string& assign( std::initializer_list<CharT> ilist ); |
(8) | (since C++11) |
문자열의 내용을 치환합니다.
1) 내용을
count개의 문자 ch로 치환합니다.2) 내용을
str의 복사본으로 치환합니다.3) 내용을
str의 부분 문자열 [pos, pos+count)로 치환합니다. 만약 요청된 부분 문자열이 문자열의 끝을 넘거나 count == npos인 경우에는, [pos, size())가 부분 문자열이 됩니다. pos > str.size()인 경우에는 std::out_of_range 예외가 발생합니다.4) Replaces the contents with those of
str using move semantics. str is in undefined state after the operation.5) Replaces the contents with the first
count characters of character string pointed to by s. s can contain null characters.6) Replaces the contents with those of null-terminated character string pointed to by
s. The length of the string is determined by the first null character. 7) Replaces the contents with copies of the characters in the range
[first, last). This overload does not participate in overload resolution if InputIt does not satisfy InputIterator. (since C++11)8) Replaces the contents with those of the initializer list
ilist.Parameters
| count | - | size of the resulting string |
| 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 |
| init | - | initializer list to initialize the characters of the string with |
| 형식의 필요조건 | ||
-InputIt 는 다음 조건을 만족해야 한다 : InputIterator.
| ||
Return value
*this
Complexity
1) linear in
count2) linear in size of
str3) linear in
count4) constant. If
alloc is given and alloc != other.get_allocator(), then linear.5) linear in
count6) linear in size of
s7) linear in distance between
first and last8) linear in size of
initExceptions
If an exception is thrown for any reason, this function has no effect (strong exception guarantee). (since C++11)
If the operation would result in size() > max_size(), throws std::length_error.
Example
코드 실행
#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 ") + std::string("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"
}
Output:
====
Exemplary==Exemplary
Exemplar
C++ by example
C-style
C-style
C-style string
C-style
See also
constructs a basic_string (public member function) | |
| assigns values to the string (public member function) |