搜索在一组的shared_ptr&LT的; QString的> [英] Searching in a set of shared_ptr<QString>

查看:217
本文介绍了搜索在一组的shared_ptr&LT的; QString的>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象:

class Object {
public:
  boost::shared_ptr<QString> const& name() const {reutrn _name;}
private:
  boost::shared_ptr<QString> _name;
};

和一个multi_index集

And a multi_index set

typedef
    boost::multi_index_container<
        Object,
        boost::multi_index::indexed_by<
            boost::multi_index::ordered_unique<
                boost::multi_index::const_mem_fun<
                    Object,
                    boost::shared_ptr<QString> const&,
                    & Object::name>,
                StringPointerLess> > >
    ObjectSet;

现在如果我想找到的东西集,我有的QString
我需要它的一个副本堆分配,并创建的shared_ptr

Now If I want to find something in the set and I have QString I need to make a copy of it to allocate in heap and create shared_ptr.

是否有可能避免这种不必要的复制操作,留下一组,因为它是?

Is it possible to avoid this unnecessary copy operation, leaving the set as it is?

推荐答案

一个简单的方法:添加以下成员函数来你的 StringPointerLess 比较predicate:

A simpler way: add the following member functions to your StringPointerLesscomparison predicate:

struct StringPointerLess{
  ...
  bool operator()(boost::shared_ptr<QString> const& x,const QString& y)const{
    return *x<y;
  }
  bool operator()(const QString& x,boost::shared_ptr<QString> const& y)const{
    return x<*y;
  }
  ...
};

,现在你可以通过简单地提供查找所需的的QString

IteratorType find( MyContainerType const& container, QString const& key )
{
   return container.find( key );
}

这背后的魔力在href=\"http://www.boost.org/libs/multi_index/doc/tutorial/basics.html#special_lookup\" rel=\"nofollow\">特殊的查找操作部分 Boost.MultiIndex的文档

The magic behind this is explained at the special lookup operations section in Boost.MultiIndex documentation.

这篇关于搜索在一组的shared_ptr&LT的; QString的&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆