M
Martoon
I want to instantiate an STL map with my own compare function, and I
want to pass a parameter to the compare function that will be stored
and used for all comparisons in that map instance.
As an example, let's say the map key is char* strings, and I want the
comparison based on the nth character in the string. When the map is
instantiated, I need to somehow specify n. So I might have something
like this:
class MyClass
{
public:
void MyFunction(int i);
private:
class MyCompare
{
public:
MyCompare() : m_iCharIndex(0) {}
MyCompare(int iCharIndex) : m_iCharIndex(iCharIndex) {}
bool operator()(const char* c1, const char* c2) const
{
return c1[m_iCharIndex] < c2[m_iCharIndex];
}
int m_iCharIndex;
};
};
void MyClass::MyFunction(int i)
{
// this uses default constructor for MyCompare()
std::map<const char*, float, MyCompare> myMap;
// how do I use MyCompare(i) instead?
}
I can create a map with a char* key and MyCompare as the compare
function. But what I want to do is somehow set the m_iCharIndex member
of the MyCompare class for each map that I create (so I might have one
map that keys on the 3rd character of the strings, another that keys on
the 17th character, etc.).
Is there any way that I can do something like this? I'm not actually
trying to implement an nth character compare class. That's just a
concrete example I came up with for illustration purposes. But I am
trying to find a way to pass a parameter to a compare function for each
map that I create. And that parameter will be stored and used in all
comparisons for that map instance.
Any help appreciated.
want to pass a parameter to the compare function that will be stored
and used for all comparisons in that map instance.
As an example, let's say the map key is char* strings, and I want the
comparison based on the nth character in the string. When the map is
instantiated, I need to somehow specify n. So I might have something
like this:
class MyClass
{
public:
void MyFunction(int i);
private:
class MyCompare
{
public:
MyCompare() : m_iCharIndex(0) {}
MyCompare(int iCharIndex) : m_iCharIndex(iCharIndex) {}
bool operator()(const char* c1, const char* c2) const
{
return c1[m_iCharIndex] < c2[m_iCharIndex];
}
int m_iCharIndex;
};
};
void MyClass::MyFunction(int i)
{
// this uses default constructor for MyCompare()
std::map<const char*, float, MyCompare> myMap;
// how do I use MyCompare(i) instead?
}
I can create a map with a char* key and MyCompare as the compare
function. But what I want to do is somehow set the m_iCharIndex member
of the MyCompare class for each map that I create (so I might have one
map that keys on the 3rd character of the strings, another that keys on
the 17th character, etc.).
Is there any way that I can do something like this? I'm not actually
trying to implement an nth character compare class. That's just a
concrete example I came up with for illustration purposes. But I am
trying to find a way to pass a parameter to a compare function for each
map that I create. And that parameter will be stored and used in all
comparisons for that map instance.
Any help appreciated.