std::basic_string<CharT,Traits,Allocator>:: basic_string
| (1) | ||
|
basic_string
(
)
:
basic_string
(
Allocator
(
)
)
{
}
|
(C++11부터)
(C++17까지) |
|
|
basic_string
(
)
noexcept
(
noexcept
(
Allocator
(
)
)
)
: basic_string ( Allocator ( ) ) { } |
(C++17부터)
(C++20부터 constexpr) |
|
| (2) | ||
|
explicit
basic_string
(
const
Allocator
&
alloc
=
Allocator
(
)
)
;
|
(C++11 이전) | |
|
explicit
basic_string
(
const
Allocator
&
alloc
)
;
|
(C++17부터 noexcept)
(C++20부터 constexpr) |
|
|
basic_string
(
size_type count, CharT ch,
const Allocator & alloc = Allocator ( ) ) ; |
(3) | (constexpr since C++20) |
|
template
<
class
InputIt
>
basic_string
(
InputIt first, InputIt last,
|
(4) | (constexpr since C++20) |
|
template
<
container-compatible-range
<
CharT
>
R
>
constexpr
basic_string
(
std::
from_range_t
, R
&&
rg,
|
(5) | (C++23부터) |
|
basic_string
(
const
CharT
*
s, size_type count,
const Allocator & alloc = Allocator ( ) ) ; |
(6) | (constexpr C++20부터) |
|
basic_string
(
const
CharT
*
s,
const
Allocator
&
alloc
=
Allocator
(
)
)
;
|
(7) | (C++20부터 constexpr) |
|
basic_string
(
std::
nullptr_t
)
=
delete
;
|
(8) | (C++23 이후) |
|
template
<
class
StringViewLike
>
explicit
basic_string
(
const
StringViewLike
&
t,
|
(9) |
(C++17부터)
(C++20부터 constexpr) |
|
template
<
class
StringViewLike
>
basic_string
(
const
StringViewLike
&
t,
|
(10) | (C++17부터) |
|
basic_string
(
const
basic_string
&
other
)
;
|
(11) | (constexpr since C++20) |
|
basic_string
(
basic_string
&&
other
)
noexcept
;
|
(12) |
(C++11부터)
(C++20부터 constexpr) |
|
basic_string
(
const
basic_string
&
other,
const
Allocator
&
alloc
)
;
|
(13) |
(C++11 이후)
(C++20 이후 constexpr) |
|
basic_string
(
basic_string
&&
other,
const
Allocator
&
alloc
)
;
|
(14) |
(C++11부터)
(C++20부터 constexpr) |
|
basic_string
(
const
basic_string
&
other, size_type pos,
const Allocator & alloc = Allocator ( ) ) ; |
(15) | (C++20부터 constexpr) |
|
constexpr
basic_string
(
basic_string
&&
other, size_type pos,
const Allocator & alloc = Allocator ( ) ) ; |
(16) | (C++23 이후) |
|
basic_string
(
const
basic_string
&
other,
size_type pos, size_type count,
|
(17) | (constexpr since C++20) |
|
constexpr
basic_string
(
basic_string
&&
other,
size_type pos, size_type count,
|
(18) | (C++23 이후) |
|
basic_string
(
std::
initializer_list
<
CharT
>
ilist,
const Allocator & alloc = Allocator ( ) ) ; |
(19) |
(C++11부터)
(C++20부터 constexpr) |
다양한 데이터 소스로부터 새로운 문자열을 구성하며, 필요에 따라 사용자가 제공한 할당자 alloc 를 사용할 수 있습니다.
|
만약
|
(C++11부터) |
|
이 오버로드는
|
(C++17부터) |
[
first
,
last
)
의 내용으로 문자열을 생성합니다.
[
first
,
last
)
범위의 각 반복자는 정확히 한 번 역참조됩니다.
|
만약
|
(C++11 이전) |
|
이 오버로드는
만약
|
(C++11 이후) |
CharT
가
EmplaceConstructible
하지 않다면
std::
basic_string
<
CharT
>
에
*
ranges::
begin
(
rg
)
로부터, 동작은 정의되지 않습니다.
[
s
,
s
+
count
)
.
[
s
,
s
+
count
)
가 유효한 범위가 아닌 경우, 동작은 정의되지 않습니다.
|
이 오버로드는
|
(C++17부터) |
std::basic_string
는
nullptr
로부터 생성될 수 없습니다.
std:: basic_string_view < CharT, Traits >> 가 true 이고 std:: is_convertible_v < const StringViewLike & , const CharT * > 가 false 인 경우입니다.
std:: basic_string_view < CharT, Traits >> 가 true 인 경우에만 오버로드 해결에 참여합니다.
basic_string&&
인 경우, 생성이 완료되면
other
는 유효하지만 지정되지 않은 상태가 됩니다.
|
할당자는 마치
std::
allocator_traits
<
Allocator
>
::
|
(C++11부터) |
[
other.
data
(
)
+
pos
,
other.
data
(
)
+
other.
size
(
)
)
.
[
other.
data
(
)
+
pos
,
other.
data
(
)
+
(
pos
+
std::
min
(
count, other.
size
(
)
-
pos
)
)
)
.
목차 |
매개변수
| alloc | - | 이 문자열의 모든 메모리 할당에 사용할 할당자 |
| count | - | 결과 문자열의 크기 |
| ch | - | 문자열 초기화에 사용할 값 |
| pos | - | 포함할 첫 번째 문자의 위치 |
| first, last | - | 문자를 복사할 범위 |
| s | - | 문자열 초기화에 사용할 문자 배열의 포인터 |
| other | - | 문자열 초기화에 사용할 다른 문자열 |
| ilist | - | std::initializer_list 문자열 초기화에 사용할 |
| t | - | 객체 ( std::basic_string_view 로 변환 가능) 문자열 초기화에 사용할 |
| rg | - | 컨테이너 호환 범위 |
복잡도
예외
생성된 문자열의 길이가
std::length_error
를 발생시킵니다. 만약 생성된 문자열의 길이가
max_size()
를 초과하는 경우 (예를 들어
count
>
max_size
(
)
가
(3)
인 경우).
Allocator::allocate
호출은 예외를 발생시킬 수 있습니다.
어떤 이유로든 예외가 발생하면, 이 함수들은 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
참고 사항
문자열 리터럴
으로 초기화할 때 내장된
'
\0
'
문자를 포함하는 경우
(7)
번 오버로드를 사용하며, 이는 첫 번째 널 문자에서 중단됩니다. 다른 생성자를 지정하거나
operator""s
를 사용하여 이를 피할 수 있습니다:
std::string s1 = "ab\0\0cd"; // s1은 "ab"를 포함합니다 std::string s2{"ab\0\0cd", 6}; // s2는 "ab\0\0cd"를 포함합니다 std::string s3 = "ab\0\0cd"s; // s3는 "ab\0\0cd"를 포함합니다
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | 태그된 생성자 (5) 로부터 컨테이너 호환 범위 를 통한 생성 |
예제
#include <cassert> #include <cctype> #include <iomanip> #include <iostream> #include <iterator> #include <string> int main() { std::cout << "1) string(); "; std::string s1; assert(s1.empty() && (s1.length() == 0) && (s1.size() == 0)); std::cout << "s1.capacity(): " << s1.capacity() << '\n'; // 미지정 std::cout << "3) string(size_type count, CharT ch): "; std::string s2(4, '='); std::cout << std::quoted(s2) << '\n'; // "====" std::cout << "4) string(InputIt first, InputIt last): "; char mutable_c_str[] = "다른 C 스타일 문자열"; std::string s4(std::begin(mutable_c_str) + 8, std::end(mutable_c_str) - 1); std::cout << std::quoted(s4) << '\n'; // "C-style string" std::cout << "6) string(CharT const* s, size_type count): "; std::string s6("C-style string", 7); std::cout << std::quoted(s6) << '\n'; // "C-style", i.e. [0, 7) std::cout << "7) string(CharT const* s): "; std::string s7("C-style\0string"); std::cout << std::quoted(s7) << '\n'; // "C-style" std::cout << "11) string(string&): "; std::string const other11("예시"); std::string s11(other11); std::cout << std::quoted(s11) << '\n'; // "Exemplar" std::cout << "12) string(string&&): "; std::string s12(std::string("C++ by ") + std::string("example")); std::cout << std::quoted(s12) << '\n'; // "C++ by example" std::cout << "15) string(const string& other, size_type pos): "; std::string const other15("Mutatis Mutandis"); std::string s15(other15, 8); std::cout << std::quoted(s15) << '\n'; // "Mutandis", 즉 [8, 16) std::cout << "17) string(const string& other, size_type pos, size_type count): "; std::string const other17("모범적인"); std::string s17(other17, 0, other17.length() - 1); std::cout << std::quoted(s17) << '\n'; // "예시" std::cout << "19) string(std::initializer_list<CharT>): "; std::string s19({'C', '-', 's', 't', 'y', 'l', 'e'}); std::cout << std::quoted(s19) << '\n'; // "C-style" }
가능한 출력:
1) string(); s1.capacity(): 15 3) string(size_type count, CharT ch): "====" 4) string(InputIt first, InputIt last): "C-style string" 6) string(CharT const* s, size_type count): "C-style" 7) string(CharT const* s): "C-style" 11) string(string&): "Exemplar" 12) string(string&&): "C++ by example" 15) string(const string& other, size_type pos): "Mutandis" 17) string(const string& other, size_type pos, size_type count): "Exemplar" 19) string(std::initializer_list<CharT>): "C-style"
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 수정된 동작 |
|---|---|---|---|
| LWG 301 | C++98 |
오버로드
(4)
가 매개변수
alloc 를 사용하지 않음 (
InputIt
가 정수형일 경우)
|
해당 매개변수 사용 |
| LWG 438 | C++98 |
오버로드
(4)
가 오버로드
(3)
를 호출하는 경우
(
InputIt
가 정수형일 때만)
|
오버로드
(3)
호출 조건 변경
(
InputIt
가
LegacyInputIterator
가 아닐 때)
|
| LWG 847 | C++98 | 예외 안전성 보장이 없었음 | 강력한 예외 안전성 보장 추가 |
| LWG 2193 | C++11 | 기본 생성자가 explicit였음 | non-explicit로 변경 |
| LWG 2235 | C++98 | s 가 null 포인터 값일 수 있었음 | 이 경우 동작이 정의되지 않음 |
| LWG 2250 | C++98 |
오버로드
(17)
의 동작이 정의되지 않음
( pos > other. size ( ) 일 경우) |
이 경우 항상 예외 발생 |
| LWG 2583 | C++98 | 오버로드 (17) 에 할당자 제공 방법이 없었음 | 오버로드 (15) 추가 |
| LWG 2946 | C++17 | 오버로드 (9) 가 일부 경우에 모호성 발생 | 템플릿으로 변경하여 회피 |
| LWG 3076 | C++17 |
오버로드
(3,7)
이 클래스 템플릿 인수 추론에서
모호성 발생 가능 |
제약 조건 추가 |
|
LWG 3111
( P1148R0 ) |
C++98 |
LWG 이슈 2235
의 해결이
basic_string ( nullptr, 0 ) 의 동작을 정의되지 않게 만듦 |
이 경우 동작이 명확히
정의됨 [1] |
-
↑
[nullptr,nullptr + 0)는 유효한 빈 범위입니다. 왜냐하면 널 포인터 값에 0을 더하는 것도 잘 정의되어 있기 때문입니다(결과는 여전히 널 포인터 값입니다).
참고 항목
|
문자열에 문자들을 할당
(public member function) |
|
|
문자열에 값을 할당
(public member function) |
|
|
(C++11)
|
정수 또는 부동소수점 값을
string
으로 변환
(function) |
|
(C++11)
|
정수 또는 부동소수점 값을
wstring
으로 변환
(function) |
basic_string_view
를 생성
(
std::basic_string_view<CharT,Traits>
의 public member function)
|