I'm trying to make a program that will erase an integer from a vector, containing a random sequence of integers only if it is found in the vector. I wrote code already, but I get an error when I call the erase() function to actually delete the same integers in the vector. Thoughts?

#include <iostream>
#include <vector>

 using namespace std;

 void removeX(vector<int>& wl, int x);

  int main()
 {
vector<int> m_list;
int num;


for(int i = 0; i <= 25; i++)    //made the vector have values from 1 to 25.
  {
      m_list.push_back(i);
  }


cout << "Enter a number you wish to find and remove: " << endl;
cin >> num;


removeX(m_list, num);

for (size_t i = 0; i < m_list.size(); i++)
    {
        cout << "Content at index: " << i << ": " << m_list[i] << endl;
    }

return 0;
   }


  void removeX(vector<int>& wl, int x)
   {
for(size_t i = 0; i < wl.size(); i++)
{

        if (x == wl[i])
    {
        wl.erase(w[i]);

    }
}

}
有帮助吗?

解决方案

you show know that the parameter of function erase is iterator type, not size_t type.

void removeX(vector<int>& wl, int x)
{
    vector<int>::iteratot iter = wl.begin();
    while(iter !=  wl.end())
    {

            if (*iter == wl[i])
             {
               iter = wl.erase(iter);
             }
            else
               iter++;
        }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top