std::vector::insert() це вбудована функція в C++ STL, яка вставляє нові елементи перед елементом у вказаній позиції, фактично збільшуючи розмір контейнера на кількість вставлених елементів.
Складність часу – Лінійний, O(N)
Функція вставки перевантажена для роботи з кількома випадками:
- Вставити елемент за вказаним індексом.
- Вставте елемент кілька разів.
- Вставте діапазон елементів за вказаним індексом.
1. Вставте елемент за вказаним індексом
Синтаксис insert() у Vector
vector_name.insert (position, val);>
Параметри
Функція приймає два вказані нижче параметри:
- положення – Він визначає ітератор, який вказує на позицію, де має бути виконана вставка.
- вал – Він визначає значення, яке потрібно вставити.
Приклад insert() у векторі
C++
// C++ program to illustrate the function of> // vector_name.insert(position,val)> #include> using> namespace> std;> > int> main()> {> > > // Initialising the vector> > vector<> int> >ім'я_вектора{ 1, 2, 3, 4, 5 };> > > // Printing out the original vector> > cout <<> 'Original vector :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > cout <<> '
'> ;> > > // Inserting the value 100 at position 3(0-based> > // indexing) in the vector> > vector_name.insert(vector_name.begin() + 3, 100);> > > // Printing the modified vector> > cout <<> 'Vector after inserting 100 at position 3 :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > cout <<> '
'> ;> > > // Inserting the value 500 at position 1(0-based> > // indexing) in the vector> > vector_name.insert(vector_name.begin() + 1, 500);> > > // Printing the modified vector> > cout <<> 'Vector after inserting 500 at position 1 :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > return> 0;> }> > // This code is contributed by Abhijeet Kumar(abhijeet19403)> |
java викидає виняток
>
>Вихід
Original vector : 1 2 3 4 5 Vector after inserting 100 at position 3 : 1 2 3 100 4 5 Vector after inserting 500 at position 1 : 1 500 2 3 100 4 5>
2. Вставте кілька елементів за заданим індексом
Синтаксис insert() у Vector
vector_name.insert(position, size, val)>
Параметри
Функція приймає три параметри, указані нижче:
- положення – Він визначає ітератор, який вказує на позицію, де має бути виконана вставка.
- розмір – Він визначає кількість разів, коли значення буде вставлено у вказану позицію.
- вал – Він визначає значення, яке потрібно вставити.
Приклад insert() у Vector
C++
// C++ program to illustrate the function of> // vector_name.insert(position,size,val)> #include> using> namespace> std;> > int> main()> {> > > // Initialising the vector> > vector<> int> >ім'я_вектора{ 1, 2, 3, 4, 5 };> > > // Printing out the original vector> > cout <<> 'Original vector :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > cout << endl;> > > // Inserting the value 100, 4 times starting at position> > // 3> > vector_name.insert(vector_name.begin() + 3, 4, 100);> > > // Printing the modified vector> > cout <<> 'Vector after inserting 100, 4 times, starting '> > 'at position 3 :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > return> 0;> }> > // This code contributed by Harsh Singh (hsnooob)> |
>
структури даних в java
>Вихід
Original vector : 1 2 3 4 5 Vector after inserting 100, 4 times, starting at position 3 : 1 2 3 100 100 100 100 4 5>
3. Вставте діапазон елементів за заданим індексом
Синтаксис Vector insert()
vector_name.insert(position, iterator1, iterator2)>
Параметри
Функція приймає три вказані нижче параметри:
- положення – Він визначає позицію, у якій має бути виконана вставка у векторі.
- ітератор1 – Він визначає початкову позицію, з якої мають бути вставлені елементи
- ітератор2 – Він визначає кінцеву позицію, до якої потрібно вставити елементи
Приклад Vector insert()
C++
// C++ program to illustrate the function of> // vector_name.insert(position,itr1,itr2)> #include> using> namespace> std;> > int> main()> {> > > // Initialising the vector> > vector<> int> >оригінал{ 1, 2, 3, 4, 5 };> > > vector<> int> >temp{ 2, 5, 9, 0, 3, 10 };> > > // Printing out the original vector> > cout <<> 'Original vector :
'> ;> > for> (> auto> x : original)> > cout << x <<> ' '> ;> > cout << endl;> > > // Inserting the portion of temp vector in original> > // vector temp.begin()+3 is starting iterator of vector> > // to be copied temp.begin()+5 is ending iterator of> > // vector to be copied> > original.insert(original.begin() + 3, temp.begin() + 2,> > temp.begin() + 5);> > > // Printing the modified vector> > cout <<> 'Vector after Inserting the portion of temp '> > 'vector in original vector :
'> ;> > for> (> auto> x : original)> > cout << x <<> ' '> ;> > return> 0;> }> > // This code contributed by Harsh Singh (hsnooob)> |
>
>Вихід
Original vector : 1 2 3 4 5 Vector after Inserting the portion of temp vector in original vector : 1 2 3 9 0 3 4 5>