.rfind()

This is a discussion on .rfind() within the C++ Programming forums, part of the General Programming Boards category; I'm really confused right now about this compiling issue with my code for the C++ style strings tutorial (took a ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    .rfind()

    I'm really confused right now about this compiling issue with my code for the C++ style strings tutorial (took a break from programming and came back, so I haven't advanced much).

    Code:
     
    
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    { 
    string review; 
    string::size_type length; 
    length = review.length(); 
    int bad; 
    int timesofword; 
    for (bad = review.rfind("bad", review.end()); bad != string::npos; bad = review.rfind("bad", bad)) {
    timesofword++; 
    bad++; 
    cout<<timesofword<<endl; 
    cin.get(); 
    }
    }

    Results:
    13 C:\Documents and Settings\Main\My Documents\stringnpos and find.cpp no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::rfind(const char[4], __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)'

    I don't know at all how to fix it, and I've looked up a bunch of stuff on size_type (I think that's the problem), but still just cannot seem to figure this one out. Could anyone here please help me out and explain to me what I've done incorrectly?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Your use of review.end() is wrong. That parameter is supposed to be a position, not an iterator. You would use string::npos, but that is the default value so you can just leave it out.

    You can also make it an easier to read loop if you initialize bad to string::npos and just use it every time.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    review.end() returns an iterator and not a size_type.

    You are also changing bad in a lot of places, but I'm not sure if it does the job (counting number of "bad"s in the string?). Why not use normal find and start at the end of the previous "bad" occurrence each time?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Thanks to both of you for your fast replies.

    Anon: when making this code, I was using the following code example from the C++ style strings tutorial here as a guideline:

    Code:
    string input;
    int i = 0;
    int cat_appearances = 0;
    
    getline(cin, input, '\n');
    
    for(i = input.find("cat", 0); i != string::npos; i = input.find("cat", i))
    {
        cat_appearances++;
        i++;  // Move past the last discovered instance to avoid finding same
              // string
    }
    cout<<cat_appearances;

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, but since you are using rfind (searching from the end of the string), I thought you might need to decrement bad in the loop to move it towards the beginning of the string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by anon View Post
    Yes, but since you are using rfind (searching from the end of the string), I thought you might need to decrement bad in the loop to move it towards the beginning of the string.
    Ah, k. Well I've decided to just skip the .rfind() function, since all the time frustrating about it isn't worth the little to no benefitial gain from knowing it. Thanks for your help, though .

Popular pages Recent additions subscribe to a feed

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