gpt4 book ai didi

c++ - boost multi_index修改成员并保留序列

转载 作者:太空狗 更新时间:2023-10-29 23:07:34 27 4
gpt4 key购买 nike

我正在使用 boost::multi_index 容器。基本上它包括一些数据加上数据集信息是否完整的信息(当一个项目被添加到容器时,信息还没有完成)。容器已排序,因为我必须知道订单项已添加到容器中。

#include <boost/multi_index_container.hpp>    
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/key_extractors.hpp>


struct indexed_struct
{
unsigned int identifier;
unsigned int iterationFinished;

indexed_struct():
identifier(0), iterationFinished(0){}

indexed_struct(unsigned int identifier, unsigned int iterationFinished):
identifier(identifier), iterationFinished(iterationFinished){}



friend std::ostream& operator<<(std::ostream& os,const indexed_struct& c)
{
os<<c.identifier<<std::endl;
return os;
}
};

struct identifierTag {};
struct finishedTag {};


typedef boost::multi_index::multi_index_container<
indexed_struct,
boost::multi_index::indexed_by<boost::multi_index::sequenced<>,
boost::multi_index::ordered_unique<boost::multi_index::tag<identifierTag>,
BOOST_MULTI_INDEX_MEMBER(indexed_struct, unsigned int, identifier)
>,
boost::multi_index::ordered_non_unique<boost::multi_index::tag<finishedTag>,
BOOST_MULTI_INDEX_MEMBER(indexed_struct, unsigned int, iterationFinished)
>
>
> cmm_iteration_table;



void setFinished(cmm_iteration_table& table, unsigned int iteration)
{
cmm_iteration_table::index<identifierTag>::type::iterator it;
it = table.get<identifierTag>().find(iteration);
indexed_struct mod(*it);
mod.iterationFinished = 1;
table.get<identifierTag>().replace(it, mod);

}


void main()
{
cmm_iteration_table table;

//add some items with iterationFinished set to 0
table.push_back(indexed_struct(30,0));
table.push_back(indexed_struct(20,0));
table.push_back(indexed_struct(40,0));

//now set iterationFinished to 1 in a random order
setFinished(table, 30);
setFinished(table, 40);
setFinished(table, 20);

//try to get iterator for iterationFinished == 1 and sequenced
//30-20-40 as added

std::copy(table.get<finishedTag>().equal_range(1).first, table.get<finishedTag>().equal_range(1).second, std::ostream_iterator<indexed_struct>(std::cout)); //outputs 20,40,30 but 30, 20, 40 is intended

std::copy(table.begin(), table.end(), std::ostream_iterator<indexed_struct>(std::cout)); //outputs 30,20,40, but would also output items where iterationFinished == 0

}

我想获得一个迭代器对,迭代 iterationFinished 设置为 1 的已排序项。

第一个 std::copy 带有指向 iterationFinished 标志的索引,但顺序不正确(意思是我希望顺序作为被插入容器的项目)

第二个 std::copy 给出了正确的顺序,但也会打印 iterationFinished == 0 的项目。

有什么提示吗?

最佳答案

尝试使用 boost::filter_iterator

像这样:

struct is_finished 
{
bool operator()(indexed_struct const& x) { return x.iterationFinished; }
};

typedef cmm_iteration_table::index<identifierTag>::type::iterator iterator_type;

iterator_type it, ite;
it = table.get<identifierTag>().begin();
ite = table.get<identifierTag>().end();

typedef boost::filter_iterator<is_finished, iterator_type> filter_iter_t;
filter_iter_t f_it(is_finished(), it, ite), f_ite(is_finished(), ite, ite);

std::copy(f_it, f_ite, std::ostream_iterator<indexed_struct>(std::cout));

关于c++ - boost multi_index修改成员并保留序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12411528/

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