Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 一种多索引,其中一个索引是整个集合的子集_C++_Boost_Boost Multi Index - Fatal编程技术网

C++ 一种多索引,其中一个索引是整个集合的子集

C++ 一种多索引,其中一个索引是整个集合的子集,c++,boost,boost-multi-index,C++,Boost,Boost Multi Index,我想构建一个包含两个排序视图的多索引,其中 我只能从一个视图中删除值 代码: #include <boost/multi_index_container.hpp> #include <boost/multi_index/sequenced_index.hpp> #include <algorithm> #include <iterator> #include <vector> using namespace boost::multi

我想构建一个包含两个排序视图的多索引,其中 我只能从一个视图中删除值

代码:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>

#include <algorithm>
#include <iterator>
#include <vector>

using namespace boost::multi_index;

typedef multi_index_container<
  int,
  indexed_by<
    sequenced<>,
    sequenced<> 
    > 
  > container;

int main()
{
  container c;
  std::vector<int> 
    // complete
    data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 
    // only uneven
    uneven_data;
  std::copy_if(begin(data), end(data), 
               std::back_inserter(uneven_data), 
               [](int i) { return i % 2 != 0; });

  container cont;
  std::copy(begin(data), end(data), std::back_inserter(cont));

  auto& idx0 = cont.get<0>();
  auto& idx1 = cont.get<1>();
  // remove all uneven from idx1
  idx1.remove_if([](int i) { return i % 2 == 0; });

  // behavior I would like to be true, but now both idx0 and idx1 are
  // equal to uneven_data
  assert(std::equal(begin(data), end(data), begin(idx0)));
  assert(std::equal(begin(uneven_data), end(uneven_data), begin(idx1)));

  return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::multi_索引;
typedef多索引容器<
int,
索引<
按顺序排列,
排序
> 
>容器;
int main()
{
容器c;
向量
//完整的
数据={1,2,3,4,5,6,7,8,9,10},
//只是参差不齐
数据不均匀;
std::复制_if(开始(数据),结束(数据),
标准:反向插入器(数据不均匀),
[](int i){返回i%2!=0;});
集装箱运输;
std::copy(开始(数据)、结束(数据)、std::back_插入器(cont));
auto&idx0=cont.get();
auto&idx1=cont.get();
//从idx1上拆下所有不均匀部件
idx1.remove_if([](int i){return i%2==0;});
//行为我希望是真的,但现在idx0和idx1都是真的
//等于不均匀数据
断言(std::equal(begin(数据)、end(数据)、begin(idx0));
断言(std::equal(begin(不均匀_数据)、end(不均匀_数据)、begin(idx1));
返回0;
}

在boost multi_index容器中,索引作为相同值的单独视图耦合在一起。这是boost multi_index_容器的重要保证

“Boost.MultiIndex允许指定 多索引容器,由一个或多个具有不同索引的索引组成 指向相同元素集合“()”的接口

使用两个不同的顺序索引,可以用两种不同的方式重新排列顺序,但它们都必须包含所有值

如果希望根据布尔谓词查看元素的子集(例如:值为奇数),请考虑使用。这不会尝试删除容器中的值,而是在迭代过程中跳过它们

或者,选择/过滤可以通过排序获得,使用具有适当属性的多索引


例如,基于:
[](int i){return i%2;}
的有序非唯一索引将奇数值和偶数值放在不同的组中,因此可以使用。

如上所述,使用。使用两个或多个,可以使一个对象同时参与两个或多个序列

#include <boost/intrusive/list.hpp>
#include <boost/foreach.hpp>

#include <algorithm>
#include <memory>

#include <iostream>
namespace intr = boost::intrusive;
template<class Value>
struct Sequence2
{
    typedef intr::link_mode< intr::auto_unlink > AutoUnlinkMode;
    typedef intr::list_member_hook< AutoUnlinkMode > SeqHook;

    struct Node
    {
        Node( const Value& i ) : value(i) {}

        operator Value&()
        {
            return value;
        }

        Value value;

        SeqHook mainHook;
        SeqHook hook0;
        SeqHook hook1;
    };

    typedef intr::member_hook< Node, SeqHook,&Node::mainHook > UsingMainHook;
    typedef intr::member_hook< Node, SeqHook,&Node::hook0 > UsingSeqHook0;
    typedef intr::member_hook< Node, SeqHook,&Node::hook1 > UsingSeqHook1;

    typedef intr::constant_time_size<false> NonConstantTimeSized;

    typedef intr::list< Node, UsingMainHook, NonConstantTimeSized > NodesList;
    typedef intr::list< Node, UsingSeqHook0, NonConstantTimeSized > Sequence0;
    typedef intr::list< Node, UsingSeqHook1, NonConstantTimeSized > Sequence1;

    NodesList nodes;
    Sequence0 seq0;
    Sequence1 seq1;

    typename NodesList::iterator insert( const Value& item )
    {
        Node* node = new Node(item);

        nodes.push_back( *node );

        typename NodesList::iterator iter = nodes.end(); iter--;
        seq0.push_back( *node );
        seq1.push_back( *node );
        return iter;
    }

    typename Sequence0::iterator iterator0( typename NodesList::iterator iter )
    {
        return seq0.iterator_to( *iter );
    }
    typename Sequence1::iterator iterator1( typename NodesList::iterator iter )
    {
        return seq1.iterator_to( *iter );
    }

    //! Erase from both sequences
    void erase( typename NodesList::iterator at )
    {
        nodes.erase_and_dispose( at, std::default_delete<Node>() );   
    }
    //! Erase from sequence 0
    void erase( typename Sequence0::iterator at )
    {
        Node& n = *at;

        assert( n.hook0.is_linked() );
        if( ! n.hook1.is_linked() )
        {
            seq0.erase_and_dispose( at, std::default_delete<Node>() );   
        }
        else
        {
            seq0.erase(at);
        }
    }
    //! Erase from sequence 1
    void erase( typename Sequence1::iterator at )
    {
        Node& n = *at;

        assert( n.hook1.is_linked() );
        if( ! n.hook0.is_linked() )
        {
            seq1.erase_and_dispose( at, std::default_delete<Node>() );   
        }
        else
        {
            seq1.erase(at);
        }
    }

    ~Sequence2()
    {
        nodes.clear_and_dispose( std::default_delete<Node>() );
    }
};

template< class T >
void show( Sequence2<T>& mseq, const std::string& comment )
{
    std::cout << comment << "\nseq 0:\t";
    BOOST_FOREACH( T& i, mseq.seq0 )
    {
        std::cout << i << " ";
    }
    std::cout << "\nseq 1:\t";
    BOOST_FOREACH( T& i, mseq.seq1 )
    {
        std::cout << i << " ";
    }
    std::cout << "\n\n";
}

int main(void)
{
    Sequence2< std::string > mseq;

    mseq.insert( "." );
    auto iterX = mseq.insert("X");
    auto iterY = mseq.insert("Y");
    auto iterZ = mseq.insert("Z");
    mseq.insert(".");
    mseq.insert(".");

    show(mseq, "start" );

    mseq.seq0.reverse();
    show(mseq, "after reverse seq0");    

    mseq.erase( mseq.iterator0(iterY) );
    show(mseq, "after erase Y in seq0");

    // Update a value in both sequences
    std::string& v = *iterZ;
    v = "z";
    show(mseq, "after modify Z in both");


    mseq.erase( iterX );
    show(mseq, "after erase X in both");

    mseq.erase( iterY );
    show(mseq, "after erase Y in both");

    return 0;
}

它不可能是基于谓词的,也不可能用已删除的
bool
标记每个值。感谢您提供来源丰富的答案(即使问题刚过一岁生日)。这些要求可以通过使用定制的多索引来解决。使用两个或多个,可以使一个对象同时参与两个或多个序列。
start
seq 0:  . X Y Z . . 
seq 1:  . X Y Z . . 

after reverse seq0
seq 0:  . . Z Y X . 
seq 1:  . X Y Z . . 

after erase Y in seq0
seq 0:  . . Z X . 
seq 1:  . X Y Z . . 

after modify Z in both
seq 0:  . . z X . 
seq 1:  . X Y z . . 

after erase X in both
seq 0:  . . z . 
seq 1:  . Y z . . 

after erase Y in both
seq 0:  . . z . 
seq 1:  . z . .