我有一個自定義列表類,並希望支持使用STL中已知的「比較運算符」的操作。例如:C++:如何讓自己的比較運算符()?
std::list<MyClass> d;
struct not_key {
not_key(std::string const& str) : str_(str) {}
bool operator(MyClass& elem) {
return !elem.findThatThing();
}
std::string str_;
};
not_key comp("value");
d.remove_if(comp);
mylist<MyClass> e(d);
e.filter(comp);
而我正在努力接受這些「一般」比較運算符的方法的簽名。由於它們都有不同的類型,我不想要靜態成員函數。我怎樣才能添加一個方法到我的類接受比較運算符?
非常感謝! :)
...如果你想在類之外的函數定義(儘管它應該仍然在頭文件中),你需要'template template inline void mylist :: filter(Pred pred){/ * ... * /}' –
aschepler
2013-02-19 15:07:59