C++ boost的标准集::共享\u ptr<;字符串>;
我有一组boost::shared_ptr,我希望它不是通过共享指针而是通过字符串排序和唯一。我是否必须提供一个新的比较函数来获取共享指针并比较内容,或者已经存在这样一个比较器,我可以使用它?这非常具体,因此您可能需要一个自定义比较器 这应该起作用:C++ boost的标准集::共享\u ptr<;字符串>;,c++,boost,shared-ptr,C++,Boost,Shared Ptr,我有一组boost::shared_ptr,我希望它不是通过共享指针而是通过字符串排序和唯一。我是否必须提供一个新的比较函数来获取共享指针并比较内容,或者已经存在这样一个比较器,我可以使用它?这非常具体,因此您可能需要一个自定义比较器 这应该起作用: struct pointercompare { bool operator()(const boost::shared_ptr<std::string>& a, const boost::shared_ptr<st
struct pointercompare
{
bool operator()(const boost::shared_ptr<std::string>& a, const boost::shared_ptr<std::string>& b)
{
return (*a)>(*b);
}
}
struct pointer比较
{
bool运算符()(常量boost::shared\u ptr&a,常量boost::shared\u ptr&b)
{
申报表(*a)>(*b);
}
}
我将编写一种包装谓词和迭代器的通用方法,将值语义映射到任何类似于所有者的指针上
这就变得完全通用和可重用了
这里是简单的版本
下面的奖励代码为此类内容介绍了一个完整的库
#include <utility>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <algorithm>
template<class Comp>
struct pointee
{
pointee(Comp comp = Comp()) : _comp(comp) {}
template<class APtr, class BPtr>
bool operator()(const APtr& a, const BPtr& b)
{
return _comp(*a, *b);
}
Comp _comp;
};
int main()
{
std::vector<boost::shared_ptr<int>> v;
std::sort(v.begin(), v.end(), pointee<std::less<>>());
std::sort(v.begin(), v.end(), pointee<std::greater<>>());
}
#包括
#包括
#包括
#包括
模板
结构指针
{
指针对象(Comp-Comp=Comp()):u-Comp(Comp){}
模板
布尔运算符()(常数APtr&a、常数BPtr&b)
{
返回_comp(*a,*b);
}
公司(Comp);
};
int main()
{
std::向量v;
排序(v.begin(),v.end(),pointee());
排序(v.begin(),v.end(),pointee());
}
奖励积分
#include <utility>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <algorithm>
#include <functional>
template<class T, class X, class Y>
struct is_binary_op
{
template<class U> static auto test(U* p) -> decltype((*p)(std::declval<X>(), std::declval<Y>()), void(), std::true_type());
template<class U> static auto test(...) -> decltype(std::false_type());
static constexpr bool value = decltype(test((T*)0))::value;
};
template<class T, class X, class Y> static constexpr bool IsBinaryOp = is_binary_op<T, X, Y>::value;
template<class T, class X>
struct is_unary_op
{
template<class U> static auto test(U* p) -> decltype((*p)(std::declval<X>()), void(), std::true_type());
template<class U> static auto test(...) -> decltype(std::false_type());
static constexpr bool value = decltype(test((T*)0))::value;
};
template<class T, class X> static constexpr bool IsUnaryOp = is_unary_op<T, X>::value;
namespace detail {
template<class Comp>
struct pointee
{
pointee(Comp comp = Comp()) : _comp(comp) {}
template<
class APtr,
class BPtr
>
auto operator()(const APtr& a, const BPtr& b) const
-> std::enable_if_t<
IsBinaryOp<Comp, decltype(*a), decltype(*b)>
, bool>
{
return _comp(*a, *b);
}
template<
class APtr
>
auto operator()(const APtr& a) const
-> std::enable_if_t<
IsUnaryOp<Comp, decltype(*a)>
, bool>
{
return _comp(*a);
}
Comp _comp;
};
template<class Iter>
struct deref_iter : Iter
{
deref_iter(Iter iter) : Iter(iter) {}
auto& operator*() const {
return **static_cast<const Iter&>(*this);
}
};
}
template<class Pred>
auto pointee(Pred&& pred)
{
return detail::pointee<std::decay_t<Pred>>(std::forward<Pred>(pred));
}
template<class Iter>
auto deref_pointee(Iter&& iter)
{
return detail::deref_iter<std::decay_t<Iter>>(std::forward<Iter>(iter));
}
int main()
{
std::vector<boost::shared_ptr<int>> v;
// sort using the less predicate on the pointee
std::sort(v.begin(), v.end(), pointee(std::less<>()));
// sort using the greater predicate on the pointee
std::sort(v.begin(), v.end(), pointee(std::greater<>()));
// apply a unary predicate to every pointee
std::for_each(v.begin(), v.end(), pointee(std::logical_not<>()));
// transform the pointees by binding a binary predicate to a value and
// turning it into a unary predicate that adds 6
std::transform(v.begin(), v.end(),
deref_pointee(v.begin()),
pointee(std::bind(std::plus<>(), 6, std::placeholders::_1)));
}
#包括
#包括
#包括
#包括
#包括
模板
结构是二进制的
{
模板静态自动测试(U*p)->decltype((*p)(std::declval(),std::declval()),void(),std::true_type());
模板静态自动测试(…)->decltype(std::false_type());
静态constexpr bool value=decltype(测试((T*)0))::值;
};
模板静态constexpr bool IsBinaryOp=is_binary_op::value;
模板
结构是一元的
{
模板静态自动测试(U*p)->decltype((*p)(std::declval()),void(),std::true_type());
模板静态自动测试(…)->decltype(std::false_type());
静态constexpr bool value=decltype(测试((T*)0))::值;
};
模板静态constexpr bool IsUnaryOp=is_-unary_-op::value;
名称空间详细信息{
模板
结构指针
{
指针对象(Comp-Comp=Comp()):u-Comp(Comp){}
模板<
APtr级,
类BPtr
>
自动运算符()(常数APtr&a、常数BPtr&b)常数
->std::如果启用,则启用<
IsBinaryOp
,bool>
{
返回_comp(*a,*b);
}
模板<
类APtr
>
自动运算符()(常数APtr&a)常数
->std::如果启用,则启用<
伊苏纳约普
,bool>
{
返回公司(*a);
}
公司(Comp);
};
模板
国际热核聚变实验堆结构:国际热核聚变实验堆
{
国际热核实验堆(iter):国际热核实验堆(iter){}
自动运算符*()常量(&O){
返回**静态投影(*此);
}
};
}
模板
自动指针对象(Pred&&Pred)
{
返回细节::指针对象(std::forward(pred));
}
模板
自动反辐射点(Iter&Iter)
{
返回详细信息::德雷夫国际热核试验堆(标准::转发(国际热核试验堆));
}
int main()
{
std::向量v;
//使用指针对象上的less谓词进行排序
std::sort(v.begin()、v.end()、pointee(std::less());
//使用指针对象上的较大谓词进行排序
std::sort(v.begin()、v.end()、pointee(std::greater());
//对每个指针对象应用一元谓词
std::for_each(v.begin()、v.end()、pointee(std::logical_not());
//通过将二进制谓词绑定到值和
//将其转换为一元谓词,添加6
std::transform(v.begin(),v.end(),
deref_pointee(v.begin()),
指针对象(std::bind(std::plus(),6,std::占位符::_1));
}