gpt4 book ai didi

c++ - 如何通过索引访问 boost::multi_index::multi_index_container<> 的所有元素?

转载 作者:行者123 更新时间:2023-11-30 03:59:06 26 4
gpt4 key购买 nike

我正在使用 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
>
>

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

最佳答案

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

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";
}

更新 对于 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";
}

现在,如果您/meant/_insertion 顺序,那么您明确需要添加例如indexed_by<sequenced<> >indexed_by<random_access<> >

关于c++ - 如何通过索引访问 boost::multi_index::multi_index_container<> 的所有元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27104068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com