Algorithm::find on string

This is a discussion on Algorithm::find on string within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to use the 'find' function from the STL algorithms library on 'string' by using iterators only ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    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"

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

    Code:
    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)’
    http://www.cplusplus.com/reference/algorithm/find/
    Last edited by rodrigorules; 02-25-2011 at 05:39 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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
    Location
    Australia
    Posts
    174
    Not sure why you got that compiler error, this is what I got:

    Code:
    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:

    Code:
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <string>
     4 using namespace std;
     5
     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, 10:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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