Thread: Algorithm::find on string

  1. #1
    Registered User
    Join Date
    Aug 2005

    Algorithm::find on string

    Hi, I am trying to use the 'find' function from the STL algorithms library on 'string' by using iterators only
    (I am aware that string::find exists)

    I am having trouble getting this to compile...any clues?

    (This example should read the string and insert a '+' character before every '-' in the string "curr"

                    String curr = "aaa-bbb-ccc";
    		string::iterator plus = curr.begin();
    		while(plus != curr.end())
    			plus = find(plus,curr.end(),'-');
    			if(plus != curr.end())
    any ideas?

    q.cpp:19: error: no matching function for call to ‘find(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char)’
    Last edited by rodrigorules; 02-25-2011 at 06:39 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Perhaps you need to include <algorithm>.

    You seem to be using the insert method incorrectly, though. It can invalidate the iterator, so you need to use the return value.
    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).

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Not sure why you got that compiler error, this is what I got:

    stuff$ g++ strIter.cpp
    strIter.cpp: In function ‘int main()’:
    strIter.cpp:8: error: ‘String’ was not declared in this scope
    strIter.cpp:8: error: expected ‘;’ before ‘curr’
    strIter.cpp:9: error: ‘curr’ was not declared in this scope
    Here's the complete program I put your code into:

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <string>
     4 using namespace std;
     6 int main()
     7 {
     8     String curr = "aaa-bbb-ccc";
     9     string::iterator plus = curr.begin();
    10     while(plus != curr.end())
    11     {
    12         plus = find(plus,curr.end(),'-');
    13         if(plus != curr.end())
    14         {
    15             curr.insert(plus,'+');
    16             ++plus;
    17         }
    18     }
    19 }
    Getting it to compile is a simple matter of changing "String" to "string" on line 8. However, as anon said, using insert invalidates your "plus" iterator. Indeed, when I run the program I get a segmentation fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM