0
StringVec func(StringVec & str1, Stringvec & str2)
{

std::vector<string>::iterator s2 = str2->vec.begin();

    for (std::vector<string>::iterator s = str1->vec.begin(); s != str1->vec.end(); ++s)

    {

        if(s2 == NULL)
        {
            cout << "str2 is smaller than str1" << endl;
        }

        ++s2;

    }


}

When a iterator goes out of bound, because the vector is smaller, won't that possibly create a situation where the pointer points to some rubbish instead of NULL? So how do I make the code above work in all situation?

2
Contributors
2
Replies
18
Views
3 Years
Discussion Span
Last Post by OpenTheTrollGate
1

Iterators aren't the same thing as pointers. Testing an iterator against NULL makes no sense. Typically, an iterator is tested for being beyond the range of the container by comparing it to the iterator returned with end, e.g.

if(s2 == str2->vec.end())

Edited by Moschops

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.