std::basic_string<CharT,Traits,Allocator>:: insert
|
basic_string
&
insert
(
size_type index, size_type count, CharT ch
)
;
|
(1) | (C++20부터 constexpr) |
|
basic_string
&
insert
(
size_type index,
const
CharT
*
s
)
;
|
(2) | (C++20부터 constexpr) |
|
basic_string
&
insert
(
size_type index,
const
CharT
*
s, size_type count
)
;
|
(3) | (C++20부터 constexpr) |
|
basic_string
&
insert
(
size_type index,
const
basic_string
&
str
)
;
|
(4) | (C++20부터 constexpr) |
| (5) | ||
|
basic_string
&
insert
(
size_type index,
const
basic_string
&
str,
size_type s_index, size_type count ) ; |
(C++14 이전) | |
|
basic_string
&
insert
(
size_type index,
const
basic_string
&
str,
size_type s_index, size_type count = npos ) ; |
(C++14 이후)
(C++20 이후 constexpr) |
|
| (6) | ||
|
iterator insert
(
iterator pos, CharT ch
)
;
|
(C++11 이전) | |
|
iterator insert
(
const_iterator pos, CharT ch
)
;
|
(C++11 이후)
(C++20 이후 constexpr) |
|
| (7) | ||
|
void
insert
(
iterator pos, size_type count, CharT ch
)
;
|
(C++11 이전) | |
|
iterator insert
(
const_iterator pos, size_type count, CharT ch
)
;
|
(C++11 이후)
(C++20 이후 constexpr) |
|
| (8) | ||
|
template
<
class
InputIt
>
void insert ( iterator pos, InputIt first, InputIt last ) ; |
(C++11 이전) | |
|
template
<
class
InputIt
>
iterator insert ( const_iterator pos, InputIt first, InputIt last ) ; |
(C++11 이후)
(C++20 이후 constexpr) |
|
|
iterator insert
(
const_iterator pos,
std::
initializer_list
<
CharT
>
ilist
)
;
|
(9) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
StringViewLike
>
basic_string & insert ( size_type index, const StringViewLike & t ) ; |
(10) |
(C++17부터)
(C++20부터 constexpr) |
|
template
<
class
StringViewLike
>
basic_string
&
insert
(
size_type index,
const
StringViewLike
&
t,
|
(11) |
(C++17부터)
(C++20부터 constexpr) |
문자열에 문자를 삽입합니다.
[
s
,
s
+
count
)
범위의 문자들을
index
위치에 삽입합니다. 해당 범위는 널 문자를 포함할 수 있습니다.
[
first
,
last
)
범위의 문자들을
pos
가 가리키는 요소(존재하는 경우) 앞에 삽입합니다. 마치
insert
(
pos
-
begin
(
)
, basic_string
(
first, last, get_allocator
(
)
)
)
를 호출한 것처럼 동작합니다.
|
이 오버로드는
|
(C++11부터) |
std:: basic_string_view < CharT, Traits >> 가 true 이고 std:: is_convertible_v < const StringViewLike & , const CharT * > 가 false 인 경우입니다.
[
t_index
,
t_index
+
count
)
에 해당하는 문자들을 삽입합니다.
-
요청된 부분 뷰가
sv
의 끝을 넘어서거나,
count
==
npos
인 경우, 결과 부분 뷰는
[t_index,sv. size ( ))가 됩니다. - 만약 t_index > sv. size ( ) 이거나 index > size ( ) 인 경우, std::out_of_range 가 발생합니다.
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
요구사항을 충족해야 함.
|
||
반환값
예외
모든 경우에, 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로 만들어 회피함 |
참고 항목
|
(C++23)
|
문자 범위를 삽입합니다
(public member function) |
|
끝에 문자를 추가합니다
(public member function) |
|
|
끝에 문자 하나를 추가합니다
(public member function) |