【问题标题】:Vector of objects with identification numbers and connections带有标识号和连接的对象矢量
【发布时间】:2014-12-04 09:59:10
【问题描述】:

我正在尝试这样做:

我想要一个向量(std::vector),其对象定义如下:

class MyObj{
   private:
       idNumber= // it could be a pointer
       firstConnection= // it could be a pointer
       secondConnection= // it could be a pointer
   public:
   ...
};

vector<MyObj> vectorOfObj(10);

所以我想要对象的idNumber,它是vector的一个元素,对应同一个元素的索引,并设置元素之间的连接(双向)(例如vectorOfObj[5]与@连接987654324@ 和 vectorOfObj[4]) 如果向量的结构被修改(例如,如果元素 6 设置在 4 中并且 4 设置在 6 中,则对象会更改),我希望元素保持其引用有效(id 和连接)他们的 id 和如果 5 与 4 连接,现在与 6 连接)。如果值得,我接受所有类型的解决方案。我想了很多,但我找不到解决方案。

我希望我很清楚。谢谢!

【问题讨论】:

  • 一个对象可以连接到其他几个对象吗?您需要双向引用(5 -> 7、7 -> 5)还是仅在一个方向(5 -> 7)?
  • 双向引用,是的,一个对象可以连接多个对象!
  • 你的约束太紧,你的要求太简短。您将 能够存储指向向量元素的有效指针或迭代器(除非您保证该向量此后永远不会增长),我什至不建议尝试这样做。你不能存储索引?或者一些 other 类型的唯一索引?然后使用地图而不是矢量?都很可疑......
  • 是的。可以使用地图,我不是专家,所以所有建议对我都有好处!很抱歉出现问题!

标签: c++ object vector


【解决方案1】:

你可以这样使用

map<yourcommonthing, vector<MyObj>> yourMap;

那么所有有共同点的MyObj是什么,你可以按照map的key来分组。

如果 idNumber 是一个或多个 MyObj 之间的共同点,那么,

map<idNumber, vector<MyObj>> vectorOfObj;

【讨论】:

    【解决方案2】:

    所以,你有一个普通的Graph。 您可以通过两种主要方式来存储参考:

    我认为最简单的解决方案是使用 edjes 列表,

    std::vector<std::pair<int, int>> connections;
    

    并且每次您想要找到连接点时,您都​​必须浏览所有连接的整个列表。 如果你有很多连接(thouthands 和更多),你可以通过使用来加速它

    std::multimap<int, int> conncections
    .....
    // to find all connected points:
    std::pair <std::multimap<int,int>::iterator, std::multimap<int,int>::iterator> ret;
    ret = conncections.equal_range(ch);
    

    【讨论】:

      【解决方案3】:

      您的描述告诉我您可能正在寻找图形表示。

      您可以在此处使用 Boost Graph:

      Live On Coliru

      #include <boost/graph/adjacency_list.hpp>
      #include <boost/range.hpp> // make_iterator_range
      #include <iostream>
      #include <iomanip> // for std::setw
      
      using namespace boost;
      
      struct MyObj{
          int idNumber;
      };
      
      typedef adjacency_list<vecS, vecS, bidirectionalS, MyObj> Graph;
      
      int main() {
          Graph g;
      
          Graph::vertex_descriptor // essentially, index into the vector of MyObj
              node1 = add_vertex(MyObj {42}, g),
              node2 = add_vertex(MyObj { 7},  g),
              node3 = add_vertex(MyObj {99}, g),
              node4 = add_vertex(MyObj {-1}, g);
      
          std::cout << "node1: " << node1 << "\n"; // 0
          std::cout << "node2: " << node2 << "\n"; // 1
          std::cout << "node3: " << node3 << "\n"; // 2
          std::cout << "node4: " << node4 << "\n"; // 3
      
          add_edge(node1, node3, g);
          add_edge(node2, node3, g);
          add_edge(node4, node1, g);
      
          // now we have a graph with these connections:
          for(auto const& connection: make_iterator_range(edges(g)))
          {
              Graph::vertex_descriptor sd = source(connection, g);
              Graph::vertex_descriptor td = target(connection, g);
              MyObj const& s = g[sd];
              MyObj const& t = g[td];
      
              std::cout << "Connection of " << sd << " (idNumber=" << std::setw(2) << s.idNumber << ") <-> "
                                            << td << " (idNumber=" << std::setw(2) << t.idNumber << ")\n";
          }
      
      }
      

      输出:

      node1: 0
      node2: 1
      node3: 2
      node4: 3
      Connection of 0 (idNumber=42) <-> 2 (idNumber=99)
      Connection of 1 (idNumber= 7) <-> 2 (idNumber=99)
      Connection of 3 (idNumber=-1) <-> 0 (idNumber=42)
      

      【讨论】:

        猜你喜欢
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-25
        相关资源
        最近更新 更多