귀하의 설명 당신은 아마 그래프 표현을 찾고 있음을 알려줍니다. 당신은 여기 부스트 그래프를 사용할 수
:
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)
를 사용하여 속도를 높일 수는 하나의 개체가 여러 가지 다른 객체에 연결 될 수 있습니까? 양방향 참조 (5 -> 7, 7 -> 5) 또는 한 방향 (5 -> 7) 만 필요합니까? –
양방향 참조 및 예 한 객체가 여러 객체와 연결될 수 있습니다! –
제약 조건이 너무 빡빡하고 요구 사항이 너무 짧습니다.벡터에 대한 요소에 유효한 포인터 나 반복자를 저장할 수는 없습니다. (벡터가 그 이후에 결코 커지지 않는다는 것을 보장하지 않는 한) 나는 그렇게하려고 시도조차 권하지 않습니다. 색인을 저장할 수 없습니까? 아니면 어떤 종류의 고유 색인입니까? 그런 다음 벡터 대신지도를 사용 하시겠습니까? 모두 꽤 생선 ... –