Hi!
I am learning C++ from the Stroustrup book and I have a problem with the use of iterators.
This is the code:
I get three errors.Code:# include <iostream> # include <string> using namespace std; int count_char (const string &s, char c) { int n = 0; string::const_iterator i = find (s.begin (), s.end (), c); // error No.1 while ( i != s.end() ) { ++n; i = find (i+1, s.end (), c); // error No.2 and No.3 } return n; } int main () { string s1; char ch = 0; cout << "Enter string: "; cin >> s1; cout << "Which character you want to count? "; cin >> ch; cout << "\n\nCharacter " << ch << "occurs " << count_char (s1, ch) << "times."; cin >> ch; return 0; }
1. d:\cpp\iterators-count-char\count.cpp(16): error C2065: 'find' : undeclared identifier
2. d:\cpp\iterators-count-char\count.cpp(20): error C2440: '=' : cannot convert from ''unknown-type'' to 'std::basic_string<_Elem,_Traits,_Ax>::const_itera tor'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
3. d:\cpp\iterators-count-char\count.cpp(20): error C2100: illegal indirection
Please help.
Thanks.