CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: std::vector with negative subscript?

  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,734

    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

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    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

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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" ?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,149

    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++17 Compiler: Microsoft VS2019 (16.6.3)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    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.

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: std::vector with negative subscript?

    Quote Originally Posted by lucky6969b View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)