std :: vecteur :: insert () est une fonction intégrée en C++ STL qui insère de nouveaux éléments avant l'élément à la position spécifiée, augmentant ainsi la taille du conteneur du nombre d'éléments insérés.
Complexité temporelle – Linéaire, O(N)
La fonction insert est surchargée pour travailler sur plusieurs cas qui sont les suivants :
- Insère un élément à l'index donné.
- Insérez un élément plusieurs fois.
- Insère une plage d'éléments à l'index donné.
1. Insérer un élément à l'index donné
Syntaxe de insert() dans Vector
vector_name.insert (position, val);>
Paramètres
La fonction accepte deux paramètres spécifiés ci-dessous :
- position – Il spécifie l'itérateur qui pointe vers la position où l'insertion doit être effectuée.
- Val – Il précise la valeur à insérer.
Exemple de insert() dans un vecteur
C++
// C++ program to illustrate the function of> // vector_name.insert(position,val)> #include> using> namespace> std;> > int> main()> {> > >// Initialising the vector> >vector<>int>>nom_vecteur{ 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)> |
10 sur 100,00
>
>Sortir
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. Insérer plusieurs éléments à un index donné
Syntaxe de insert() dans Vector
vector_name.insert(position, size, val)>
Paramètres
La fonction accepte trois paramètres spécifiés comme ci-dessous :
- position – Il spécifie l'itérateur qui pointe vers la position où l'insertion doit être effectuée.
- taille – Il spécifie le nombre de fois qu'un val doit être inséré à la position spécifiée.
- Val – Il précise la valeur à insérer.
Exemple de insert() dans 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>>nom_vecteur{ 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)> |
>
>
j'essaie d'entrerSortir
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. Insérer la plage d'éléments à l'index donné
Syntaxe de Vector insert()
vector_name.insert(position, iterator1, iterator2)>
Paramètres
La fonction accepte trois paramètres spécifiés ci-dessous :
- position – Il précise la position à laquelle l'insertion doit être effectuée dans le vecteur.
- itérateur1 – Il précise la position de départ à partir de laquelle les éléments doivent être insérés
- itérateur2 – Il spécifie la position finale jusqu'à laquelle les éléments doivent être insérés
Exemple d'insertion vectorielle()
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>>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)> |
>
>Sortir
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>