|
-
Oct 31st, 2003, 04:53 PM
#1
Thread Starter
Frenzied Member
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

-
Nov 1st, 2003, 12:25 PM
#2
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.
-
Nov 3rd, 2003, 11:27 AM
#3
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|