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.
[1:]
Python's list slicing operator:
list1 = [1, 2, 3] list2 = list1[1:] print(list2) # [2, 3]
C++ Desired result:
std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2; v2 = v1[1:]; std::cout << v2 << std::endl; //{2, 3}
This can easily be done using std::vector's copy constructor:
std::vector
v2 = std::vector<int>(v1.begin() + 1, v1.end());
1.4m articles
1.4m replys
5 comments
57.0k users