0

Dear friends:

 I need to use a data structure which can start the first index of an array from a nagtive value. 
      I have searched the MTL library, and bltiz lib, but it seems  that they doesn't support this kind of vecotr.
        Could you please give me some suggestions.
    Regards
2
Contributors
3
Replies
25
Views
1 Year
Discussion Span
Last Post by tinstaafl
1

One way would be with a custom class to represent the collection. Use a vector as the base collection, then overload the index operator to accept a negative value and offset it to find the correct positive value in the base collection.

Here's what such a class might look like:

class myCollection {
private:
    vector<int> a;
    int _offset;
public:
    myCollection()
    {
        offset = 0;
    }
    myCollection(size_t size, int offset = 0)
    {
        a.resize(size);
        _offset = offset;
    }
    myCollection(int offset = 0)
    {
        _offset = offset;
    }
    size_t size()
    {
        return a.size();
    }
    int& operator[] (int x) 
    {
        return a[x + offset];
    }  
};

It still need functions for adding and removing data.

Edited by tinstaafl

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.