3

I am trying to iterate over a vector and remove the first occurrence of an object. I keep getting a compile error (using g++), but I am removing it the way stackoverflow answers and other sources suggested removing it. There is probably something super simple that I'm missing, so another set of eyes would be great too.

#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;

bool Garage::remove(const Car &car){
assert(!empty());

int size = v.size();
for(vector<Car>::const_iterator it = v.begin(); it != v.end(); ++it){
    if(it -> Car::make() == car.Car::make()){
        it = v.erase(it);
        assert(v.size() == size - 1);
        return true;
     }
 }
 return false;
}

The compile error is error: no matching function for call to 'std::vector::erase(const Car&)'

chris
  • 60,560
  • 13
  • 143
  • 205
Sams
  • 197
  • 1
  • 3
  • 14
  • Please don't use the homework tag. [It's deprecated.](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated) – chris Dec 30 '12 at 04:03
  • 2
    You should search for the _erase/remove_ idiom... – K-ballo Dec 30 '12 at 04:04
  • The error doesn't seem to match your code. Are you sure your code doesn't say `erase(*it)` or something like that? – Kerrek SB Dec 30 '12 at 04:04
  • @chris Sorry, I don't come here often so I didn't realize the tag was no longer used. People used to tell me to add it. – Sams Dec 30 '12 at 04:05
  • I tried it and *it and both gave me the same error. – Sams Dec 30 '12 at 04:05
  • I did look at the erase/remove algorithm and it seemed like it was for erasing multiple occurrences, but I just want to erase the first occurrence. – Sams Dec 30 '12 at 04:06
  • Something simple like `v.erase(std::find(...))` would work. Make sure that it is found, though, before erasing it. – chris Dec 30 '12 at 04:07
  • `Car::make()` is static function, so `if(it -> Car::make() == car.Car::make())` will always evaluate to true? – billz Dec 30 '12 at 04:15

2 Answers2

3

You're trying to erase using a const_iterator. Since you're trying to modify the vector, switch to a regular iterator.

for(vector<Car>::iterator it = v.begin(); it != v.end(); ++it){

This works:

int main()
{
   vector<int> ints;

   for (vector<int>::iterator iter = ints.begin();iter != ints.end();++iter)
   {
      ints.erase(iter);
   }
}

This doesn't:

int main()
{
   vector<int> ints;

   for (vector<int>::const_iterator iter = ints.begin();iter != ints.end();++iter)
   {
      ints.erase(iter);
   }
}

Error when using a const_iterator:

test.cpp:18:22: error: no matching function for call to ‘std::vector<int>::erase(std::vector<int>::const_iterator&)’
Ryan Guthrie
  • 688
  • 3
  • 11
  • I removed const and it compiles now. Just have to test it a ton. – Sams Dec 30 '12 at 04:10
  • Just a note that on compilers following the C++11 standard, it *will* work with a `const_iterator`. – Bo Persson Dec 30 '12 at 10:54
  • 1
    @Bo Persson With which version of gcc? I am running 4.6.3 (g++ -std=c++0x) and it doesn't support const_iterator. I see where it is specified here, but gcc doesn't support it yet? http://en.cppreference.com/w/cpp/container/vector/erase – Ryan Guthrie Dec 30 '12 at 13:49
1

It appears that you'll need to use find to find the index of the element and subsequently erase it. I say "appears" because I'm not a C++ programmer.

Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91