Results 1 to 3 of 3

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 was wondering if there was an easier way to this maybe with a template?

    I have a bunch of structs that have one common element. I use that common element to do a std::lower_bound on a vector of these structs. So for example:

    Code:
    struct a
    {
      std::string sX;
      std::string sName;
    };
    struct b
    {
      std::string sX;
      std::string sLocation;
    };
    struct c
    {  
      std::string sX;
      std::string sPeople;
    };
    
    struct A_LESS
    {
         bool operator () (const a& lhs , const a& rhs)
        {
            if (lhs.sX.size() != rhs.sX.size())
    			return lhs.sX.size() > rhs.sX.size();
            return lhs.sX < rhs.sX;
        }
    };
    
    vector<a> vecA;
    ...all the rest are vectors, then data is read in.  Then in a function
    
    b b_temp.sX = "xyz";
    vector<a>::iterator it = std::lower_bound(vecA.begin(),
    						vecA.end(),
    						b_tmp,A_LESS());
    My question is that all the structs have sX in them. Is it possible to create a template or something instead of having to make a new less than struct for each of the types of structs (a,b,c), so I can do std::lower_bound against all the structs (a,b,c)?
    Last edited by Technocrat; Oct 31st, 2003 at 04:57 PM.
    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
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Yes, that would work:
    Code:
    template <typename T>
    struct generic_less
    {
        inline bool operator () (const T& lhs , const T& rhs) { ... }
    };
    std::lower_bound(vecA.begin(), vecA.end(), b_tmp, generic_less<a>());
    But what kind of data are you storing here? Maybe a std::map would be better suited. Also, to use std::lower_bound, the vector must be sorted.

  3. #3

    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
    When I compile it I get "error C2955: 'generic_less' : use of class template requires template argument list" in the lower_bound.

    Code:
    template <typename T>
    struct generic_less
    {
        inline bool operator () (const T& lhs , const T& rhs) 
    	{
    		if (lhs.sX.size() != rhs.sX.size())
    			return lhs.sX.size() > rhs.sX.size();
    		return lhs.sX < rhs.sX;
    	}
    };
    Another way I guess would be to do this:
    Code:
    struct base
    {
      std::string sX;
    };
    struct a : public base
    {
      std::string sName;
    };
    struct b : public base
    {
      std::string sLocation;
    };
    struct c : public base
    {
      std::string sPeople;
    };
    
    struct BASE_LESS
    {
         bool operator () (const base& lhs , const base& rhs)
        {
            if (lhs.sX.size() != rhs->sX.size())
    		return lhs.sX.size() > rhs.sX.size();
            return lhs.sX < rhs.sX;
        }
    };
    Which I guess would be ok, I was just hoping to not have to mess with the stucts.

    The reason I havent used std::map is that search by the sX is not always what I want to do. There are many cases where other elements are looked up, plus sX is not always unique.
    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


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