如何在C ++中为模板化容器类定义排序谓词 [英] How do I define a sort predicate for a templated container class in C++

查看:135
本文介绍了如何在C ++中为模板化容器类定义排序谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我的C ++有点生锈.你们其中一位大师可以帮助我为Container类定义一个SORT谓词,而该谓词本身就是另一个类.

My C++ is a little rusty as of late. Can one of you gurus help me define a SORT predicate, for a Container Class, with a template parameter which it self is a another class.

template <class Element>
class OrderedSequence
// Maintains a sequence of elements in
// ascending order (by "<"), allowing them to be retrieved
// in that order.
{


 public:
 // Constructors
 OrderedSequence();

 OrderedSequence(const OrderedSequence<Element>&);

 // Destructor
 ~OrderedSequence(); // destructor

 OrderedSequence<Element>& operator= (const OrderedSequence<Element>& ws);

 // Get an element from a given location
 const Element& get (int k) const;



// Add an element and return the location where it
// was placed.
int add (const Element& w);

bool empty() const      {return data.empty();}
unsigned size() const   {return data.size();}


// Search for an element, returning the position where found
// Return -1 if not found.
int find (const Element&) const;


void print () const;

bool operator== (const OrderedSequence<Element>&) const;
bool operator< (const OrderedSequence<Element>&) const;

private:

std::vector<Element> data;

};

因此,此类接收一个模板参数,该参数是带有std :: string成员变量的STRUCT.

So, this class receives a template parameter which is a STRUCT with std::string member variable.

我想定义一个简单的排序谓词,以便可以调用: std :: sort(data.begin(),data.end(),sort_xx) 执行后: data.push_back() 在上述类的add()成员函数中.

I would like to define a simple sort predicate, so that I can call : std::sort(data.begin(), data.end(), sort_xx) after performing a : data.push_back() within the add() member function of the class above.

我该怎么做?我没有使用C ++ 11-只是普通的C ++.

How do I do it? I am not using C++ 11 - just plain old C++.

模板参数Element ..转换为:

Template parameter Element.. gets translated to:

struct AuthorInfo 
{
string name;
Author* author;

AuthorInfo (string aname)
   : name(aname), author (0)
 {}

bool operator< (const AuthorInfo&) const;
bool operator== (const AuthorInfo&) const;
};

bool AuthorInfo::operator< (const AuthorInfo& a) const
{
   return name < a.name;
}

bool AuthorInfo::operator== (const AuthorInfo& a) const
{
  return name == a.name;
}

推荐答案

可以使用什么 std :: find_if ,如果您需要自定义谓词.

What can use std::find_if, if you need a custom predicate.

要定义谓词ala C ++ 03:

To define a Predicate ala C++03 :

// For find()
struct MyPredicate
{
public:
  explicit MyPredicate(const std::string name) name(name) { }

  inline bool operator()(const Element & e) const { return e.name == name; }

private:
  std::string name;
};

// Assuming you want to lookup in your vector<> member named "data"
std::find_if(data.begin(), data.end(), MyPredicate("Luke S."));

// To sort it, its exactly the same but with a Sort comparer as the predicate:
struct MySortComparator
{
 bool operator() (const Element& a, const Element& b) const
 {
    return a.name < b.name;
 }
};

std::sort(data.begin(), data.end(), MySortComparator());

// Or you can style sort Author without predicates if you define `operator<` in the `Element` class :
std::sort(data.begin(), data.end())

如果可以使用C ++ 11,则只需使用lambda即可:

If you can use C++11, you can simply use a lambda :

std::find_if(data.begin(), data.end(), [](const Element & e) -> bool { return e.name == "Luke S."; });

现在显示Element,我看到您已经在Author中重载了operator==,所以您也可以这样做:

Now that you show Element I see that you already overloaded operator== in Author, so you could also do :

int find (const Element& e) const
{
  std::vector<Element>::iterator iter = std::find(data.begin(), data.end(), e);
  return std::distance(data.begin(), iter);
}

这篇关于如何在C ++中为模板化容器类定义排序谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆