Boost文档没有详细介绍,但是有一个(可选的) KeyCompare函数可以传递给ptree。
有谁有使用自定义KeyCompare函数的好例子吗?
我最近一直在使用一个非常慢的ptree。我的键是长字符串(路径),我假设是字符串比较使它变慢。
从我收集到的信息来看,默认的KeyCompare是std::less(),我想要更改这一点。我认为只是比较两个字符串的哈希值。
不用说(但我还是要说),我将为键使用一个不同的对象来促进这一点:具有(std::string+hash)的东西,而不仅仅是std::string。哈希将在构造过程中计算。
谢谢,瑞克。
发布于 2013-10-21 23:25:17
在boost源代码中发现了以下内容:不区分大小写的KeyCompare示例:
template<class T>
struct less_nocase
{
typedef typename T::value_type Ch;
std::locale m_locale;
inline bool operator()(Ch c1, Ch c2) const
{
return std::toupper(c1, m_locale) < std::toupper(c2, m_locale);
}
inline bool operator()(const T &t1, const T &t2) const
{
return std::lexicographical_compare(t1.begin(), t1.end(),
t2.begin(), t2.end(), *this);
}
};然后,您需要做的就是将其传递给basic_ptree类:
typedef basic_ptree<std::string, std::string,
less_nocase<std::string> > iptree;https://stackoverflow.com/questions/16226363
复制相似问题