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
  • 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());

  • 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   ...

  • 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...

  • What is the software bug, and how do you fix the issue for the code fragment...

    What is the software bug, and how do you fix the issue for the code fragment below? #include <iostream> #include <limits> using namespace std; int main() { int x; //find the maximum integer value and avoid an overflow int imax = std::numeric_limits ::max(); cout <<"Enter a value for x: "; cin >> x; if (x > imax) cout << "overflow\n"; else cout << x; return 0; }

  • 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...

  • 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...

  • The C++ code below will compile but has a variety of runtime issues. Identify a runtime...

    The C++ code below will compile but has a variety of runtime issues. Identify a runtime issue with the program and describe how you might fix the problem. #include <iostream> #include <vector> using namespace std; //------------------------------------------------------------------------------ int main() { vector<double> temps; // temperatures double temp = 0; double sum = 0; double high_temp = 0; double low_temp = 0; while (cin >> temp) // read and put into temps temps.push_back(temp); for (int i = 0; i < temps.size(); ++i) {...

  • Plz give me correct code and screen shots for this question by using SASM !!!!!!!! Implement...

    Plz give me correct code and screen shots for this question by using SASM !!!!!!!! Implement the following working C++ program in assembler. Submit assembler code and screen shot. #include <iostream> #include <iomanip> using namespace std; // This menu-driven Health Club membership program carries out the // appropriate actions based on the menu choice entered. A do-while loop // allows the program to repeat until the user selects menu choice 4. int main() { // Constants for membership rates const...

  • C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream>...

    C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream> template <typename T, int N=10> class Array { private:   T array[N+1]; public:   Array() {     for(int i=0; i<N+1; i++) array[i] = 0;   }      T& operator [] (int index) {     if (index>N || index < 0)       return array[N];     else       return array[index];   }   template <typename S>   Array<T,N>& operator= (S &other) {     for(int i=0; i<N+1; i++)       array[i] = other[i];         return *this;   } }; template <typename T, typename...

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
ADVERTISEMENT