Martin Dvoracek
Martin Dvoracek

Reputation: 1738

How to make iterator behave like a type through it iterates?

I want to iterate through a vector of stirngs and compare all inputs with a value. So I've created this construction:

for (vector<string>::const_iterator i = needle.begin(); i != needle.end(); ++i) {         
            cout << *i << " ";

        }

Printing works all right, but I want to be able to access std::string functions, ie find So while I'm able to write needle[index].find("sampleString"); I want to be able to make iterator behave similar to this. So how to make something like *i.find("sampleString"); ?

Upvotes: 1

Views: 97

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254431

You're almost there, but the operator precedence means that *i.find() doesn't work. It tries to dereference i.find(), rather than dereferencing i then calling find on the result. Use whichever of these looks nicest to you:

(*i).find("sampleString");
i->find("sampleString");

Upvotes: 13

Related Questions