指向成员函数的指针 - C ++ std :: list sort [英] Pointer to member functions - C++ std::list sort
问题描述
我如何传递指向成员函数的指针到std :: list.sort()?
这是否可能?感谢
struct Node {
uint32_t ID;
char * Value;
};
类myClass {
private:
uint32_t myValueLength;
public:
list< queueNode *> MyQueue;
bool compare(Node * first,Node * second);
bool doStuff();
}
bool myClass :: compare(Node * first,Node * second){
unsigned int ii = 0;
while(ii< myValueLength)
{
if(first-> Value [ii]< second-> Value [ii])
{
return true;
} else if(first-> Value [ii]> second-> Value [ii])
{
return false;
}
++ ii;
}
return false;
}
bool myClass :: doStuff()
{
list.sort(compare);
}
我想在类中使用长度变量而不是strlen )在比较函数内(该值将始终是相同的长度)
编辑:myValueLength不是我想在比较函数内访问的唯一变量我只是简化了它使示例更短。
阐述 grieve的响应,为什么不使用函子?例如:
struct Functor
{
bool operator()(char * a,char * b)
{return strcmp(a,b)< 0; }
};
然后您可以使用:
Functor f;
myList.sort(f);
您甚至可以通过定义operator()...
class myClass {
...
bool operator()(queueNode * a,queueNode * b)
{return compare(a,b); }
void doStuff(){MyQueue.sort(* this); }
};
代码:
#include< iostream>
#include< list>
using namespace std;
//假设TYPE t; cout < t;已验证。
template< class TYPE>
inline ostream&运算符<< (ostream& theOstream,
const list< TYPE& theList)
{
typename list< TYPE> :: const_iterator listIterator = theList.begin();
for(int i = 0; listIterator!= theList.end(); listIterator ++,i ++)
thestream< [< i<< ]:\<<(* listIterator)<<\< endl
return theOstream;
}
struct Functor
{
bool operator()(const char * a,const char * b)
{return strcmp(a,b )& 0; }
};
int
main()
{
list< char *> l;
/ *加载一些示例测试数据... * /
char s [3];
s [2] ='\0'; (s [0] ='c'; s [0]> ='a'; s [0] - )
for(s [1] ='c'; s [ 1]> ='a'; s [1] - )
l.push_back(strdup(s));
/ *告诉我们测试数据... * /
cout< 1<< endl
/ *排序列表。 * /
Functor f;
l.sort(f);
/ *告诉我们我们现在有什么... * /
cout< 1<< endl
}
How do i pass a pointer to a member function to std::list.sort()?
Is this possible? Thanks
struct Node {
uint32_t ID;
char * Value;
};
class myClass {
private:
uint32_t myValueLength;
public:
list<queueNode *> MyQueue;
bool compare(Node * first, Node * second);
bool doStuff();
}
bool myClass::compare(Node * first, Node * second) {
unsigned int ii =0;
while (ii < myValueLength)
{
if (first-> Value[ii] < second-> Value[ii])
{
return true;
} else if (first-> Value[ii] > second-> Value[ii])
{
return false;
}
++ii;
}
return false;
}
bool myClass::doStuff()
{
list.sort(compare);
}
I want to use a length variable from within the class instead of doing strlen() within the compare function (The Value will always be the same length)
Edit: The myValueLength was not the only variable i wanted to access from within the comparison function I just simplified it to make the example shorter.
Elaborating on grieve's response, why not use a functor? E.g.:
struct Functor
{
bool operator()( char * a, char * b )
{ return strcmp(a,b) < 0; }
};
Then you could just use:
Functor f;
myList.sort(f);
You could even use your class as the Functor by defining operator()...
class myClass {
...
bool operator()( queueNode * a, queueNode * b )
{ return compare( a, b ); }
void doStuff() { MyQueue.sort(*this); }
};
Simple example code:
#include <iostream>
#include <list>
using namespace std;
// Assumes TYPE t; cout << t; is valid.
template<class TYPE>
inline ostream & operator<< ( ostream & theOstream,
const list<TYPE> & theList )
{
typename list<TYPE>::const_iterator listIterator = theList.begin();
for ( int i = 0; listIterator != theList.end(); listIterator ++, i ++ )
theOstream << " [" << i << "]: \"" << (*listIterator) << "\"" << endl;
return theOstream;
}
struct Functor
{
bool operator()( const char * a, const char * b )
{ return strcmp(a,b) < 0; }
};
int
main()
{
list<char*> l;
/* Load up some example test data... */
char s[3];
s[2] = '\0';
for ( s[0]='c'; s[0]>='a'; s[0]-- )
for ( s[1]='c'; s[1]>='a'; s[1]-- )
l.push_back(strdup(s));
/* Show us that test data... */
cout << l << endl;
/* Sort list. */
Functor f;
l.sort(f);
/* Show us what we have now... */
cout << l << endl;
}
这篇关于指向成员函数的指针 - C ++ std :: list sort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!