Gice

Technology and General Blog

The dynamic array can be implemented by using a vector in C++. The aspects can be extra to the vector in different ways. The force_back again() functionality is a single of the methods to insert a new factor at the stop of the vector that raises the sizing of the vector by 1. This perform is handy when 1 component is essential to insert to the vector.  If the information variety of the vector does not guidance the benefit passed by the argument of this purpose, then an exception will be created, and no knowledge will be inserted. The way to insert information in vector working with the push_back again() perform has demonstrated in this tutorial.

Syntax:

vector::thrust_back(value_form n)

The worth of the n will be inserted at the conclude of the vector if the facts type of the vector supports the data type of the n. It returns almost nothing.

Pre-requisite:

Before checking the illustrations of this tutorial, you have to test the g++ compiler is set up or not in the process. If you are utilizing Visible Studio Code, then install the necessary extensions to compile the C++ resource code to create the executable code. Below, the Visual Studio Code software has been used to compile and execute the C++ code. Distinctive uses of the press_back again() purpose to insert aspect(s) into a vector have demonstrated in the future aspect of this tutorial.

Illustration-1: Introducing many aspects at the close of the vector

Generate a C++ file with the next code to insert multiple components at the stop of the vector utilizing the drive_again() functionality. A vector of three string values has been described in the code. The drive_back() purpose has been named three periods to insert 3 aspects at the finish of the vector. The material of the vector will be printed just before and soon after inserting the elements.

//Include things like needed libraries

#include

#include

making use of namespace std

int major()

        //Declare a vector of string values

        vector<string> birds = “Gray Parrot”, “Diamond Dove”, “Cocktail”

        cout << “The values of the vector before insert:n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < birds.size() ++i)

                cout << birds[i] << ” “

        cout << n

        /*

        Add three values at the end of the vectior

        using push_back() function

        */

        birds.push_back(“Mayna”)

        birds.push_back(“Budgies”)

        birds.push_back(“Cockatoo”)

        cout << “The values of the vector after insert:n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < birds.size() ++i)

                cout << birds[i] << ” “

        cout << n

        return 0

Output:

The following output will appear after executing the above code. The output shows that three new elements have been inserted at the end of the vector.

Example-2: Insert values into the vector by input

Create a C++ file with the following code to insert the element into an empty vector by taking values from the user and using the push_back() function. An empty vector of integer data type has been declared in the code. Next, a ‘for’ loop takes 5 numbers from the user and inserts the numbers into the vector using the push_back() function.  The content of the vector will be printed after the insertion.

//Include necessary libraries

#include

#contain

applying namespace std

int principal ()

        //Declare an integer vector

        vector<int> intVector

        //Declare an integer number

        int amount

        cout << “Enter 5 numbers: n

        /*

        Iterate the loop for 5 times to insert 5 integer values

        into the vector using push_back() function

        */
       
        for( int i=0 i < 5 i++)

                cin >> range

                intVector.drive_again (range)

       

        cout << “The values of the vector after insert:n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < intVector.size() ++i)

                cout << intVector[i] << ” “

        cout << n

        return 0

Output:

The following output will appear after executing the above code. The output shows that the five numbers taken from the user have been inserted into the vector.

Example-3: Insert values into vector-based on the specific condition

Create a C++ file with the following code to insert the specific numbers from an integer array into an empty vector. An empty vector and an array of 10 integer numbers have been declared in the code. The ‘for’ loop has been used to iterate each value of the array and insert the number into the vector using the push_back() function if the number is less than 30 or greater than 60. The content of the vector will be printed using the display_vector() function after the insertion.

//Include necessary libraries

#include

#include things like

working with namespace std

//Show the vector

void screen_vector(vector<int> nums)

        //Print the values of the vector using loop

        for(auto ele = nums.begin() ele != nums.conclude() ele++)

                cout << *ele << ” “

        //Add new line

        cout << n

int main ()

        //Declare an integer vector

        vector<int> intVector

        //Declare an array of numbers

        int myArray[10] = 9, 45, 13, 19, 30, 82, 71, 50, 35, 42

        /*

        Iterate the loop to read each and every ingredient of the array

        and insert those people values into the vector

        which are significantly less than 30 and greater than 60

        making use of press_again() function

        */

        for (int i= i < 10 i++) myArray[i] > 60)

                intVector.drive_again (myArray[i])

       

        cout << “The values of the vector after insert: “ << endl

        display_vector(intVector)

        return 0

Output:

The following output will appear after executing the above code. The output shows that the numbers 9, 13, 19,  82, and 71 have been inserted into the vector.

Conclusion:

Many functions exist in C++ to insert data at the beginning or ending or any particular position of the vector, such as push_front(), insert(), etc. Using the push_back() function will be cleared after practicing the examples shown in this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *