-1

I'm trying to splice a vector in C++ using the following code:

sequence(sequence.begin() + i, sequence.end());

where sequence is a vector containing integers and i is an integer, but when I run my code it gives the following error:

error: no match for call to ‘(std::vector<int>) (__gnu_cxx::__normal_iterator<int*, std::vector<int> >, std::vector<int>::iterator)’ sequence(sequence.begin() + i, sequence.end()); ^

I don't understand what is wrong with my code. I used the following stack overflow questions to help write this code:

Creating a new C++ subvector?

Slicing a vector

Rory Danes
  • 163
  • 1
  • 6
  • 14
  • 2
    May I suggest you read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Rakete1111 Feb 09 '18 at 20:35

1 Answers1

0

You can't do that, because sequence doesn't have an overloaded call operator. You can only use that syntax when initializing objects when you are defining them:

int i(1); // ok
i(2); // nope!

You are trying to do the second line above, which fails because it's not a valid form of initialization in this context. If you want to splice a vector, you can use std::vector::erase;

sequence.erase(sequence.begin() + i, sequence.end()); // erases [start, end)
Rakete1111
  • 47,013
  • 16
  • 123
  • 162