首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指向成员函数的指针- C++ std::list排序

指向成员函数的指针- C++ std::list排序
EN

Stack Overflow用户
提问于 2009-03-12 15:16:26
回答 6查看 9.8K关注 0票数 5

如何将指向成员函数的指针传递给std::list.sort()?

这个是可能的吗?谢谢

代码语言:javascript
复制
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() (值总是相同的长度)

编辑: myValueLength不是我想要从比较函数中访问的唯一变量,我只是简化了它以使示例更简短。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2009-03-12 16:25:36

详细说明grieve's响应,为什么不使用函数器?例如:

代码语言:javascript
复制
struct Functor
{
  bool operator()( char * a, char * b )
    { return strcmp(a,b) < 0; }
};

然后你可以直接使用:

代码语言:javascript
复制
Functor f;
myList.sort(f);

您甚至可以通过定义operator()将您的类用作函数器...

代码语言:javascript
复制
class myClass {
  ...
  bool operator()( queueNode * a, queueNode * b )
  { return compare( a, b ); }

  void doStuff() { MyQueue.sort(*this); }
};

简单的示例代码:

代码语言:javascript
复制
#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;
}
票数 7
EN

Stack Overflow用户

发布于 2009-03-12 15:26:01

这是可能的。你有没有考虑过使用boost::function?

代码语言:javascript
复制
list.sort( boost::bind( &myClass::compare, this, _1, _2 ) );

你的“比较”功能会依赖于这个数据吗?如果没有-你可以简单地把'compare‘函数变成static。然后它就会成为

代码语言:javascript
复制
list.sort( &myClass::compare );

您可以添加辅助结构来进行比较,然后

代码语言:javascript
复制
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;
};
票数 6
EN

Stack Overflow用户

发布于 2009-03-12 15:18:51

您可能想要使用函数器。

http://www.newty.de/fpt/functor.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/639100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档