Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: basic_string

From cppreference.net
std::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,

const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr since C++20)
template < container-compatible-range < CharT > R >

constexpr basic_string ( std:: from_range_t , R && rg,

const Allocator & = Allocator ( ) ) ;
(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,

const Allocator & alloc = Allocator ( ) ) ;
(9) (C++17부터)
(C++20부터 constexpr)
template < class StringViewLike >

basic_string ( const StringViewLike & t,
size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(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,

const Allocator & alloc = Allocator ( ) ) ;
(17) (constexpr since C++20)
constexpr basic_string ( basic_string && other,

size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(18) (C++23 이후)
basic_string ( std:: initializer_list < CharT > ilist,
const Allocator & alloc = Allocator ( ) ) ;
(19) (C++11부터)
(C++20부터 constexpr)

다양한 데이터 소스로부터 새로운 문자열을 구성하며, 필요에 따라 사용자가 제공한 할당자 alloc 를 사용할 수 있습니다.

1) C++11부터 도입된 기본 생성자. 기본 생성된 할당자와 함께 빈 문자열을 생성합니다.
Allocator가 DefaultConstructible 가 아닌 경우, 동작은 정의되지 않습니다.
2) C++11 이전의 기본 생성자. 주어진 할당자 alloc 로 빈 문자열을 생성합니다.
3) 문자 ch count 개 복사한 문자열을 생성합니다.

만약 CharT CopyInsertable 요구사항을 std:: basic_string < CharT > 에 만족하지 않을 경우, 동작은 정의되지 않습니다.

(C++11부터)

이 오버로드는 Allocator Allocator 요구사항을 만족할 때만 오버로드 해결에 참여합니다.

(C++17부터)
4) 범위 [ first , last ) 의 내용으로 문자열을 생성합니다. [ first , last ) 범위의 각 반복자는 정확히 한 번 역참조됩니다.

만약 InputIt LegacyInputIterator 요구사항을 만족하지 않으면, (3) 번 오버로드가 인자 static_cast < size_type > ( first ) , last , alloc 와 함께 대신 호출됩니다.

(C++11 이전)

이 오버로드는 InputIt LegacyInputIterator 요구사항을 만족할 때만 오버로드 해결에 참여합니다.

만약 CharT * first 로부터 std:: basic_string < CharT > EmplaceConstructible 되지 않으면, 동작은 정의되지 않습니다.

(C++11 이후)
5) 범위 rg 의 내용으로 문자열을 생성합니다. rg 의 각 반복자는 정확히 한 번 역참조됩니다.
만약 CharT EmplaceConstructible 하지 않다면 std:: basic_string < CharT > * ranges:: begin ( rg ) 로부터, 동작은 정의되지 않습니다.
6) 범위의 내용으로 문자열을 생성합니다 [ s , s + count ) .
만약 [ s , s + count ) 가 유효한 범위가 아닌 경우, 동작은 정의되지 않습니다.
7) 다음과 동일함: basic_string ( s, Traits :: length ( s ) , alloc ) .

이 오버로드는 Allocator Allocator 요구 사항을 충족할 때만 오버로드 해결에 참여합니다.

(C++17부터)
8) std::basic_string nullptr 로부터 생성될 수 없습니다.
9) 암시적으로 t 를 문자열 뷰 sv 로 변환하며, 마치 std:: basic_string_view < CharT, Traits > sv = t ; 와 같이 수행한 후, 문자열을 마치 basic_string ( sv. data ( ) , sv. size ( ) , alloc ) 와 같이 생성합니다.
이 오버로드는 다음 조건이 충족될 때만 오버로드 해결에 참여합니다: std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true 이고 std:: is_convertible_v < const StringViewLike & , const CharT * > false 인 경우입니다.
10) 암시적으로 t 를 문자열 뷰 sv 로 변환하며, 마치 std:: basic_string_view < CharT, Traits > sv = t ; 와 같이 수행한 후, 문자열을 마치 basic_string ( sv. substr ( pos, n ) , alloc ) 와 같이 생성합니다.
이 오버로드는 다음 조건이 std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true 인 경우에만 오버로드 해결에 참여합니다.
11-18) other 의 내용(일부)을 사용하여 문자열을 생성합니다. other 의 타입이 basic_string&& 인 경우, 생성이 완료되면 other 는 유효하지만 지정되지 않은 상태가 됩니다.
11) 복사 생성자.

할당자는 마치 std:: allocator_traits < Allocator > ::
select_on_container_copy_construction
( other. get_allocator ( ) )
을 호출하는 것처럼 얻어집니다.

(C++11부터)
12) 이동 생성자. 할당자는 other. get_allocator ( ) 로부터 이동 생성으로 얻어집니다.
13) 복사 생성자와 동일하지만, alloc 이 할당자로 사용된다는 점이 다릅니다.
만약 CharT CopyInsertable 하지 않다면 std:: basic_string < CharT > 에 대해, 동작은 정의되지 않습니다.
14) 이동 생성자와 동일하지만, alloc 이 할당자로 사용된다는 점이 다릅니다.
만약 CharT MoveInsertable 하지 않다면 std:: basic_string < CharT > 에 대해, 동작은 정의되지 않습니다.
15,16) 범위의 내용으로 문자열을 생성합니다 [ other. data ( ) + pos , other. data ( ) + other. size ( ) ) .
17,18) 범위의 내용으로 문자열을 생성합니다 [ other. data ( ) + pos , other. data ( ) + ( pos + std:: min ( count, other. size ( ) - pos ) ) ) .
19) 다음과 동등함: basic_string ( ilist. begin ( ) , ilist. end ( ) ) .

목차

매개변수

alloc - 이 문자열의 모든 메모리 할당에 사용할 할당자
count - 결과 문자열의 크기
ch - 문자열 초기화에 사용할 값
pos - 포함할 첫 번째 문자의 위치
first, last - 문자를 복사할 범위
s - 문자열 초기화에 사용할 문자 배열의 포인터
other - 문자열 초기화에 사용할 다른 문자열
ilist - std::initializer_list 문자열 초기화에 사용할
t - 객체 ( std::basic_string_view 로 변환 가능) 문자열 초기화에 사용할
rg - 컨테이너 호환 범위

복잡도

1,2) 상수.
3-7) 문자열의 크기에 선형적입니다.
9-11) 문자열의 크기에 선형적입니다.
12) 상수.
13) 문자열의 크기에 선형적입니다.
14) 문자열의 크기에 선형적으로 비례하는 시간이 소요됩니다. 단, alloc ! = other. get_allocator ( ) true 인 경우에 해당하며, 그렇지 않을 경우 상수 시간이 소요됩니다.
15-19) 문자열의 크기에 선형적입니다.

예외

10) std::out_of_range 만약 pos 가 범위를 벗어난 경우.
14) alloc == str. get_allocator ( ) true 인 경우 아무 것도 throw하지 않습니다.
15-18) std::out_of_range 만약 pos > other. size ( ) true 인 경우.

생성된 문자열의 길이가 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]
  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)