In my book, it says find() must be used like so:
ex:
iter = var.find(scores.begin(), scores.end(), score);

but later it uses this in practice:

ex:
while (used.find(guess) != string::npos)

Why can you simply pass 'guess' var to it here and not need to search a range? Isn't find() an algorithm that requires 3 arguments to work?
*Guess is 'char guess' declared earlier.

Recommended Answers

All 6 Replies

Ok I see. so find() operates differently depending which type of variable it's being used on, and what type of arguments are being passed to it right?

The find() in algorithm is completely independent of find() in string. find() in algorithm should be able to be used on any STL container (including std::string if I am not mistaken). The find() in string can only be used from a string object and takes different kinds of parameters as overloads (see those references I posted).

I should have mentioned that used was declared ahead of the code as:

string used = "";

But now I'm curious about this reference you're making. What makes one find() an Algorithm, and another a Member function? I'm looking through the links you sent me, and maybe it's my newb-ness getting in the way, but how do you tell the difference?

What is the type of var in your above code? The <algorithm> versions are not member functions of an object as normally they take iterators to objects as parameters. That's why I'm confused as to where var is coming from. I misread it initially.

Just to add, you can declare multiple functions with the same name in the same namespace as long as the arguments are different. This is called function overloading.

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.