Sign in to follow this  
the_cyberlord

Q about C++ containers

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 >
{
public:
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]; }
};



WARNING WARNING WARNING

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.

WARNING WARNING WARNING

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this