C++ 为什么可以';t我使用无序贴图从boost图形库中打印出该顶点的名称
我正在从一个文件中读取一个边列表,并试图使用boost图形库从中生成一个图形。最终的目标是实现girvan-newman算法,但现在我正在努力读入图形。我以两个无序映射的形式读取中的数据,这两个映射使用开关键和值对进行复制,这样我就可以使用。使用键和值进行查找。这是我的密码:C++ 为什么可以';t我使用无序贴图从boost图形库中打印出该顶点的名称,c++,boost,boost-graph,C++,Boost,Boost Graph,我正在从一个文件中读取一个边列表,并试图使用boost图形库从中生成一个图形。最终的目标是实现girvan-newman算法,但现在我正在努力读入图形。我以两个无序映射的形式读取中的数据,这两个映射使用开关键和值对进行复制,这样我就可以使用。使用键和值进行查找。这是我的密码: #include <iostream> #include <utility> //std::pair #include <algorithm> //std::for_each #i
#include <iostream>
#include <utility> //std::pair
#include <algorithm> //std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/small_world_generator.hpp>
#include <boost/random/linear_congruential.hpp>
#include <fstream>
typedef adjacency_list< vecS, vecS, bidirectionalS, no_property,
property< edge_weight_t, float > >
Graph_type;
typedef std::pair< int, int > Edge;
typedef typename graph_traits< Graph_type >::vertex_descriptor Vertex;
void readInGraph(){
ifstream myFile("../data/put_data_here.txt");
//gets first line and turns it to int
string x;
myFile >> x;
stringstream geek(x);
int numEdges;
geek >> numEdges;
Edge edge_array[numEdges];
string prev = "prev";
string line;
myFile.ignore();
std::unordered_map<string, int> name;
std::unordered_map<int, string> inverseName;
int mapCounter = 0;
string tempVar;
//loops through file to store names
//only loops through first column of edges to minimize lookup time
while(getline(myFile, line)){
string delimiter = " ";
string token = line.substr(0, line.find(delimiter));
if(token != prev){
name[token] = mapCounter;
inverseName[mapCounter] = token;
mapCounter++;
}
prev = token;
}
myFile.clear();
myFile.seekg(0, ios::beg);
myFile >> tempVar; //gets rid of first line of file
myFile.ignore();
// loops through the second column of edges and adds any vertices that weren't
// previously added to the map
while(getline(myFile, line)){
string delimiter = "-";
string token = line.substr(line.find(delimiter) + 2, string::npos);
if(name.count(token) == 0){
name[token] = mapCounter;
inverseName[mapCounter] = token;
mapCounter++;
}
}
int numVertices = name.size();
cout << numVertices;
//now that all the names are collected we can read in the edge array
//and store it as the integer mapped to that name in the map
myFile.clear();
myFile.seekg(0, ios::beg);
myFile >> tempVar; //gets rid of first line of file
myFile.ignore();
for(int i = 0; i < numEdges; i++){
string hyphen; //gets rid of hyphen
myFile >> tempVar;
edge_array[i].first = name.find(tempVar)->second;
myFile >> hyphen;
myFile >> tempVar;
edge_array[i].second = name.find(tempVar)->second;
}
float transmission_delay[] = { 1 }; //no edge weights
Graph_type newGraph(edge_array, edge_array + numEdges, transmission_delay, numVertices);
std::for_each(vertices(newGraph).first, vertices(newGraph).second, exercise_vertex< Graph_type >(newGraph, inverseName));
我已经试了一整天了,我不知道还能试什么。我认为问题出在exercisevertex函数上,因为我在readInGraph函数中做了一些测试,能够列出所有边及其对应的名称。我想这可能与我如何访问地图有关。在最初的代码中,在构造器中,我必须删除这些括号才能编译它,我不知道为什么。我认为这可能与我的问题有关,可能它实际上没有将名称识别为映射,因此.find函数正在工作。我的另一个想法是,get(vertex_id,v)
没有返回映射中的内容,但是如果该值以前可以用作char*name
中的索引,那么我认为搜索我的映射应该可以
以下是我的输入文件的缩写版本,仅供参考:
78
C-B
D-B
D-C
E-B
电子商务
E-D您的初始边权重
transmission_delay[] = {1}; // no edge weights
是单个元素,因此图形构造调用
您需要一个对边数有效的迭代器。这些构造函数有点“不安全”,因为它们使用原始迭代器,因此没有进行检查。
BGL在那里显示出一些年龄
修复该问题(以及C99可变长度数组的使用):imk
请注意,我们现在返回图形,因此主程序如下
Mappings mappings;
auto g = readInGraph("put_data_here.txt", mappings);
auto& invmap = mappings.right;
for (Vertex v : boost::make_iterator_range(vertices(g))) {
std::cout << "Vertex " << v << " name '" << invmap.at(v) << "':\n";
for (auto e : boost::make_iterator_range(out_edges(v, g))) {
auto t = target(e, g);
std::cout << " - adjacent to " << t << " (" << invmap.at(t) << ")\n";
}
}
std::vector<float> transmission_delay(edge_array.size(),
1.f); // no edge weights
int const numVertices = mappings.size();
return Graph_type(edge_array.data(), edge_array.data() + edge_array.size(),
transmission_delay.data(), numVertices);
using Graph_type =
boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS,
boost::no_property,
boost::property<boost::edge_weight_t, float>>;
using Vertex = typename boost::graph_traits<Graph_type>::vertex_descriptor;
using Edge = std::pair<Vertex, Vertex>;
Graph_type readInGraph(std::string const& fname, Mappings& mappings) {
std::ifstream myFile(fname);
// gets first line and turns it to int
unsigned numEdges = 0;
myFile >> numEdges;
std::vector<Edge> edge_array;
for (std::string src, hyphen, tgt;
edge_array.size() < numEdges && myFile >> src >> hyphen >> tgt;) {
// combined lookup/insert:
auto s = mappings.insert(Mappings::value_type(src, mappings.size()))
.first->get_right();
auto t = mappings.insert(Mappings::value_type(tgt, mappings.size()))
.first->get_right();
// now that all the names are collected we can read in the edge array
// and store it as the integer mapped to that name in the map
edge_array.emplace_back(s, t);
}
std::vector<float> transmission_delay(edge_array.size(),
1.f); // no edge weights
int const numVertices = mappings.size();
return Graph_type(edge_array.data(), edge_array.data() + edge_array.size(),
transmission_delay.data(), numVertices);
}
Mappings mappings;
auto g = readInGraph("put_data_here.txt", mappings);
auto& invmap = mappings.right;
for (Vertex v : boost::make_iterator_range(vertices(g))) {
std::cout << "Vertex " << v << " name '" << invmap.at(v) << "':\n";
for (auto e : boost::make_iterator_range(out_edges(v, g))) {
auto t = target(e, g);
std::cout << " - adjacent to " << t << " (" << invmap.at(t) << ")\n";
}
}
Vertex 0 name 'C':
- adjacent to 1 (B)
Vertex 1 name 'B':
Vertex 2 name 'D':
- adjacent to 1 (B)
- adjacent to 0 (C)
Vertex 3 name 'E':
- adjacent to 1 (B)
- adjacent to 0 (C)
- adjacent to 2 (D)