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?

Recommended Answers

All 2 Replies

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())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.