I want to implement an array that can increment as new values are added. Just like in Java. I don't have any idea of how to do this. Can anyone give me a way ?
This is done for learning purposes, thus I cannot use std::vector
.
I want to implement an array that can increment as new values are added. Just like in Java. I don't have any idea of how to do this. Can anyone give me a way ?
This is done for learning purposes, thus I cannot use std::vector
.
An array which grows dynamically as we add elements are called dynamic array, growable array, and here is a complete implementation of a dynamic array .
Here's a starting point: you only need three variables, nelems
, capacity
and a pointer to the actual array. So, your class would start off as
class dyn_array
{
T *data;
size_t nelems, capacity;
};
where T
is the type of data you want to store; for extra credit, make this a template class. Now implement the algorithms discussed in your textbook or on the Wikipedia page on dynamic arrays.
Note that the new
/delete
allocation mechanism does not support growing an array like C's realloc
does, so you'll actually be moving data
's contents around when growing the capacity.
In C and C++ array notation is basically just short hand pointer maths. So in this example.
int fib [] = { 1, 1, 2, 3, 5, 8, 13};
This:
int position5 = fib[5];
Is the same thing as saying this:
int position5 = int(char*(fib)) + (5 * sizeof(int));
So basically arrays are just pointers.
So if you want to auto allocate you will need to write some wrapper functions to call malloc() or new, ( C and C++ respectively).
Although you might find vectors are what you are looking for...
I would like to take the opportunity to interest you in an interesting but somewhat difficult topic: exceptions.
std::unique_ptr<char[]>
), you still have to ensure that operations that change the object leave it in a consistent state should they fail.For example, here is a simple class with an incorrect
resize
method (which is at the heart of most code):Okay, so that's the easy part (although already a bit tricky).
Now, how do you push a new element at the end ?
Feels right no ?
I mean, we even take care of a possible exception raised by
T
's copy constructor! yeah!Do note the subtle issue we have though: if an exception is thrown, we have changed the
size
andcapacity
members but still have the oldbuffer
.The fix is obvious, of course: we should first change the buffer, and then the size and capacity. Of course...
But it is "difficult" to get it right.
I would recommend using an alternative approach: create an immutable array class (the capacity should be immutable, not the rest), and implement an exception-less
swap
method.Then, you'll be able to implement the "transaction-like" semantics much more easily.