|
-
April 9th, 2005, 09:49 PM
#1
a question about standard algorithm
Code:
#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?
-
April 9th, 2005, 09:58 PM
#2
Re: a question about standard algorithm
#include <algorithm>
Regards,
Paul McKenzie
-
April 9th, 2005, 10:54 PM
#3
Re: a question about standard algorithm
Also, there is a std::count() algorithm
-
April 10th, 2005, 01:21 AM
#4
Re: a question about standard algorithm
And you have a typo in your code:
Code:
cout<<cout("Abcdiadcsa",'a')<<endl;
-
April 10th, 2005, 05:25 AM
#5
Re: a question about standard algorithm
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).
Insert entertaining phrase here
-
April 10th, 2005, 06:56 AM
#6
Re: a question about standard algorithm
And finally...
- It is
- You would rather use the member function 'find' instead of the algorithm (despite that 'count()' would be the better algorithm here).
Last edited by Andreas Masur; April 10th, 2005 at 06:59 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|