Jump to content
  • Advertisement
Sign in to follow this  

Q about C++ containers

This topic is 5062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I read about it somewhere but I can't find it anymore: It was about a container that worked just like an array, but you could just add a value to a higher index then the current maximum one and it would resize itself to the new index. like this:
superarray x[5]; // array of size 5
x[10] = 6; //array is now size 10

Share this post

Link to post
Share on other sites
There's no container that allows that exact syntax. The closest thing to it in the STL would be std::map:

std::map<int,int> x;
x[5] = 3;
x[10000] = 5;

This will get slow with large collections, but has the nice benifit that x[10000] = 5; dosn't force the system to allocate 10000 elements - it only allocates a second one.

Using hash_map (which isn't standard STL, although SGI's implementation has it: clicky) will speed up lookups if you have larger collections.

If you want to implement your own container that implements such functionality, I'd suggest you inherit from std::deque. Most of this example is a hacked up copy of the version of deque that comes with G++ 3.3.3

template < typename T , typename AllocT = std::allocator<T> > class autoarray : public std::deque< T , AllocT >
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef deque::iterator iterator;
typedef deque::const_iterator const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef deque::allocator_type allocator_type;

explicit autoarray(const allocator_type& a = allocator_type()) : deque(a) {}
autoarray(size_type n, const value_type& value, const allocator_type& a = allocator_type()) : deque(n,value,a) {}
explicit autoarray(size_type n) : deque(n) {}
autoarray(const autoarray& x) : deque(x) {}
template<typename InputIteratorT> autoarray(InputIteratorT first, InputIteratorT last, const allocator_type& a = allocator_type()) : deque(first,last,a) {}

virtual ~autoarray() {} //WARNING!!! deque's dtor is NOT virtual, you should PROBABLY NOT attempt to delete an autoarray through deque - although it might work since we add no members and have a dtor that does nothing.

//WARNING: these operators now need to do bounds checking each time. This is an overhead most users of STL containers will not expect.

reference operator[](size_type n) { if (n >= size()) resize(n+1); return deque::operator [](n); }
const_reference operator[](size_type ) const { if (n >= size()) return value_type(); return deque::operator [](n) }
reference at(size_type n) { return (*this)[n]; }
const_reference at(size_type n) { return (*this)[n]; }


This code is all untested, use at your own risk. The added checks now required by operator[] bring it up to the complexity of the at() statement. I wouldn't recommend using it.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!