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

Recommended Answers

All 3 Replies

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.

great thank you very much for you r kinely help

If your question is answered please remember to mark this solved. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.