如何访问boost :: multi_index :: multi_index_container<>的所有元素他们的指数?

时间:2014-11-24 11:44:31

标签: c++ boost

我正在使用boost::multi_index::multi_index_container<>

以下是我的容器声明:

typedef boost::multi_index::multi_index_container<
  myClssPtr,
  boost::multi_index::indexed_by<
    OrderdBValue //  OrderdBValue  this is boost::multi_index::ordered_unique type
  >
>

我想按顺序访问此容器的所有元素,我该怎么做?

1 个答案:

答案 0 :(得分:2)

问题当然是按什么顺序排列。但让我假设最直接的解释:

typedef boost::multi_index::multi_index_container<
  myClssPtr,
  boost::multi_index::indexed_by<
    OrderdBValue //  OrderdBValue  this is boost::multi_index::ordered_unique type
  >
> container;

for(myClassPtr& e : container.get<0>())
{
    // e.g.:
    std::cout << e << "\n";
}

事实上,看到你只有一个索引,这也是默认(第一个)索引,所以你甚至可以说

for(myClassPtr& e : container)
{
    // e.g.:
    std::cout << e << "\n";
}

UPDATE 对于c ++ 03,语法更加笨拙:

typedef employee_set::nth_index<0>::type idx_type;
for(idx_type::iterator it=container.get<0>().begin(); it != container.get<0>().end(); ++it)
{
     // e.g.
     std::cout << *it << "\n";
}

现在,如果您/表示/ _插入顺序,那么您明确需要添加例如indexed_by<sequenced<> >indexed_by<random_access<> >