我正在编写一些代码,并且我有一个部分可以执行一次性排序功能。为了实现它,我决定重载 operator< 函数是最简单的。我更愿意做的是通过使用某种 boost::bind、boost::phoenix、lambda 或某种其他类型的实现来使排序的实现更接近实际调用。不幸的是,我无法访问新的 C++11 功能。下面是一些示例代码。
// In a header
struct foo
{
char * a;
char * c_str() { return a; }
}
// In a header
struct bar
{
foo * X;
bar(foo * _X) : X(_X) {}
bool operator < (const bar& rhs) const
{
return std::string(X->c_str()) < std::string(rhs.X->c_str());
}
};
struct bars : public std::vector<bar> { ... some stuff };
// Some other header
bars Bs;
// A cpp file
... other stuff happens that fills the Xs vector with objects
...::Function()
{
// Current use and it works fine
std::sort(Bs.begin(), Bs.end())
// Would like something that accomplishes this:
// std::sort(Bs.begin(), Bs.end(),
// std::string(lhs.X->c_str()) < std::string(rhs.X->c_str()))
// A non-working example of what I'm trying to do
// std::sort(Xs.begin(), Xs.end(),
// std::string((bind(bar::X->c_str(), _1)) <
// std::string((bind(bar::X->c_str(), _2)) )
}
当我试图弄清楚如何访问成员指针、成员函数然后将结果全部转换为 boost::bind 函数时,我迷失了方向。
谢谢您的帮助。