-
June 29th, 2015, 04:05 AM
#1
std::vector with negative subscript?
My application I am developing is purely geometric.
So sometimes I need to index an array with a negative subscript.
For example, the extent of a scene may span from a negative range to a positive range.
That forms a grid. But std::vector won't allow me to use a negative subscript.
Are there any workaround this?
Thanks
Jack
-
June 29th, 2015, 04:16 AM
#2
Re: std::vector with negative subscript?
You cannot access an array with a negative subscript because that would be accessing the array out of bounds. What you can do is to have a pointer point to somewhere in the middle of the array, upon which a negative subscript could access an element that actually exists.
In the case of a std::vector, perhaps you could write a function to translate the given subscript to the actual index of the element in the vector. The pointer trick could work too.
-
June 29th, 2015, 04:25 AM
#3
Re: std::vector with negative subscript?
Well, you could use a plain C array instead.
Or implement the additional vector to recalculate the main vector indexes.
Last edited by VictorN; June 29th, 2015 at 04:26 AM.
Reason: laserlight was faster! :)
Victor Nijegorodov
-
June 29th, 2015, 05:35 AM
#4
Re: std::vector with negative subscript?
I finally resort to a O Log (N) solution, by specifying a key like
Code:
stringstream oss;
oss << x << ":" << z;
grid[oss.str()] = node;
It gets slower, but I think it's more robust.
Thanks
Jack
-
June 29th, 2015, 07:04 AM
#5
Re: std::vector with negative subscript?
string based indexes... how... horribly inefficient and non-robust (what do you expect to happen when the input string doesn't look like a "X:Z" but is "hello world" ?
-
June 29th, 2015, 11:29 AM
#6
Re: std::vector with negative subscript?
But std::vector won't allow me to use a negative subscript.
As vector uses random access iterators, you can add or subtract a number to a vector iterator if the resulting iterator is not out of bounds. See http://www.cplusplus.com/reference/iterator/
So if you obtain a vector iterator that points somewhere within the vector then a number can be added or subtracted from this iterator.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 29th, 2015, 01:05 PM
#7
Re: std::vector with negative subscript?
If the boundaries of your range are known, such as -10,000 - 10,000, you could use a 20,000 element vector and just add 10,000 to your index.
-
June 30th, 2015, 05:40 AM
#8
Re: std::vector with negative subscript?
Originally Posted by lucky6969b
My application I am developing is purely geometric.
So sometimes I need to index an array with a negative subscript.
For example, the extent of a scene may span from a negative range to a positive range.
That forms a grid. But std::vector won't allow me to use a negative subscript.
Are there any workaround this?
Thanks
Jack
You can use Boost.MultiArray, which allows setting an 'index base' to change the index of the first element in the array.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|