Question

In C++ please. 8. Explain what insert_iterator and inserter() function is. Given the following data structures:...

In C++ please.

8. Explain what insert_iterator and inserter() function is.
   Given the following data structures:

     vector<int> v1 = {1, 2, 3, 4, 5};
     vector<int> v2;
 
   Explain the problem with the below code and fix it with inserter()
   function.

        copy(v1.begin(), v1.end(), v2.begin());   

Homework Answers

Answer #1

8). ANSWER :

GIVENTHAT :

std::inserter constructs an insert iterator that inserts new elements into x in successive locations starting at the position pointed by it. It is defined inside the header file .

An insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (such as copy) to instead insert new elements automatically at a specific position in the container.

Syntax:

std::inserter(Container& x, typename Container::iterator it);
x: Container in which new elements will 
be inserted.
it: Iterator pointing to the insertion point.

Returns: An insert_iterator that inserts elements into 
x at the position indicated by it.

Inserting values anywhere : Now, just imagine, if we had to copy value into a container such as a vector, firstly, we had to move elements and then copy, but with the help of std::insert() we can insert at any position with ease.

1.One of the pitfalls of std::inserter is that it can be used with only those containers that have to insert as one of its methods like in case of vector,list and dequeue etc...

2.insert() vs std::insterter():Now, you may be thinking that insert() and std::inserter() are similar,but they are not.When you have to pass an iterator in the algorihtm,then you should use inserter() like in above case,while for normally inserting the values ine the container,insert() and be used.

3.In place of using std::inserter,we can create a insert_iterator and then use it,as eventually,std::inserter returns a insert_iterator only.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C++ please. 1. Explain what default parameter values are. Describe "function overloading" and "function call...
In C++ please. 1. Explain what default parameter values are. Describe "function overloading" and "function call resolution". Simplify the following code using default parameter values. int *setUpArray(int size, int value){ int *a = new int[size]; for (int i=0; i < size; ++i) a[i] = value; return a; } int *setUpArray(int size){ int *a = new int[size]; for (int i=0; i < size; ++i) a[i] = 0; return a; } int *setUpArray(){ int *a = new int[100]; for (int i=; i...
Please complete the following function getMax, which accepts three integer parameters (int p, int q, int...
Please complete the following function getMax, which accepts three integer parameters (int p, int q, int r), and must return the maximum of those 3 integers received to the caller. int getMax(int p, int q, int r) // returns the max of 3 integers { // begin getMax() // Enter your code here:::::::::::::::::::::::::::::      } // end getMax() void main( ) // To test function getMax( ) { cout << "Data: 9 18 4 , Max = " << getMax(9,...
Let H=Span{v1,v2} and K=Span{v3,v4}, where v1,v2,v3,v4 are given below. v1 = [3 2 5], v2 =[4...
Let H=Span{v1,v2} and K=Span{v3,v4}, where v1,v2,v3,v4 are given below. v1 = [3 2 5], v2 =[4 2 6], v3 =[5 -1 1], v4 =[0 -21 -9] Then H and K are subspaces of R3 . In fact, H and K are planes in R3 through the origin, and they intersect in a line through 0. Find a nonzero vector w that generates that line. w = { _______ }
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid...
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid populated by the alternating dots and stars (see example below). The odd rows contain stars and even rows contain dots. Debug the code to fix all the compilation and run-time errors, so that the code generates the desired output. For instance, when the 'n' value passed to the function is 6, the output would look like the following. ****** ..... **** ... ** ....
1) Explain the problem with the following code segment and suggest a way to fix it....
1) Explain the problem with the following code segment and suggest a way to fix it. int i = 42; int *p1, *p2; p1 = &i; *p2 = *p1; 2) What are the Invariants for the bag class that uses a static array?  (Hints: explain the functionality of the class’s private member variables – used and data).
Let A = v1 v2 v3 v1 2 8 16 v2 8 0 4 v3 16...
Let A = v1 v2 v3 v1 2 8 16 v2 8 0 4 v3 16 4 1 be the ADJACENCY MATRIX for an undirected graph G. Solve the following: 1) Determine the number of edges of G 2) Determine the total degree of G 3) Determine the degree of each vertex of G 4) Determine the number of different walks of length 2 from vertex v3 to v1 5) Does G have an Euler circuit? Explain
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
What will the following Arduino code do? Please explain in a few words.    In this...
What will the following Arduino code do? Please explain in a few words.    In this setup, a motor is connected to an H-bridge, which is controlled by pin11 and pin 10. A distance sensor is connected to pin 2. Here is the code: int  motorPin1 = 11; // Variables defined before setup and loop functions can be used anywhere in any function.      int  motorPin2 = 10;    int distance_sensor_pin = 2;   long int pulse_duration, distance_in_inches;   void setup() {     // The...
What will the following Arduino code do? Please explain in a few words. In this setup,...
What will the following Arduino code do? Please explain in a few words. In this setup, a motor is connected to an H-bridge, which is controlled by pin11 and pin 10. A distance sensor is connected to pin 2. Here is the code: int  motorPin1 = 11; // Variables defined before setup and loop functions can be used anywhere in any function.      int  motorPin2 = 10;    int distance_sensor_pin = 2;   long int pulse_duration, distance_in_inches;   void setup() {     // The setup...
Must be written in c++: This problem solely involves your demonstration of the proper usage of...
Must be written in c++: This problem solely involves your demonstration of the proper usage of C++ STL std::list<int> Below you will be given four total functions to develop (two are listed here, two separately after) Put all four of your functions in the same single source code file Submit ONLY your one source code CPP file online Submitting two or three or more separate CPP files will lose points (1) The first function you will need to implement is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT