Question

Consider this code below. If this is correct, explain how it works. If it is not...

Consider this code below. If this is correct, explain how it works. If it is not correct, state the problem and provide a fix.

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

cout << "Enter number to remove from vector";

int toRemove; cin >> toRemove;

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

if(*it == toRemove)

v.erase(it);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

vector<int> v{10, 20, 10, 20, 30, 10 };

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

if(*it == toRemove)

v.erase(it);

Add a comment
Know the answer?
Add Answer to:
Consider this code below. If this is correct, explain how it works. If it is not...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Vectors. This lab is to be done using Unix. To familiarize yourself with Unix, read this...

    Vectors. This lab is to be done using Unix. To familiarize yourself with Unix, read this tutorial. To prove that you used Unix tools, as part of your project submission, you need to submit a typescript file with the record of at least one successful compilation of the second project programs below (the execution of the compiler on your source code file). Create a project titled Lab12_Vectors. Implement the dynamically expanding and contracting container functionality described in previous labs using...

  • In C++ please. 8. Explain why modifying STL algorithms such as remove () and unique ()...

    In C++ please. 8. Explain why modifying STL algorithms such as remove () and unique () do not actually eliminate the unsuitable elements from the container. Describe remove-erase idiom. Assuming that "v" is a vector of integers. Explain what the below code does. v.erase(remove_if(v.begin(), v.end(), [](int i){return i > 5;}), v.end());

  • Write code in main that will read in 6 ints and store them into the list...

    Write code in main that will read in 6 ints and store them into the list values. When you read a positive number it should be added to the back of the list. When you read a negative number, it should be inserted at the front of the list. Example: Given input 5 2 -4 3 -1 6, you would build up: 5 5 2 -4 5 2 -4 5 2 3 -1 -4 5 2 3 -1 -4 5...

  • Please, explain clearly each line of code with your answers. Question 1 Consider the following code...

    Please, explain clearly each line of code with your answers. Question 1 Consider the following code snippet: int ctr = 0; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr; ctr = ctr + i; } cout << myarray[2]; What is the output of the code snippet? Question 2 Consider the following code snippet: int cnt = 0; int numarray[2][3]; for (int i = 0; i < 3; i++) {   for (int j =...

  • So I'm not sure what I'm trying to do here. I don't understand how *adr works,...

    So I'm not sure what I'm trying to do here. I don't understand how *adr works, I guess it's a pointer to the struct Address. How do I complete this function? And how would I have it just return the zip code or one element if I wanted to? I have two structs: struct Address {    int number; // street number    string street; // street Name    string city; // city    short zip; // zip code   ...

  • In the code C++ below it keeps saying expression must have a constant value at line...

    In the code C++ below it keeps saying expression must have a constant value at line 4, how I can fix it using with the dynamic memory int n; cout << "Enter number of homework grades: "; cin >> n; int hw[n]; float sum = 0; float hwavg, qavg; cout << "Homework Grades: ";   for (int i = 0; i < n; i++) { cin >> hw[i]; sum += hw[i]; } hwavg = sum / n;

  • C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using...

    C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using namespace std; vector<int> destroyedOnes; class Movie { public: Movie(int r) : m_rating(r) {} ~Movie() { destroyedOnes.push_back(m_rating); } int rating() const { return m_rating; } private: int m_rating; }; // Remove the movies in li with a rating below 50 and destroy them. // It is acceptable if the order of the remaining movies is not // the same as in the original list. void...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • 6. (Short answer) The C++ code below is very close to working, but, as written, won't...

    6. (Short answer) The C++ code below is very close to working, but, as written, won't compile. When trying to compile, the compiler generates a number of errors similar to "cout' was not declared in this scope." Respond to the prompts below about the code.(20 points, 5 points each) #include <iostream> void FindMax(int a, int b, int& max) { if (a > b) max = a; else { max = b; } int main() { int num1 = 0; int...

  • Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are...

    Please answer in C++ ONLY, and please fill all comments elaboratively. Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are required for this question. Run and provide a single screenshot showing the output. Complete the missing comments to explain the action performed by each statement highlighted in red. The first comment is given as an example. Copy and paste the source code with the comments filled out in your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT