2013-04-25 67 views
1

Boost文档没有详细说明,但有一个(可选)KeyCompare函数可以传递给ptree。boost :: ptree和KeyCompare函数?

任何人都有一个使用自定义KeyCompare函数的好例子吗?

我最近一直在使用真正慢的ptree。我的键是长字符串(路径),我假设它是字符串比较,使它变慢。

从我可以收集到的,默认的KeyCompare是std :: less(),我想改变它。我认为只是比较两个字符串的哈希值。

不言而喻(但我会说它无论如何),我会使用不同的对象的关键,以促进这一点:有(std :: string +哈希),而不是一个std ::串。哈希将在施工期间计算。

谢谢, Rik。

回答

0

来自增压源代码中发现这一点:不区分大小写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;