std::vector::insert() to wbudowana funkcja w języku C++ STL, która wstawia nowe elementy przed elementem w określonej pozycji, skutecznie zwiększając rozmiar kontenera o liczbę wstawionych elementów.
Złożoność czasu – Liniowy, O(N)
Funkcja wstawiania jest przeciążona, aby działać na wielu przypadkach, które są następujące:
- Wstaw element o podanym indeksie.
- Wstaw element wielokrotnie.
- Wstaw zakres elementów o podanym indeksie.
1. Wstaw element pod podanym indeksem
Składnia wstawki() w wektorze
vector_name.insert (position, val);>
Parametry
Funkcja przyjmuje dwa parametry określone poniżej:
- pozycja – Określa iterator wskazujący pozycję, w której ma zostać wykonane wstawienie.
- wal – Określa wartość, która ma zostać wstawiona.
Przykład funkcji wstawki() w wektorze
C++
// C++ program to illustrate the function of> // vector_name.insert(position,val)> #include> using> namespace> std;> > int> main()> {> > >// Initialising the vector> >vector<>int>>nazwa_wektora{ 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)> |
nazwa miasta w USA
>
>Wyjście
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. Wstaw wiele elementów pod podanym indeksem
Składnia wstawki() w wektorze
vector_name.insert(position, size, val)>
Parametry
Funkcja przyjmuje trzy parametry określone jak poniżej:
- pozycja – Określa iterator wskazujący pozycję, w której ma zostać wykonane wstawienie.
- rozmiar – Określa, ile razy wartość ma zostać wstawiona w określonej pozycji.
- wal – Określa wartość, która ma zostać wstawiona.
Przykład funkcji Insert() w Vectorze
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>>nazwa_wektora{ 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)> |
>
>
częściowo pochodny lateksWyjście
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. Wstaw zakres elementów o podanym indeksie
Składnia wstawki wektorowej()
vector_name.insert(position, iterator1, iterator2)>
Parametry
Funkcja przyjmuje trzy parametry określone poniżej:
- pozycja – Określa pozycję, w której ma zostać wykonane wstawienie w wektorze.
- iterator1 – Określa pozycję wyjściową, od której mają zostać wstawione elementy
- iterator2 – Określa pozycję końcową, do której mają zostać wstawione elementy
Przykład wstawki wektorowej()
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>>oryginał{1, 2, 3, 4, 5 };> > >vector<>int>>temperatura{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)> |
>
>Wyjście
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>