繁体   English   中英

如何在C ++中为模板化容器类定义排序谓词

[英]How do I define a sort predicate for a templated container class in C++

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

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。

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

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

模板参数Element ..转换为:

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什么。

定义谓词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:

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

编辑:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM