I have a vector and it is going to hold 10 objects maximum. When I create the vector I believe it creates 10 “blank” objects of the class it will hold. Therefore I have a problem because my code relies on checking that a vector element is null and obviously it is never null.
How can I check whether a vector object contains an element I inserted, or one of the default constructor “blank” objects upon initialisation?
Is there a technique round this?
(I need to check for null because I am writing a recursive algorithm and the termination point, when the recursive function returns is when an object in the vector is null)
An instance of a class cannot be null. Only a pointer.
You do, however, have size() that you can use.
With a recursive function you could use this idea by passing along a maximum index.
Then when checking your condition to recurse, you would need to check “am I at the end of the vector?”
Alternatively, instead of working on indexes, you could use iterators:
If done correctly (and if possible in your situation), this could decouple your manipulation from being aware of the underlying data being stored in a vector.
The loop to go over the vector would then look like: