std::vector::insert() je vstavaná funkcia v C++ STL, ktorá vkladá nové prvky pred prvok na zadanú pozíciu, čím efektívne zväčšuje veľkosť kontajnera o počet vložených prvkov.
Časová zložitosť - Lineárne, O(N)
Funkcia vloženia je preťažená, aby fungovala vo viacerých prípadoch, ktoré sú nasledovné:
- Vložte prvok na daný index.
- Vložte prvok viackrát.
- Vložte rozsah prvkov na daný index.
1. Vložte prvok do daného indexu
Syntax funkcie insert() vo vektore
vector_name.insert (position, val);>
Parametre
Funkcia akceptuje dva parametre špecifikované nižšie:
- pozíciu – Špecifikuje iterátor, ktorý ukazuje na pozíciu, kde sa má vloženie vykonať.
- val – Určuje hodnotu, ktorá sa má vložiť.
Príklad insert() vo vektore
C++
// C++ program to illustrate the function of> // vector_name.insert(position,val)> #include> using> namespace> std;> > int> main()> {> > >// Initialising the vector> >vector<>int>>názov_vektora{ 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)> |
sieťovú architektúru
>
>Výkon
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. Vložte viacero prvkov do daného indexu
Syntax funkcie insert() vo vektore
vector_name.insert(position, size, val)>
Parametre
Funkcia akceptuje tri parametre špecifikované nižšie:
- pozíciu – Špecifikuje iterátor, ktorý ukazuje na pozíciu, kde sa má vloženie vykonať.
- veľkosť – Určuje, koľkokrát sa má vložiť val na určenú pozíciu.
- val – Určuje hodnotu, ktorá sa má vložiť.
Príklad insert() vo vektore
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>>názov_vektora{ 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)> |
>
vytváranie tabuliek v latexe
>Výkon
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. Vložte rozsah prvkov do daného indexu
Syntax funkcie Vector insert()
vector_name.insert(position, iterator1, iterator2)>
Parametre
Funkcia akceptuje tri parametre špecifikované nižšie:
- pozíciu – Špecifikuje polohu, v ktorej sa má vykonať vloženie do vektora.
- iterátor1 – Určuje počiatočnú pozíciu, z ktorej sa majú prvky vkladať
- iterátor2 – Určuje koncovú pozíciu, do ktorej sa majú vkladať prvky
Príklad funkcie 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>>originál{ 1, 2, 3, 4, 5 };> > >vector<>int>>teplota { 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)> |
>
>Výkon
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>