string::find_last_of function signature

This is a discussion on string::find_last_of function signature within the C++ Programming forums, part of the General Programming Boards category; Anyone know the proper declaration for a function pointer that I'm going to initialize to the find_last_of(string&, string::size_type) function. Something ...

  1. #1
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102

    string::find_last_of function signature

    Anyone know the proper declaration for a function pointer that I'm going to initialize to the find_last_of(string&, string::size_type) function.

    Something with the typedefs or fact that string is from STL is giving me problems.

    The documentation, parameter help list, and headers all show something different. I can't even understand what's in the headers.

    Please I really need this. Thanks in advance.




    http://www.function-pointer.org/
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I'm not sure I understand what you want to do. You'll have a tough time making find_last_of a function pointer, since it's a member function to a class. For what it's worth, the function is called like this:

    Code:
    int main()
    {
         string str("Hello, World");
         string string_to_find(",");
    
         string::size_type pos = str.find_last_of(string_to_find);
    
         if ( pos == string::npos ) 
              cout<<"Not found"<<endl;
         else
              cout<<"Comma at position "<<pos<<endl;
    
         return 0;
    }
    As another note, if you are doing something like iterating through a container of strings with a standard algorithm, and you want to call find_last_of on each string in the container, you might want to call find_last_of via a function object. If this is what you are trying to do, then maybe I can post an example of it...
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I have some string parsing functions and want the user to be able to define whether they want it done in reverse. At the beginning I want to do something like this.
    PHP Code:
    unsigned int myFunctionPointer(const string &, unsigned int);//declare the function pointer


    if(REVERSESEARCH)//if the user wants functions to behave with reverse searching
             
    myFunctionPointer=&string::find_last_of;
    else
             
    myFunctionPointer=&string::find_first_of
    then all my functions will call myFunctionPointer and it will behave approriately, so that I don't have to put an if else statement at every place I do a search to see whether it's forward or reverse. I'll just setup the function pionter at the beginning.

    The www.function-pointer.org website shows how to do this with class member functions, and I've done it before with my own classes.

    The problem I'm having is figuring out what the correct function signature is so that I don't get the parameter mismatch error. The documentation, header files, and little thing that pops up whenever you hit the first ( in VC++ all show different function signatures. If I try to use size_type for the second paramter it says undefined. So i changed it to unsigned int like I saw somewhere else. But really it's dependent on some template parameter.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Maybe try something like this:
    Code:
    #include <string>
    using namespace std;
    
    class SearchString {
        bool forward;
        string str;
        public:
    
        SearchString(string& s, bool d = true) { forward = d; str = s; }
    
         string::size_type operator()(string& s, string::size_type  i = 0 )
         {
              if ( forward == true )
                  return str.find_first_of(s,i);
              else
                  return str.find_last_of(s,i);
          }
    };
    
    int main()
    {
        string mystring("Hello, World");
        string comma(",");
        SearchString ss(mystring);
         // or reverse search:
        // SearchString ss(mystring, false); 
    
        string::size_type t = ss(comma);
    
         // etc.
    
         return 0;
    }
    This code compiled fine on visual c++ 6.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    After looking at your hyperlink, I think you probably want something more like this, but this code doesnt compile....But I think it is very close. It is late, and maybe somebody else can see what I am missing here. I am getting the error "Term does not evaluate to a function". This is just modified code of what mem_fun/mem_fun_t etc. is doing:

    It's interesting...maybe I will look at it more tomorrow.

    Code:
    #include <functional>
    #include <string>
    using namespace std;
    
    template<class R, class T, class A1, class A2>
        struct bin_mem_fun_t : public binary_function<A1, A2, R> {
    	R(T::*_pm)(const A1&,A2) const;
    	bin_mem_fun_t() { _pm = NULL;}
                    bin_mem_fun_t(R (T::*pm)(const A1&, A2) const)
    	{_pm = pm; }
    
    	R operator()(T& t, const A1 a, A2 b) { return t.*_pm(a,b); }
        };
    
    template <class R, class T, class A1, class A2>
    bin_mem_fun_t<R,T,A1,A2> bin_mem_fun(R (T::*fp)(const A1&,A2) const)
    { return bin_mem_fun_t<R,T,A1,A2>(fp); }
    
    int main()
    {
    	string str("Hello, world");
    	string comma(",");
    	bin_mem_fun_t<string::size_type, string, string, string::size_type> 
    		pfun = bin_mem_fun<string::size_type, string, string,string::size_type>(&string::find_first_of);
    
    	pfun(str, comma, 0);
    
    
    	return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Well...here's the whole enchilada, kid. The only problem is that find_first_of expects the second arg to be 0, ie. search from pos 0 forward, and find_last_of expects arg 2 to be str.size(), ie. search from pos str.size() backward. Hopefully you can figure some way to modify the code below to make this work for you.

    Code:
    #include <functional>
    #include <string>
    #include <iostream>
    using namespace std;
    
    #pragma warning(disable:4786)
    
    template<class R, class T, class A1, class A2>
        struct mem_fun2_t : public binary_function<A1, A2, R> {
    	R(T::*pmem)(const A1&,A2) const;
    	mem_fun2_t() { pmem = NULL;}
                    mem_fun2_t(R (T::*pm)(const A1&, A2) const)
    	{pmem = pm; }
    
    	mem_fun2_t<R,T,A1,A2>::result_type operator()(T& t, const mem_fun2_t<R,T,A1,A2>::first_argument_type a, mem_fun2_t<R,T,A1,A2>::second_argument_type b) 
    	{ return (t.*pmem)(a,b); }
        };
    
    template <class R, class T, class A1, class A2>
    mem_fun2_t<R,T,A1,A2> mem_fun2(R (T::*fp)(const A1&,A2) const)
    { return mem_fun2_t<R,T,A1,A2>(fp); }
    
    int main()
    {
    	char c;
    	string str("Hello, world");
    	string comma(",");
    	mem_fun2_t<string::size_type, string, string, string::size_type> pfun;
    
    	// Ask user which way to search
    	cout<<"Search in reverse(Y/N)?";
    	c = cin.get();
    
    	// set up member function pointer accordingly
    	if ( c == 'Y' )
    	{
    	    pfun = mem_fun2<string::size_type, string, string,string::size_type>(&string::find_last_of);
    	}
    	else
    	{
    	    pfun = mem_fun2<string::size_type, string, string,string::size_type>(&string::find_first_of);
    	}
    
    	// use the object
    	string::size_type pos = pfun(str, comma, 0); // but the third param must be str.size() if you search in reverse!!!
    
    	if ( pos == string::npos )
    	    cout<<"Not found"<<endl;
    	else
    	    cout<<"Found at pos: "<<pos<<endl;
    
    
    	return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Cool, thanks, hope that works.

    As far as the difference in second parameter I can make my own functions, one that returns 0, and another that returns str.size() and then assign the approopriate function to another funciton pointer at the same time I do the string function. Then I'll just call the function as the second paramter each time and depnding on whether it's forward or reverse search it'll evaluate to what needs to be passed as second arg.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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