Namespaces
Variants

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

From ko.cppreference.net
 
 
 
std::basic_string
 
basic_string& insert( size_type index, size_type count, CharT ch );
(1) (constexpr since C++20)
basic_string& insert( size_type index, const CharT* s );
(2) (constexpr since C++20)
basic_string& insert( size_type index, const CharT* s, size_type count );
(3) (constexpr since C++20)
basic_string& insert( size_type index, const basic_string& str );
(4) (constexpr since C++20)
basic_string& insert( size_type index, const basic_string& str,
                      size_type s_index, size_type count );
(5) (until C++14)
basic_string& insert( size_type index, const basic_string& str,
                      size_type s_index, size_type count = npos );
(since C++14)
(constexpr since C++20)
iterator insert( iterator pos, CharT ch );
(6) (until C++11)
iterator insert( const_iterator pos, CharT ch );
(since C++11)
(constexpr since C++20)
void insert( iterator pos, size_type count, CharT ch );
(7) (until C++11)
iterator insert( const_iterator pos, size_type count, CharT ch );
(since C++11)
(constexpr since C++20)
template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last );
(8) (until C++11)
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
(since C++11)
(constexpr since C++20)
iterator insert( const_iterator pos, std::initializer_list<CharT> ilist );
(9) (since C++11)
(constexpr since C++20)
template< class StringViewLike >
basic_string& insert( size_type index, const StringViewLike& t );
(10) (since C++17)
(constexpr since C++20)
template< class StringViewLike >
basic_string& insert( size_type index, const StringViewLike& t,
                      size_type t_index, size_type count = npos );
(11) (since C++17)
(constexpr since C++20)

문자열에 문자들을 삽입합니다.

1) 위치 count에 문자 ch의 index개 복사본을 삽입합니다.
2) 위치 s에 index이 가리키는 null 종료 문자 문자열을 삽입합니다. 문자열의 길이는 Traits::length(s)을 사용하여 첫 번째 null 문자로 결정됩니다.
3) 위치 [s, s + count)에 범위 index의 문자들을 삽입합니다. 범위는 null 문자를 포함할 수 있습니다.
4) 위치 str에 문자열 index을 삽입합니다.
5) 위치 str.substr(s_index, count)에서 index을 통해 얻은 문자열을 삽입합니다.
6) ch이 가리키는 문자 앞에 문자 pos를 삽입합니다.
7) count이 가리키는 요소(있는 경우) 앞에 문자 ch의 pos개 복사본을 삽입합니다.
8) [first, last)이 가리키는 요소(있는 경우) 앞에 범위 pos의 문자들을 insert(pos - begin(), basic_string(first, last, get_allocator()))을 사용하는 것처럼 삽입합니다.

이 오버로드는 InputIt이 LegacyInputIterator를 만족하지 않으면 오버로드 해결에 참여하지 않습니다.

(C++11부터)
9) ilist이 가리키는 요소(있는 경우) 앞에 초기화자 목록 pos의 요소들을 삽입합니다.
10) t을 sv을 사용하는 것처럼 문자열 뷰 std::basic_string_view<CharT, Traits> sv = t;로 암시적으로 변환한 다음, sv이 가리키는 요소(있는 경우) 앞에 index의 요소들을 insert(index, sv.data(), sv.size())을 사용하는 것처럼 삽입합니다.
이 오버로드는 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
이 true이고 std::is_convertible_v<const StringViewLike&, const CharT*>가 false인 경우에만 오버로드 해결에 참여합니다.
11) t을 sv을 사용하는 것처럼 문자열 뷰 std::basic_string_view<CharT, Traits> sv = t;로 암시적으로 변환한 다음, index이 가리키는 요소(있는 경우) 앞에 [t_index, t_index + count)의 하위 뷰 sv의 문자들을 삽입합니다.
  • 요청된 하위 뷰가 sv의 끝을 넘어가거나, count == npos인 경우 결과 하위 뷰는 [t_index, sv.size())입니다.
  • t_index > sv.size()이거나 index > size()이면 std::out_of_range가 발생합니다.
이 오버로드는 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
이 true이고 std::is_convertible_v<const StringViewLike&, const CharT*>가 false인 경우에만 오버로드 해결에 참여합니다.

pos이 *this에 대한 유효한 반복자가 아닌 경우, 동작은 정의되지 않습니다.

매개변수

index - 내용이 삽입될 위치
pos - 문자들이 삽입될 반복자 앞 위치
ch - 삽입할 문자
count - 삽입할 문자 개수
s - 삽입할 문자열에 대한 포인터
str - 삽입할 문자열
first, last - 삽입할 문자들을 정의하는 범위
s_index - str 에서 삽입할 첫 번째 문자의 위치
ilist - std::initializer_list 에서 삽입할 문자들
t - ( std::basic_string_view 로 변환 가능한) 문자들을 삽입할 객체
t_index - t 에서 삽입할 첫 번째 문자의 위치
타입 요구사항
-
InputIt 는 LegacyInputIterator 요구사항을 충족해야 함.

반환값

1-5) * this
6-9) 삽입된 첫 번째 문자의 복사본을 참조하는 반복자 또는 pos (문자가 삽입되지 않은 경우 - count == 0 또는 first == last 또는 ilist. size ( ) == 0 )
10,11) * this

예외

1-4,10) std::out_of_range 를 발생시킵니다, 만약 index > size ( ) 인 경우.
5) 다음의 경우 std::out_of_range 를 발생시킵니다: index > size ( ) 이거나 s_index > str. size ( ) 인 경우.
11) 다음 경우에 std::out_of_range 를 발생시킵니다: index > size ( ) 이거나 t_index > sv. size ( ) 인 경우.

모든 경우에, std::length_error 를 발생시킵니다. 만약 size ( ) + ins_count > max_size ( ) 인 경우, 여기서 ins_count 는 삽입될 문자들의 개수입니다.

모든 경우에, std:: allocator_traits < Allocator > :: allocate 가 예외를 던지면, 해당 예외가 재던져집니다.

(since C++20)

어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).

예제

#include <cassert>
#include <iterator>
#include <string>
using namespace std::string_literals;
int main()
{
    std::string s = "xmplr";
    // insert(size_type index, size_type count, char ch)
    s.insert(0, 1, 'E');
    assert("Exmplr" == s);
    // insert(size_type index, const char* s)
    s.insert(2, "e");
    assert("Exemplr" == s);
    // insert(size_type index, string const& str)
    s.insert(6, "a"s);
    assert("Exemplar" == s);
    // insert(size_type index, string const& str,
    //        size_type s_index, size_type count)
    s.insert(8, " is an example string."s, 0, 14);
    assert("Exemplar is an example" == s);
    // insert(const_iterator pos, char ch)
    s.insert(s.cbegin() + s.find_first_of('n') + 1, ':');
    assert("Exemplar is an: example" == s);
    // insert(const_iterator pos, size_type count, char ch)
    s.insert(s.cbegin() + s.find_first_of(':') + 1, 2, '=');
    assert("Exemplar is an:== example" == s);
    // insert(const_iterator pos, InputIt first, InputIt last)
    {
        std::string seq = " string";
        s.insert(s.begin() + s.find_last_of('e') + 1,
            std::begin(seq), std::end(seq));
        assert("Exemplar is an:== example string" == s);
    }
    // insert(const_iterator pos, std::initializer_list<char>)
    s.insert(s.cbegin() + s.find_first_of('g') + 1, {'.'});
    assert("Exemplar is an:== example string." == s);
}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 7 C++98 overload (8) 이 존재하지 않는 overload를 참조함 overload (4) 를 올바르게 참조함
LWG 847 C++98 예외 안전성 보장이 없었음 strong exception safety guarantee 추가됨
LWG 2946 C++17 overload (10) 가 일부 경우에 모호함을 야기함 template로 만들어 회피함

참고 항목

문자 범위를 삽입합니다
(public member function)
끝에 문자를 추가합니다
(public member function)
끝에 문자 하나를 추가합니다
(public member function)