Vector Insert() Function in C++

The vector is a useful container class of C++ to store the sequence of data that works as a dynamic array. The size of the vector object can be increased or decreased by adding or removing an element in the object at the run time. The insert() function is used to add one or more new elements before the specific element of the vector object by mentioning the position of that element. It will increase the size of the vector object dynamically. The different syntax and the uses of this function have been explained in this tutorial.

Syntax:

The insert() function of the vector can be used in different ways for different purposes. Three different syntaxes of this function are mentioned below.

iterator insert (const_iterator position, const value_type& value);

The above insert() function is used to insert the value of the value argument before the position of the vector element mentioned by the position argument. It returns an iterator that points to the newly inserted element of the vector.

iterator insert (iterator position, size_type n, const value_type& value);

The above insert() function will work similar to the insert() function mentioned before, but it will insert the same value multiple times into the vector object.

iterator insert (const_iterator position, InputIterator first, InputIterator last);

The above insert() function will work insert the range of elements before the position of the vector element mentioned by the position argument. It returns an iterator that points to the newly inserted elements of the vector, like the previous two insert() functions.

Pre-requisite:

Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code. Different uses of the insert() function to insert element(s) into a vector have shown below.

Example-1: Inserting a single element

Create a C++ file with the following code to insert a single element using the insert() function.  A vector of 5 float numbers has been declared in the code. The first insert() function has been used to insert a number at the beginning of the vector by using begin() function. The second insert() function has been used to insert a number at the beginning of the vector by using the iterator variable. The third insert() function has to insert a number at the fourth position of the vector.

//Include necessary libraries

#include <iostream>

#include <vector>

using namespace std;

//Display the vector

void display(vector<float> nums)

{

        //Print the values of the vector using loop

        for(auto ele = nums.begin(); ele != nums.end() ; ele++)

        cout << *ele << " ";

        //Add new line

        cout << "\n";

}

int main()

{

        //Initialize the vector

        vector<float> price = { 78.56, 34.07, 23,45, 61.08, 29.3 };

        cout << "The original vector: ";

        display(price);

        //Insert the number at the front using begin()

        auto iterator = price.insert(price.begin(), 42.67);

        cout << "The vector after the first insert: ";

        display(price);

        //Insert the number at the front using iterator

        price.insert(iterator, 30.76);

        cout << "The vector after the second insert: ";

        display(price);

        //Initialize an integer variable

        int position = 3;

        //Insert the number at the particular position

        iterator = price.insert(price.begin() + position, 52.56);

        cout << "The vector after the third insert: ";

        display(price);

        return 0;

}

Output:

The following output will appear after executing the above code. The values of the vector have been printed four times in the output.

Example-2: Inserting single element multiple times

Create a C++ file with the following code to insert the same element multiple times in the vector using the insert() function with three parameters. A vector of 8 integer numbers has been declared in the code. The number 50 will be inserted 5 times at the end of the vector after executing the insert() function of the code. Here, the end() function has been used to insert elements at the end of the vector.

//Include necessary libraries

#include<iostream>

#include<vector>

using namespace std;

//Display the vector

void display(vector<int> nums)

{

        //Print the values of the vector using loop

        for(auto ele = nums.begin(); ele != nums.end() ; ele++)

                cout << *ele << " ";

        //Add new line

        cout << "\n";

}

int main()

{

        //Initialize the vector

        vector<int> intArray {7, 5, 12, 4, 45, 3, 64, 10};

        cout << "The original vector: ";

        display(intArray);

        //Insert the same number multiple times at the end of the vector

        intArray.insert(intArray.end(), 5, 50);

        cout << "The vector after inserting the same number 5 times : ";

        display(intArray);

        cout << "\n";

        return 0;

}

Output:

The following output will appear after executing the above code. The values of the vector have been printed two times in the output.

Example-3: Inserting the range of elements

Create a C++ file with the following code to insert all elements from a vector to the end of another vector. Here, the insert() function contains three parameters. The position of the insertion has set in the first parameter.  The starting and ending iterators of the second vector have been set in the second and the third argument of the insert() function.

//Include necessary libraries

#include<iostream>

#include<vector>

using namespace std;

//Define the function prototype

void display(vector<string> list);

int main()

{

        //Declare the first string vector

        vector<string> list1 {"html","css","javascript","bootstrap"};

        //Declare the second string vector

        vector<string> list2 {"php","java","python","bash","perl"};

        cout<<"The values of the list1: ";

        display(list1);

        cout<<"The values of the list2: ";

        display(list2);

        //Insert the values of list2 at the beginning of the list1

        list1.insert(list1.begin(),list2.begin(),list2.end());

        cout<<"The values of the list1 after inserting list2: ";

        display(list1);

        return 0;

}

//Display the vector

void display(vector<string> list)

{

        //Print the values of the vector using loop

        for(auto ele = list.begin(); ele != list.end() ; ele++)

        cout << *ele << " ";

        //Add new line

        cout << "\n";

}

Output:

The following output will appear after executing the above code. The values of the vector have been printed three times in the output.

Conclusion:

The ways to insert one or more elements in any position of the vector using the insert() function have been shown in this tutorial by using multiple examples. I hope the C++ coder will use the insert() function of the vector properly after reading this tutorial.



from Linux Hint https://ift.tt/3zIRLO0

Post a Comment

0 Comments