std::vector::insert() je vgrajena funkcija v C++ STL, ki vstavi nove elemente pred element na določenem mestu, s čimer dejansko poveča velikost vsebnika za število vstavljenih elementov.
Časovna zapletenost – Linearno, O(N)
Funkcija vstavljanja je preobremenjena za delo v več primerih, ki so naslednji:
- Vstavite element na podani indeks.
- Večkrat vstavite element.
- Vstavite obseg elementov na podani indeks.
1. Vstavite element na podani indeks
Sintaksa funkcije insert() v Vector
vector_name.insert (position, val);>
Parametri
Funkcija sprejme dva parametra, navedena spodaj:
- položaj – Določa iterator, ki kaže na položaj, kjer je treba izvesti vstavljanje.
- val – Določa vrednost, ki jo je treba vstaviti.
Primer insert() v vektorju
C++
// C++ program to illustrate the function of> // vector_name.insert(position,val)> #include> using> namespace> std;> > int> main()> {> > >// Initialising the vector> >vector<>int>>ime_vektorja{ 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)> |
omrežna arhitektura
>
>Izhod
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. Vstavite več elementov na podani indeks
Sintaksa funkcije insert() v Vector
vector_name.insert(position, size, val)>
Parametri
Funkcija sprejme tri parametre, navedene spodaj:
- položaj – Določa iterator, ki kaže na položaj, kjer je treba izvesti vstavljanje.
- velikost – Podaja, kolikokrat naj bo val vstavljen na podano mesto.
- val – Določa vrednost, ki jo je treba vstaviti.
Primer insert() v 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>>ime_vektorja{ 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)> |
>
>Izhod
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. Vstavite obseg elementov pri danem indeksu
Sintaksa Vector insert()
vector_name.insert(position, iterator1, iterator2)>
Parametri
Funkcija sprejme tri spodaj navedene parametre:
- položaj – Določa položaj, na katerem je treba izvesti vstavljanje v vektor.
- iterator1 – Določa začetni položaj, iz katerega naj se vstavijo elementi
- iterator2 – Določa končni položaj, do katerega bodo elementi vstavljeni
Primer Vector insert()
C++
spanje za javascript
// C++ program to illustrate the function of> // vector_name.insert(position,itr1,itr2)> #include> using> namespace> std;> > int> main()> {> > >// Initialising the vector> >vector<>int>>original { 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)> |
>
>Izhod
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>