[英]Pointer to member functions - C++ std::list sort
如何將指向成員函數的指針傳遞給std :: list.sort()?
這可能嗎? 謝謝
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);
}
我想在類中使用長度變量,而不是在比較函數中使用strlen()(Value將始終具有相同的長度)
編輯:myValueLength不是我想從比較函數中訪問的唯一變量我只是簡化它以使示例更短。
闡述悲傷的反應,為什么不使用仿函數? 例如:
struct Functor
{
bool operator()( char * a, char * b )
{ return strcmp(a,b) < 0; }
};
然后你可以使用:
Functor f;
myList.sort(f);
您甚至可以通過定義operator()來將您的類用作Functor ...
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;
}
有可能的。 你考慮過使用boost :: function嗎?
list.sort( boost::bind( &myClass::compare, this, _1, _2 ) );
您的“比較”功能是否依賴於此數據? 如果不是 - 你可能會簡單地將'比較'功能變為靜態 。 然后它會
list.sort( &myClass::compare );
您可以添加輔助結構來進行比較
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;
};
您可能想要使用仿函數。
請注意, std::list
根據operator<
為該元素定義的元素對元素進行排序。 您需要更改compare
函數以使用為Node
對象定義的全局operator<
:
bool operator<(Node const& first, Node const& 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;
}
建議的改進將是:
bool operator<(Node const& first, Node const& second) {
for (size_t ii =0; first.Value[ii] == second.Value[ii]; ++ii) ; // note ;
return (first.Value[ii] < second.Value[ii]);
}
如果char *Value
確實代表C風格的字符串,並且您需要字典排序,則可以進一步改進:
bool operator<(Node const& first, Node const& second) {
return (strcmp(first.Value, second.Value) < 0);
}
如果它們真的是字符串,我建議你使用std::string
,你可以寫:
bool operator<(Node const& first, Node const& second) {
return first.Value < second.Value;
}
正如悲傷和mrree所說
簡單地重載()運算符
謝謝所有回答的人
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);
}
為什么不將比較函數設置為靜態,然后不再需要仿函數。 然后你可以簡單地做list.sort(比較);
nevermind ...我剛剛意識到你的比較函數正在使用類數據成員,所以它不能是靜態的。 使用Functor :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.