Results 1 to 2 of 2

Thread: Template?

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    Template?

    I asked this before but never really got an answer. So I will try again

    I have a std::vector that I search using lower_bound(). To do this I have a struct with a () operator like so:

    Code:
    struct LM_LESS
    {
         bool operator () (const LMABREP& lhs , const LMABREP& rhs)
        {
            if (lhs.sLID.size() != rhs.sLID.size())
    			return lhs.sLID.size() > rhs.sLID.size();
    		return lhs.sLID < rhs.sLID;
        }
    };
    LMABREP struct looks like:
    Code:
    struct LMABREP
    {
    	string sLID;
    	string sStreetNumber;
    	string sStreet;
    	string sStreetSufix;
    	string sStreetPrefix;
    };
    So basically sLID contains a number and I am looking for it using lower_bound(). The reason being is there is a possible repeat.

    My question is can I turn my operator () into a template because I use functions like this a lot with many different vectors with many different structs. I really dont want to have to write a new () every time I use a different struct.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Do all of your structs have this member? Then you can:
    Code:
    template <typename T>
    struct LM_LESS
    {
         bool operator () (const T& lhs , const T& rhs)
        {
            if (lhs.sLID.size() != rhs.sLID.size())
    			return lhs.sLID.size() > rhs.sLID.size();
    		return lhs.sLID < rhs.sLID;
        }
    };
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width