How to have an array with negative indices ?

Hi it may be a trivial question for experts but I am in trouble to implement it.
My question is following:

As we know in cpp language the indices of a vector array starts with 0 to n, if we have to store n entries in it. It reads as x[0], x[1], ...., x[n]. But if I want to have an array of negative indices such that it reads x[-1], x[-2], x[-3] ... then how to do it ? Here x is declared as

vector<double>x(n);
//the entries are filled as
x.push_back(1);
.....
x.push_back(n);

Please suggest.
This is completely useless and something you shouldn't be wasting your time on, but okay.

With vectors you definitely can't. The overload of operator[]() will always start at the first element. What you can do is create an array in any way you want and make a pointer to some element other than the first. For example: T *p=array+n; Now you can safely access negative elements
EDIT: sorry helios you got there before me.
C++ identifies its array elements using integers from 0 (zero) up sequentially in increments of 1. The name of the arrray (say 'a') is a pointer to the first element of array 'a' or int a[0] and therefore holds the address of a[0] in memory. You can of course) initialize the array with signed values A fifteen element array would be written as return type name [15] and its elements would be numbered from 0 to 14. I would be interested to know why you want to do this.
Last edited on
Topic archived. No new replies allowed.