CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2005
    Posts
    58

    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?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: a question about standard algorithm

    #include <algorithm>

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: a question about standard algorithm

    Also, there is a std::count() algorithm

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: a question about standard algorithm

    And you have a typo in your code:
    Code:
    cout<<cout("Abcdiadcsa",'a')<<endl;

  5. #5
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    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

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: a question about standard algorithm

    And finally...
    • It is
      Code:
      int main
    • 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
  •  





Click Here to Expand Forum to Full Width

Featured