- string[meta header]
- std[meta namespace]
- basic_string[meta class]
- function[meta id-type]
basic_string& insert(size_type pos1, const basic_string& str); // (1)
basic_string& insert(size_type pos1, const basic_string& str,
size_type pos2, size_type n); // (2) C++03
basic_string& insert(size_type pos1, const basic_string& str,
size_type pos2, size_type n = npos); // (2) C++14
basic_string& insert(size_type pos, const charT* s, size_type n); // (3)
basic_string& insert(size_type pos, const charT* s); // (4)
basic_string& insert(size_type pos, size_type n, charT c); // (5)
iterator insert(iterator p, charT c); // (6) C++03
iterator insert(const_iterator p, charT c); // (6) C++11
void insert(iterator p, size_type n, charT c); // (7) C++03
iterator insert(const_iterator p, size_type n, charT c); // (7) C++11
template<class InputIterator>
void insert(iterator p,
InputIterator first, InputIterator last); // (8) C++03
template<class InputIterator>
iterator insert(const_iterator p,
InputIterator first, InputIterator last); // (8) C++11
iterator insert(const_iterator p, initializer_list<charT>); // (9) C++11
basic_string& insert(size_type pos1,
std::basic_string_view<charT, traits> sv); // (10) C++17
basic_string& insert(size_type pos1,
std::basic_string_view<charT, traits> sv,
size_type pos2,
size_type n = npos); // (11) C++17- initializer_list[link /reference/initializer_list/initializer_list.md]
文字/文字列を挿入する。
- (1) :
pos <=size() - (2) :
pos1 <=size()およびpos2 <= str.size() - (3) : 文字配列へのポインタ
sが指す配列が少なくてもn要素あり、pos <=size()であること。 - (4) :
pos <=size()、および文字配列へのポインタsが、少なくてもtraits::length(s) + 1個の要素を指す配列を指していること。 - (6) : イテレータ
pが、*thisに対して有効であること。 - (7) : イテレータ
pが、*thisに対して有効であること。 - (8) : イテレータ
pが、*thisに対して有効であること。[first, last)が有効な範囲であること。
- (1) :
insert(pos, str.data(), str.size()) - (2) :
- (3) :
*thisのpos番目に、文字配列sの先頭n文字を挿入する。 - (4) :
insert(pos, s,traits::length(s))と等価の効果を持つ。 - (5) :
insert(pos, basic_string(n, c))と等価の効果を持つ。 - (6) : イテレータ
pが指す要素の前に、文字cのコピーを挿入する。 - (7) : イテレータ
pが指す要素の前に、文字cのコピーをn個挿入する。 - (8) :
insert(p -begin(), basic_string(first, last))と等価の効果を持つ。 - (9) :
insert(p, il.begin(), il.end()) - (10) :
return insert(pos1,sv.data(),sv.size())と等価の効果を持つ。 - (11) :
- (1) :
*this - (2) :
*this - (3) :
*this - (4) :
*this - (5) :
*this - (6) : 挿入された文字を指すイテレータを返す。
- (7) : 挿入された最初の文字を指すイテレータを返す。
n == 0ならpを返す。 - (8) : 挿入された最初の文字を指すイテレータを返す。
first == lastならpを返す。 - (10) :
*this - (11) :
*this
- (1) :
pos >size()の場合、out_of_range例外を送出する。 - (2) :
pos1 >size()もしくはpos2 > str.size()の場合、out_of_range例外を送出する。 - (3) :
pos >size()の場合、out_of_range例外を送出する。また、size()+ n >max_size()の場合にはlength_error例外を送出する。 - (11) :
pos1 >size()もしくはpos2 > sv.size()の場合、out_of_range例外を送出する。
#include <iostream>
#include <string>
int main()
{
// (1) 指定位置に文字列を挿入する
{
std::string s1 = "aaaaa";
std::string s2 = "bbbbb";
s1.insert(2, s2);
std::cout << "(1) : " << s1 << std::endl;
}
// (2) 指定位置に、部分文字列を挿入する
{
std::string s1 = "aaaaa";
std::string s2 = "12345";
// s2.substr(2, 3)を挿入する
s1.insert(2, s2, 2, 3);
std::cout << "(2) : " << s1 << std::endl;
}
// (3) 指定位置に、文字配列の先頭N文字を挿入する
{
std::string s = "aaaaa";
s.insert(2, "bbbbb", 3);
std::cout << "(3) : " << s << std::endl;
}
// (4) 指定位置に文字配列を挿入する
{
std::string s = "aaaaa";
s.insert(2, "bbbbb");
std::cout << "(4) : " << s << std::endl;
}
// (5) 指定位置に、N個の文字を挿入する
{
std::string s = "aaaaa";
s.insert(2, 3, 'b');
std::cout << "(5) : " << s << std::endl;
}
// (6) 指定したイテレータが指す要素の前に、文字を挿入する
{
std::string s = "aaaaa";
s.insert(s.begin(), 'b');
std::cout << "(6) : " << s << std::endl;
}
// (7) 指定したイテレータが指す要素の前に、N個の文字を挿入する
{
std::string s = "aaaaa";
s.insert(s.begin(), 3, 'b');
std::cout << "(7) : " << s << std::endl;
}
// (8) 指定したイテレータが指す要素の前に、文字の範囲を挿入する
{
std::string s1 = "aaaaa";
std::string s2 = "bbbbb";
s1.insert(s1.begin(), s2.begin(), s2.end());
std::cout << "(8) : " << s1 << std::endl;
}
// (9) 指定したイテレータが指す要素の前に、文字の初期化子リストを挿入する
{
std::string s = "aaaaa";
s.insert(s.begin(), {'b', 'b', 'b', 'b', 'b'});
std::cout << "(9) : " << s << std::endl;
}
// (10) 指定位置にbasic_string_viewが参照する文字列範囲を挿入する
{
std::string s1 = "aaaaa";
std::string_view sv2 = std::string_view{"CCCbbbbbDDD"}.substr(3, 5);
s1.insert(2, sv2);
std::cout << "(10) : " << s1 << std::endl;
}
// (11) 指定位置に、basic_string_viewの指定された範囲を挿入する
{
std::string s1 = "aaaaa";
std::string_view sv2 = "CCCbbbbbDDD";
s1.insert(2, sv2, 3, 5);
std::cout << "(11) : " << s1 << std::endl;
}
}- insert[color ff0000]
- begin()[link begin.md]
- end()[link end.md]
(1) : aabbbbbaaa
(2) : aa345aaa
(3) : aabbbaaa
(4) : aabbbbbaaa
(5) : aabbbaaa
(6) : baaaaa
(7) : bbbaaaaa
(8) : bbbbbaaaaa
(9) : bbbbbaaaaa
(10) : aabbbbbaaa
(11) : aabbbbbaaa