gpt4 book ai didi

c++ - 如何在 Boost Graph Library 中将自定义属性添加到 grid_graph 的边缘?

转载 作者:行者123 更新时间:2023-11-30 05:13:41 26 4
gpt4 key购买 nike

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/chrono.hpp>
#include <algorithm>
#include <ctime>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/array.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream> // for std::cout
#include <utility> // for std::pair
#include <algorithm> // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graphviz.hpp>

using namespace std;
using namespace boost;
using namespace boost::gregorian;


#define DIMENSIONS 2

typedef grid_graph<2> Graph;
typedef graph_traits<Graph> Traits;

// Define a simple function to print vertices
void print_vertex(Traits::vertex_descriptor vertex_to_print) {
std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] << ")" << std::endl;
}

int main(int argc, char* argv[]) {

// Define a 3x5 grid_graph where the second dimension doesn't wrap
// boost::array<std::size_t, 2> lengths = { { 3, 5 } };
boost::array<size_t, 2> lengths = { { 3, 5 } };
Graph graph(lengths);

// Do a round-trip test of the vertex index functions
for (Traits::vertices_size_type v_index = 0;
v_index < num_vertices(graph); ++v_index) {

// The two indicies should always be equal
std::cout << "Index of vertex " << v_index << " is " <<
get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl;

}

// Do a round-trip test of the edge index functions
for (Traits::edges_size_type e_index = 0;
e_index < num_edges(graph); ++e_index) {

// The two indicies should always be equal
std::cout << "Index of edge " << e_index << " is " <<
get(boost::edge_index, graph, edge_at(e_index, graph)) << std::endl;
}


// Print number of dimensions
std::cout << graph.dimensions() << std::endl; // prints "3"

// Print dimension lengths (same order as in the lengths array)
std::cout << graph.length(0) << "x" << graph.length(1) << std::endl; // prints "3x5x7"


// Start with the first vertex in the graph
Traits::vertex_descriptor first_vertex = vertex(0, graph);
print_vertex(first_vertex); // prints "(0, 0)"

// Print the next vertex in dimension 0
print_vertex(graph.next(first_vertex, 0)); // prints "(1, 0)"

// Print the next vertex in dimension 1
print_vertex(graph.next(first_vertex, 1)); // prints "(0, 1)"


}

这是 Boost 库的网格图。但我不知道如何使用 int 权重实现边缘。我应该使用什么属性来做它?我可以遍历边和节点,但只能遍历它们的索引,而不能遍历它们的值。

最佳答案

因此,您使用访问器通过它们的索引获取边,参见例如

要同时附加属性,请参阅例如

关于c++ - 如何在 Boost Graph Library 中将自定义属性添加到 grid_graph 的边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823613/

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