Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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++_Containers - Fatal编程技术网

C++ 如何用适当的容器表示冲突?

C++ 如何用适当的容器表示冲突?,c++,containers,C++,Containers,我有许多形状(打字形状)。如果两个形状具有重叠区域,则它们会相互冲突。我想使用一个合适的容器来记录这些形状之间的所有冲突。我尝试的第一个是std::一对的集合,如下所示 std::set<std::pair<Shape*, Shape*>> conflicts; // Add a conflict. Putting shape1 first means shape1 has priority over shape2. conflicts.insert(make_pair

我有许多形状(打字形状)。如果两个形状具有重叠区域,则它们会相互冲突。我想使用一个合适的容器来记录这些形状之间的所有冲突。我尝试的第一个是std::一对的集合,如下所示

std::set<std::pair<Shape*, Shape*>> conflicts;

// Add a conflict. Putting shape1 first means shape1 has priority over shape2.
conflicts.insert(make_pair(&shape1, &shape2));

// Add another where shapes is at the second.
conflicts.insert(make_pair(&shape3, &shape1));
std::设置冲突;
//添加冲突。将shape1放在首位意味着shape1优先于shape2。
冲突。插入(生成一对(&shape1和&shape2));
//在第二个位置添加另一个形状。
冲突。插入(生成一对(&shape3和&shape1));
但是,要获得与形状相关的所有冲突(如shape1)并不容易,因为有时shape1出现在冲突的第二个位置。有什么好办法吗

您可以使用a,其中每个形状都是一个键,值是列表(如果需要快速搜索,它实际上可能是一个列表,例如,通过检查
冲突[X],回答“形状X是否与形状Y发生冲突。计数(Y)
)与之冲突的形状

小型:

#包括
#包括
#包括
使用名称空间std;
结构形状{
int x;
};
int main(){
cout.setf(std::ios::boolalpha);
形状arr[6]={{1}、{2}、{3}、{4}、{5}、{6};//一些形状
虽然会考虑智能指针
c[&arr[0]]。插入(&arr[1]);
c[&arr[1]]。插入(&arr[0]);

你基本上有两个选择:

  • 将两个形状
    a
    b
    之间的每个冲突插入a左右
    (a,b)
    (b,a)
    (设置为双重映射)(需要多重映射,以便为每个形状存储多个冲突)

    在形状本身中维护列表/集或使用从形状到列表/集的映射在绿色中完全相同

  • 使用类似于

    我希望这个解决方案实际上效率和舒适度都会降低


  • 基于Boost.MultiIndex的解决方案:

    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/hashed_index.hpp>
    #include <boost/multi_index/member.hpp>
    #include <boost/shared_container_iterator.hpp>
    #include <vector>
    
    using namespace boost::multi_index;
    
    struct shape{};
    struct collision{shape *sh1,*sh2;};
    
    using collision_track=multi_index_container<
      collision,
      indexed_by<
        hashed_non_unique<member<collision,shape*,&collision::sh1>>,
        hashed_non_unique<member<collision,shape*,&collision::sh2>>
      >
    >;
    
    using shape_ptr_iterator=boost::shared_container_iterator<std::vector<shape*>>;
    
    inline std::pair<shape_ptr_iterator,shape_ptr_iterator>
    collisions(shape* sh,const collision_track& ct)
    {
      boost::shared_ptr<std::vector<shape*>> res(new std::vector<shape*>());
      for(auto p=ct.get<0>().equal_range(sh);p.first!=p.second;p.first++)res->push_back(p.first->sh2);
      for(auto p=ct.get<1>().equal_range(sh);p.first!=p.second;p.first++)res->push_back(p.first->sh1);
      return std::make_pair(
        shape_ptr_iterator(res->begin(),res),
        shape_ptr_iterator(res->end(),res));
    }
    
    #include <iostream>
    
    int main()
    {
      shape sh1,sh2,sh3;
      std::cout<<&sh1<<","<<&sh2<<","<<&sh3<<"\n";
    
      collision_track ct={{&sh1,&sh2},{&sh2,&sh3},{&sh3,&sh1}};
      for(auto p=collisions(&sh1,ct);p.first!=p.second;p.first++)std::cout<<*p.first<<"\n";
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    使用名称空间boost::multi_索引;
    结构形状{};
    结构冲突{shape*sh1,*sh2;};
    使用碰撞跟踪=多索引容器<
    碰撞
    索引<
    散列\u非\u唯一,
    散列非唯一
    >
    >;
    使用shape_ptr_iterator=boost::shared_container_iterator;
    内联std::pair
    碰撞(形状*sh、常量碰撞\U轨迹和ct)
    {
    boost::shared_ptr res(新的std::vector());
    对于(auto p=ct.get().equal_range(sh);p.first!=p.second;p.first++)res->push_-back(p.first->sh2);
    对于(auto p=ct.get().equal_range(sh);p.first!=p.second;p.first++)res->push_-back(p.first->sh1);
    return std::make_pair(
    形状迭代器(res->begin(),res),
    shape_ptr_迭代器(res->end(),res));
    }
    #包括
    int main()
    {
    形状sh1、sh2、sh3;
    
    std::cout仅仅是一个澄清就不会让
    multimap
    多次插入相同的值(例如
    mymm.insert({shape1,shape2});…mymm.insert({shape1,shape2});
    )然后
    mymm
    将包含
    shape2
    两次
    shape1
    是的,但重要的一点,我为什么说使用多重映射,就是对于每个形状,都可能有多个相互冲突的其他形状。我认为问题并没有那么严重(虽然如果OP决定计算碰撞次数,可能会引入一些微妙的错误,想象超过X次的碰撞将意味着什么),但它确实感觉有点奇怪,因为一个形状可以与同一个形状碰撞不止一次,我不知道,也许我是奇数:)使用您自己的类而不是对如何?自定义相等运算符可以使[a,B]=[B,a]。您还必须实现与此一致的小于运算符。这些是冲突,而不是“冲突”。
    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/hashed_index.hpp>
    #include <boost/multi_index/member.hpp>
    #include <boost/shared_container_iterator.hpp>
    #include <vector>
    
    using namespace boost::multi_index;
    
    struct shape{};
    struct collision{shape *sh1,*sh2;};
    
    using collision_track=multi_index_container<
      collision,
      indexed_by<
        hashed_non_unique<member<collision,shape*,&collision::sh1>>,
        hashed_non_unique<member<collision,shape*,&collision::sh2>>
      >
    >;
    
    using shape_ptr_iterator=boost::shared_container_iterator<std::vector<shape*>>;
    
    inline std::pair<shape_ptr_iterator,shape_ptr_iterator>
    collisions(shape* sh,const collision_track& ct)
    {
      boost::shared_ptr<std::vector<shape*>> res(new std::vector<shape*>());
      for(auto p=ct.get<0>().equal_range(sh);p.first!=p.second;p.first++)res->push_back(p.first->sh2);
      for(auto p=ct.get<1>().equal_range(sh);p.first!=p.second;p.first++)res->push_back(p.first->sh1);
      return std::make_pair(
        shape_ptr_iterator(res->begin(),res),
        shape_ptr_iterator(res->end(),res));
    }
    
    #include <iostream>
    
    int main()
    {
      shape sh1,sh2,sh3;
      std::cout<<&sh1<<","<<&sh2<<","<<&sh3<<"\n";
    
      collision_track ct={{&sh1,&sh2},{&sh2,&sh3},{&sh3,&sh1}};
      for(auto p=collisions(&sh1,ct);p.first!=p.second;p.first++)std::cout<<*p.first<<"\n";
    }