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...
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 = { _______ }
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,...
complete the following c puzzles using the listed operators. no data control structures allowed (for, if,...
complete the following c puzzles using the listed operators. no data control structures allowed (for, if, etc.) /* * logicalShift - shift x to the right by n, using a logic\ al shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321,4) = 0x08765432 * Legal ops: ! ~ & ^ | + << >> * Max ops: 20 * Rating: 3 */ int logicalShift(int x, int n) { } /* * bitCount - returns count of...
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).
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
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
please explain how does the following C code work. a) double ldexp(double x, int exponent) is...
please explain how does the following C code work. a) double ldexp(double x, int exponent) is a C math function that returns x multiplied by 2 raised to the power of exponent. How many narrowing and how many promotions happen automatically in the following code line? Name them one by one and explain each one separately. float f = 2.2f * ldexp(24.4, 3.0 * 2) - 4.4; b) int function1(int num1, int num2) { int num = num1 ^ num2;...
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT