use of iterators

This is a discussion on use of iterators within the C++ Programming forums, part of the General Programming Boards category; Hi! I am learning C++ from the Stroustrup book and I have a problem with the use of iterators. This ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    use of iterators

    Hi!

    I am learning C++ from the Stroustrup book and I have a problem with the use of iterators.

    This is the code:
    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;
    }
    I get three errors.
    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.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    I think this might work, try:
    Code:
    string::size_type i = s.find(c);
    while ( i != npos )
    {
        ++n;
        i = s.find(c,i+1);
    }
    return n;
    Iterators are returned from functions like search, I think find returns something different.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    You need to "#include <algorithm>" as well - thats where find lives.

    gg

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    hk_mp5kpdw
    Your code does not work.

    Codeplug
    That's the solution.


    Thanks both of you for your replies.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    hk_mp5kpdw
    Your code does not work.
    This worked for me as a test piece, maybe it was because of the scope resolution for npos or something else:
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string temp = "Hello";
        int n = 0;
        string::size_type i = temp.find('l');
    
        while( i != string::npos )  // Maybe you needed the string:: part here
        {
            ++n;
            i = temp.find('l',i+1);
        }
        cout << n << " occurances of l found." << endl;
    
        return 0;
    }
    This outputs the following for me:
    2 occurances of l found.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  2. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 02:19 AM
  3. Writing Iterators...
    By DeepBlackMagic in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  4. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 01:27 PM
  5. Generic Progpammimg Iterators
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2002, 10:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21