简体   繁体   中英

Checking whether element exists in a Vector

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.

typedef stdd::vector<SomeClass> vec;
//define some vec, v
for (vec::size_type i = 0, s = vec.size(); i < s; ++i) {
    //do something with v[i]
}

With a recursive function you could use this idea by passing along a maximum index.

void recursiveFunc(vec& v, vec::size_type s);

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:

template <typename Iterator>
void recursiveFunc(Iterator begin, const Iterator& end);

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:

while (begin != end) {
    //do something with *begin
    ++begin;
}

std::vector only inserts "real" objects. It (at least normally) allocates raw memory, and uses placement new to construct objects in that memory as needed. The only objects it'll contain will be the ones you put there.

Of course, if you want to, you can create a vector containing a number of copies of an object you pass to the constructor. Likewise, when you resize a vector, you pass an object it'll copy into the new locations if you're making it larger.

Neither of those is really the norm though. In a typical case, you'll just create a vector , which will start out containing 0 objects. You'll use push_back to add objects to the vector . When you search through the vector , the only objects there will be the ones you put there with push_back , and you don't need to worry about the possibility of it containing any other objects.

If you just want to check whether the vector is empty, you can just use:

if (your_vector.empty())

...which will (obviously enough) return true if it's empty, and false if it contains at least one object.

As @Corbin mentioned, size() will return the number of elements in the vector. It is guaranteed not to have any holes in between(contiguous), so you assured vector[vector.size()] is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM