The answer must be obvious , but how do i make a QList's index start with 1 instead of 0 .
And thank you ;)
@Walux Why would you want to do this?
The fact that container indexes start at zero is a language feature (I might be wrong?). Anyway, fact is that you can't just change it.
What you can do is subclassing the container class and overloading the
operatorto add an offset. Note: That is a possible solution and not a recommendation.
But as @Wieland already asked... Why would you want to do this?
Thanks for your support , it's probably not the smoothest idea , but it's good enough .
Well , all i can say is that in my program i used a lot of QLists that are better off starting with 1 as an index , it would really make the work - and especially the code - more comfortable and readable .
That's a pretty wrong idea. Your code is going to be understandable only by you and hard to debug for other people.
All list/vector like containers are indexed at 0. You seem to try to work-around something else. What is it that makes your list related code "better off starting at 1" ?
are better off starting with 1 as an index
This is what fortran does (and a few other obscure and useless languages) but it's arbitrary and completely artificial. An index in an array is the offset from the beginning of that array, so the first element has an offset of 0. There's no real, practical or good reason to think that arrays or lists should start from 1 just because people are used to counting that way; as I said such reasoning is arbitrary and is introduced completely artificially.
@kshegunov Hey, I learned programming with Turbo Pascal (it's not obscure or useless for me :-)) where you can define whether first index is 0 or 1
Actually my beef is with fortran, but you're right of course ... ;)
Still, C uses the zero-based (offset based) indexing and changing that would go against the language itself (not only against Qt) ... so there's no good reason to do (or even want) it.
@kshegunov I agree with you: there is no need to redefine this behaviour.
The "rest" of this topic got split into a separate topic.
Thanks you all for your contribution :)
I think i'm now convinced that the QList's index must start with 0 , even tho that was not the goal of this topic :b
To help make the image clear , i used plenty of variables that MUST start with 1 , and these variables are connected to a lot of arrays , what i do now is create NULL variables and store them in the beginning of the arrays i wish to use its items from "1" .
For example :
int index0 = 0 QList<int> myArray; myArray << index0 << myInteger1 << ...
But , is it safe all the time ?
Again: why must they start with 1 ?
A side effect of your current implementation is that you are wasting memory.
Hmmm , i guess that i should start accepting the way the arrays are built instead of making stubborn statements , thank you all for your advices .
I'll mark the topic as solved.
To add to all the excellent points - stuffing an artificial null element just to try to index from 1 will break as soon as you do e.g.
myArray.clear()or try to iterate with
myArray.begin(), use a range based for or tons of other code types. c++ is 0 based language. Don't swim against the tide.
Btw. calling a QList myArray is like calling a sausage chain a nunchaku ;) Although similar at first glance they are completely different things. You'll mislead readers of your code. Don't do that.
Got it ;)