Question

In: Computer Science

In C++ please. 6. Define iterator invalidation. Explain the reason for this invalidation. Point out the...

In C++ please.

6. Define iterator invalidation. Explain the reason for this

invalidation. Point out the problem in the following code and correct it.

vector <int> v = {10, 20, 30, 40, 50, 60};

int i;

cout << "Enter number to remove from vector: "; cin >> i;

for(auto it = v.begin(); it != v.end(); ++it)

if(*it == i) v.erase(it);

Solutions

Expert Solution

iterator invalidation:

When iterating over a container(for example vector), if size/shape of the container changes due to addition or removing of elements, then iterator gets invalidated

Reason: When size of the container( vector ) crosses maximum size due to insertion or size of the vector reduces due to erasing of elements, then new memory is allocated and all the elements are moved to that location. This will cause ietrator pointing to a location where there ar no elements.

<int> v = {10, 20, 30, 40, 50, 60};

int i;

cout << "Enter number to remove from vector: "; cin >> i;

for(auto it = v.begin(); it != v.end(); ++it)

if(*it == i) v.erase(it);

The element next to the element chosen to be removed will get skipped from iteration. This is the problem

For example: if user enters 30. Then all the occurences of 30 should be removed from vector. Now when while iterating vector 30 is encountered in third iteration. So v.erase(it); will remove 30 from vector and changes iterator to point to 40 . Now due to ++it. iterator gets incremented and points to 50. So 40 gets skipped.

Fix: we need to decrement iterator after erasing element.

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector <int> v = {10, 20, 30, 30, 40, 60};
int i;
cout<<"Enter number to remove from vector: ";
cin>>i;
for(auto it = v.begin(); it != v.end(); ++it){
if(*it == i) {
v.erase(it);
it--;
}
}
for (auto it=v.begin();it!=v.end();it++)
cout << (*it) << " ";
return 0;
}


Related Solutions

The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Also, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
Please Type Answer or Dont Bother Answering! Define the term carrying capacity. Explain the reason every...
Please Type Answer or Dont Bother Answering! Define the term carrying capacity. Explain the reason every environment has a carrying capacity.
Please answer in c++ 6.Define a function to find a given target value in an array,...
Please answer in c++ 6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible. 7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters. 8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in...
In C++ please. 6. Define a callback. Name three types of callbacks used in STL algorithms....
In C++ please. 6. Define a callback. Name three types of callbacks used in STL algorithms. Given an container that contains integers. myCont Use count_if() algorithm and a lambda function as a callback to count the number of integers that are even. Explain the operation of your code.
In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates....
In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates. Give an example of a class template and a function template.
Mark as true or false and briefly explain the reason 6. If you have a problem...
Mark as true or false and briefly explain the reason 6. If you have a problem of simultaneous causality, you can address this by making sure that all possible omitted variables are added to your regression as control variables. 7. When you have data on the price and quantity sold of a good, an OLS regression of log quantity on log price will normally give you a consistent estimate of the price elasticity of demand for that good.
Mark as true or false and briefly explain the reason 6. If you have a problem...
Mark as true or false and briefly explain the reason 6. If you have a problem of simultaneous causality, you can address this by making sure that all possible omitted variables are added to your regression as control variables. 7. When you have data on the price and quantity sold of a good, an OLS regression of log quantity on log price will normally give you a consistent estimate of the price elasticity of demand for that good.
The documentary sale is utilized for many international transactions. Please explain (a) the reason for the...
The documentary sale is utilized for many international transactions. Please explain (a) the reason for the documentary sale, and (b) the steps involved in such a sale
Briefly explain the following terms: a)Exiting phenomena(drilling) b)Sparkling out process(grinding) c)Six point rules
Briefly explain the following terms: a)Exiting phenomena(drilling) b)Sparkling out process(grinding) c)Six point rules
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT