amangupta
amangupta

Reputation: 142

Is there a way to start vector indexing from 1 in c++?

Just wanted to know that , If there is any way to start indexing of a vector or an array from 1 in c++ . I am not talking about pushing some random at index 0 and then carrying out work with starting at index 1 . I mean actually starting from 1.

I thought it may be possible after overloading the [] operator and decrement the value of index by 1 inside it . So that if I write my_vector[1] , it should mean(internally) my_vector[0],i.e., 0th index element. Is there any other way in standard template library itself?

Upvotes: 0

Views: 2568

Answers (1)

eerorika
eerorika

Reputation: 238311

Just wanted to know that , If there is any way to start indexing of a vector or an array from 1 in c++

No.

What you can do is define a custom container of your own with an overloaded operator[] that behaves as you would like.

On the other hand, consider carefully whether this is a good idea. Even if it may feel simpler to you, it will be confusing to others.

Upvotes: 6

Related Questions