Click to See Complete Forum and Search --> : a question about standard algorithm


greghua
April 9th, 2005, 09:49 PM
#include <iostream>
#include <string>
#include <functional>

using namespace std;

int count(const string& s, char c)
{
string::iterator i=find(s.begin(),s.end(),c);
int n=0;
while(i!=s.end())
{
++n;
i=find(i+1,s.end(),c);
}
return n;
}

void main()
{
cout<<cout("Abcdiadcsa",'a')<<endl;
}


when compile, compiler says that find is an undeclared identifier.
find is a standard algorithm, should i include any other head file?

Paul McKenzie
April 9th, 2005, 09:58 PM
#include <algorithm>

Regards,

Paul McKenzie

Philip Nicoletti
April 9th, 2005, 10:54 PM
Also, there is a std::count() algorithm

gstercken
April 10th, 2005, 01:21 AM
And you have a typo in your code:cout<<cout("Abcdiadcsa",'a')<<endl;

wien
April 10th, 2005, 05:25 AM
Furthermore find will return a std::string::const_iterator since you are searching in a const string. You will need to catch it as such since you can't construct an iterator from a const_iterator (the other way around is okay).

Andreas Masur
April 10th, 2005, 06:56 AM
And finally...

It is int main
You would rather use the member function 'find' instead of the algorithm (despite that 'count()' would be the better algorithm here).