-2

I am having trouble erasing part of a vector. I have a vector (string) named master. It is verified (through the size() function) to be 500 elements in size. I am trying to erase a range of elements, starting from zero to a variable named r. From what I see online this should be very straightforward but after reading documentation I still cannot figure this out. This is my code :

int r = 100;
master.erase(0,r);

The compiler is giving me this error :

[Error] no matching function for call to 'std::vectorstd::__cxx11::basic_string<char >::erase(int, int&)'

... Which after searching this online seems to come up often when people try to add a variable with a different type to the vector. However I am not doing that. I just have a vector where all the elements are strings and I want to erase a range.

What am I doing wrong?

David
  • 605
  • 2
  • 14
  • 44
  • 1
    *"From what I see online this should be very straightforward"* -- which online resource did you consult that told you to use indices (instead of iterators) as parameters to [`std::vector::erase()`](https://en.cppreference.com/w/cpp/container/vector/erase)? – JaMiT Nov 16 '21 at 05:16

1 Answers1

1

From std::vector::erase documentation, the arguments should be iterators, so you should pass iterators as arguments as shown below:

master.erase(master.begin(), master.begin()+100);
Jason
  • 36,170
  • 5
  • 26
  • 60