Is there an equivalent of list slicing [1:]
from Python in C++ with vectors? I simply want to get all but the first element from a vector.
Python\'s lis
It seems that the cheapest way is to use pointer to the starting element and the number of elements in the slice. It would not be a real vector but it will be good enough for many uses.
This can easily be done using std::vector
's copy constructor:
v2 = std::vector<int>(v1.begin() + 1, v1.end());
You can follow the above answer. It's always better to know multiple ways.
int main
{
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2{v1};
v2.erase( v2.begin() );
return 0;
}
I know it's late but have a look at valarray and its slices. If you are using a vector of some sort of NumericType
, then it's worth giving it a try.