+ 2
How to add negative index functionality for std::vector?
Python can do negative indexes to start indexing from the last element. How can it be implemented in c++?
2 odpowiedzi
+ 1
You cannot use negative indexes in c++. However, if you wish to read a vector from the last element, you can use a reversed for loop like this:
int size = 5;
std::vector<int> myvector = {1,2,3,4,5};
for(int i = myvector.size() - 1; i >= 0; --i)
cout << myarr[i] << endl;
output:
5
4
3
2
1