vector insert() Funkce v C++ STL

std::vector::insert() je vestavěná funkce v C++ STL, která vkládá nové prvky před prvek na zadanou pozici, čímž efektivně zvětšuje velikost kontejneru o počet vložených prvků.

Časová náročnost - Lineární, O(N)

Funkce vkládání je přetížena, aby fungovala ve více případech, které jsou následující:

  1. Vložte prvek na daný index.
  2. Vložte prvek vícekrát.
  3. Vložte rozsah prvků na daný index.

1. Vložte prvek do daného indexu

Syntaxe insert() ve Vector

vector_name.insert (position, val); 

Parametry

Funkce přijímá dva parametry specifikované níže:

  • pozice Určuje iterátor, který ukazuje na pozici, kde má být vložení provedeno.
  • val Určuje hodnotu, která se má vložit.

Příklad insert() ve vektoru

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ázev_vektoru{ 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)>

Výstup

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 více prvků na daný index

Syntaxe insert() ve Vector

vector_name.insert(position, size, val) 

Parametry

Funkce přijímá tři parametry specifikované níže:

  • pozice Určuje iterátor, který ukazuje na pozici, kde má být vložení provedeno.
  • velikost Určuje, kolikrát má být hodnota vložena na zadanou pozici.
  • val Určuje hodnotu, která se má vložit.

Příklad insert() ve 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> >název_vektoru{ 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)>

Výstup

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 prvků do daného indexu

Syntaxe Vector insert()

vector_name.insert(position, iterator1, iterator2) 

Parametry

Funkce přijímá tři parametry specifikované níže:

  • pozice Určuje polohu, ve které se má provést vložení do vektoru.
  • iterátor1 Určuje počáteční pozici, ze které mají být prvky vkládány
  • iterátor2 Určuje koncovou pozici, do které mají být prvky vloženy

Příklad funkce 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> >původní{ 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ýstup

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