C++ 指向成员函数的指针-C++;列表排序
如何将指向成员函数的指针传递给std::list.sort() 这可能吗?谢谢C++ 指向成员函数的指针-C++;列表排序,c++,list,comparison,C++,List,Comparison,如何将指向成员函数的指针传递给std::list.sort() 这可能吗?谢谢 struct Node { uint32_t ID; char * Value; }; class myClass { private: uint32_t myValueLength; public: list<queueNode *> MyQueue; bo
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);
}
struct节点{
uint32_t ID;
字符*值;
};
类myClass{
私人:
uint32_t myValueLength;
公众:
列出我的队列;
布尔比较(节点*第一,节点*第二);
bool-doStuff();
}
bool myClass::compare(节点*第一,节点*第二){
无符号整数ii=0;
而(ii值[ii]<第二个->值[ii])
{
返回true;
}否则如果(第一->值[ii]>第二->值[ii])
{
返回false;
}
++二,;
}
返回false;
}
bool myClass::doStuff()
{
列表。排序(比较);
}
我想在类中使用一个长度变量,而不是在比较函数中使用strlen()(该值将始终是相同的长度)
编辑:myValueLength不是我想从比较函数中访问的唯一变量,我只是简化了它以缩短示例 您可能需要使用函子
请注意,
std::list
可能会根据运算符对元素进行排序。您是否考虑使用Booo::函数?< /P>
list.sort( boost::bind( &myClass::compare, this, _1, _2 ) );
您的“比较”功能是否依赖于此数据?如果不是-您可以简单地将“比较”功能设置为静态。然后就是了
list.sort( &myClass::compare );
您可以添加helper结构来进行比较,然后
list.sort( Comparer( myValueLength ) );
struct Comparer
{
Comparer( uint32_t myValueLength ):
length( myValueLength )
{}
bool operator() (Node * first, Node * second)
{
unsigned int ii =0;
while (ii < length)
{
if (first-> Value[ii] < second-> Value[ii])
{
return true;
} else if (first-> Value[ii] > second-> Value[ii])
{
return false;
}
++ii;
}
return false;
}
uint32_t length;
};
list.sort(比较器(myValueLength));
结构比较器
{
比较器(uint32\u t myValueLength):
长度(myValueLength)
{}
布尔运算符()(节点*第一,节点*第二)
{
无符号整数ii=0;
而(ii<长度)
{
如果(第一个->值[ii]<第二个->值[ii])
{
返回true;
}否则如果(第一->值[ii]>第二->值[ii])
{
返回false;
}
++二,;
}
返回false;
}
uint32_t长度;
};
详细说明响应,为什么不使用函子?例如:
struct Functor
{
bool operator()( char * a, char * b )
{ return strcmp(a,b) < 0; }
};
您甚至可以通过定义运算符()将类用作函子
简单示例代码:
#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;
}
#包括
#包括
使用名称空间std;
//呈t型;正如格里夫和莫里建议的那样
只需重载()操作符就可以了
谢谢所有回答的人
struct Node {
uint32_t ID;
char * Value;
};
class myClass {
private:
uint32_t myValueLength;
public:
list<queueNode *> MyQueue;
bool operator()(Node * first, Node * second);
bool doStuff();
}
bool myClass::operator()(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(*this);
}
struct节点{
uint32_t ID;
字符*值;
};
类myClass{
私人:
uint32_t myValueLength;
公众:
列出我的队列;
布尔运算符()(节点*第一,节点*第二);
bool-doStuff();
}
bool myClass::operator()(节点*第一,节点*第二){
无符号整数ii=0;
而(ii值[ii]<第二个->值[ii])
{
返回true;
}否则如果(第一->值[ii]>第二->值[ii])
{
返回false;
}
++二,;
}
返回false;
}
bool myClass::doStuff()
{
list.sort(*this);
}
为什么不将比较函数设为静态,这样就不再需要函子了。然后您可以简单地执行list.sort(比较)
无所谓。。。我刚刚意识到您的比较函数使用的是类数据成员,因此它不能是静态的。使用Functor:)谢谢,但我不想依赖boost作为依赖项。在代码中,“compare”函数依赖于类数据并不明显。“length”是指“myValueLength”?在myClass::compare中使用length时,是指myValueLength吗?无法将其编译:错误:“bool myClass::operator My bad--copy/paste错误:P对节点使用全局运算符<”。见更新后的帖子。另外,长度应该是Node类的一部分,否则就没有意义了。您可以始终使用strncmp,它最多比较前n个字符。@Ben:Greg刚刚给了我第二个建议:)
list.sort( Comparer( myValueLength ) );
struct Comparer
{
Comparer( uint32_t myValueLength ):
length( myValueLength )
{}
bool operator() (Node * first, Node * second)
{
unsigned int ii =0;
while (ii < length)
{
if (first-> Value[ii] < second-> Value[ii])
{
return true;
} else if (first-> Value[ii] > second-> Value[ii])
{
return false;
}
++ii;
}
return false;
}
uint32_t length;
};
struct Functor
{
bool operator()( char * a, char * b )
{ return strcmp(a,b) < 0; }
};
Functor f;
myList.sort(f);
class myClass {
...
bool operator()( queueNode * a, queueNode * b )
{ return compare( a, b ); }
void doStuff() { MyQueue.sort(*this); }
};
#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;
}
struct Node {
uint32_t ID;
char * Value;
};
class myClass {
private:
uint32_t myValueLength;
public:
list<queueNode *> MyQueue;
bool operator()(Node * first, Node * second);
bool doStuff();
}
bool myClass::operator()(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(*this);
}