C++ 字符串迭代器+;偏移量超出范围

C++ 字符串迭代器+;偏移量超出范围,c++,graph,iterator,C++,Graph,Iterator,这个错误的原因是什么?我不知道它是从哪里来的。代码编译并运行,但在它说文件已加载后,它就崩溃了。在调试器中运行它没有任何好处,它不会发现任何错误 #include <iostream> #include <fstream> #include <map> #include <vector> #include <string> #include <boost/graph/adjacency_list.hpp> #include

这个错误的原因是什么?我不知道它是从哪里来的。代码编译并运行,但在它说文件已加载后,它就崩溃了。在调试器中运行它没有任何好处,它不会发现任何错误

#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/metis.hpp>

using namespace std;
using namespace boost;

typedef adjacency_list_traits<
vecS, vecS, undirectedS, listS> GraphTraits;
// type 'Vertex' identifies each vertex uniquely:
typedef GraphTraits::vertex_descriptor Vertex;
// Property type associated to each vertex:
struct VertexProperty {
string name;  // Name of vertex (i.e., "location")
Vertex predecessor; // Predecessor along optimal path.
double distance; // Distance to the goal, along shortest path.
default_color_type color; // for use by dijkstra.
VertexProperty(const string& aName = "") : name(aName) { };
};
// Property type associated to each edge:
struct EdgeProperty {
double weight; // distance to travel along this edge.
EdgeProperty(double aWeight = 0.0) : weight(aWeight) { };
};
// Type of the graph used:
typedef adjacency_list<
vecS, // out-edges stored in vector 
vecS, // vertices stored in vector
undirectedS, // undirected graph (edge don't have a specific direction)
VertexProperty, // properties associated to each vertex.
EdgeProperty // properties associated to each edge.
> Graph;
// Create a global graph object 'g'
Graph g;
// This is a visitor for the dijkstra algorithm. This visitor does nothing special.
struct do_nothing_dijkstra_visitor {
template <typename Vertex, typename Graph>
void initialize_vertex(Vertex u, const Graph& g) const { };
template <typename Vertex, typename Graph>
void examine_vertex(Vertex u, const Graph& g) const { };
template <typename Edge, typename Graph>
void examine_edge(Edge e, const Graph& g) const { };
template <typename Vertex, typename Graph>
void discover_vertex(Vertex u, const Graph& g) const { };
template <typename Edge, typename Graph>
void edge_relaxed(Edge e, const Graph& g) const { };
template <typename Edge, typename Graph>
void edge_not_relaxed(Edge e, const Graph& g) const { };
template <typename Vertex, typename Graph>
void finish_vertex(Vertex u, const Graph& g) const { };
};
int main() {
string tempName1;
string tempName2;
string tempString;
string data2;
double weight;
cout << "please enter the data file name: ";
char strFileName[256];
cin >> strFileName;
// preparing the data
ifstream fin;
fin.open(strFileName);
if (!fin) {
    cerr << "Can't open data file, leaving...\n";
    return EXIT_FAILURE;
}
else{
    cout << "file loaded." << endl << endl;
}
// Create a map to associate names to vertices of the graph:
map<string, Vertex> name2v;
getline(fin, tempString); //Vertices:
getline(fin, tempString); //Location1, Location2, ...
stringstream tempSS(tempString);
while (getline(tempSS, tempName1, ',')) {
    // Add vertex to graph, with name 'tempName1' and 
    //  record the associated Vertex in the name2v map:
    name2v[tempName1] = add_vertex(VertexProperty(tempName1), g);
}
getline(fin, tempString); //Edges:
while (getline(fin, tempString)){ // (Location1, Location2, 6)
    //remove parentheses
    tempString.erase(tempString.begin(), tempString.begin() +  
 tempString.find('(') + 1);
    tempString.erase(tempString.begin() + tempString.find(')'), 
 tempString.end());
    stringstream temp_ss(tempString);
    getline(temp_ss, tempName1, ',');
    getline(temp_ss, tempName2, ',');
    temp_ss >> weight;
    // Add edge to graph, by finding vertices associated 
    //  to tempName1 and tempName2:
    add_edge(name2v[tempName1], name2v[tempName2], EdgeProperty(weight), g);
}
char x;
Vertex current_vertex;
Vertex start_vertex;
Vertex goal_vertex;
cout << endl << "How would you like to process your data file?" << endl;
cout << "1.) shortest path" << endl;
cout << "2.) minimum spanning tree" << endl;
cout << "3.) Travelling Salesman" << endl << endl;
returnQuestion:
cout << "please enter 1,2,3 or Q to quit: ";
cin >> x;
switch (x){
case '1': //do the work for shortest path
    cout << endl << "please enter the location name to start from: ";
    cin >> tempName1;
    cout << endl << "please enter the location name for the destination: ";
    cin >> tempName2;
    // Retrieve the vertices for the start and goal:
    start_vertex = name2v[tempName1];
    goal_vertex = name2v[tempName2];
    cout << g[name2v[tempName1]].name << g[name2v[tempName2]].name;
    dijkstra_shortest_paths(
        g, goal_vertex, //<-- solve to goal 
        get(&VertexProperty::predecessor, g),
        get(&VertexProperty::distance, g),
        get(&EdgeProperty::weight, g),
        identity_property_map(), // index-map
        less<double>(), // compare
        plus<double>(), // combine 
        numeric_limits<double>::infinity(), // infinity
        0.0, // zero
        do_nothing_dijkstra_visitor(),
        get(&VertexProperty::color, g));
    cout << "distances and parents:" << endl;
    // Traverse the vertices from the start to goal,
    //  through the "predecessor" links:
    current_vertex = start_vertex;
    while (current_vertex != goal_vertex) {
        cout << g[current_vertex].name << " " << 
 g[current_vertex].distance;
        current_vertex = g[current_vertex].predecessor;
    };
    cout << g[goal_vertex].name << " " << g[goal_vertex].distance << endl;
    break;
case '2': //do the work for minimum spanning
    break;
case '3': //do the work for travelling salesman
    break;
case 'q':
case 'Q':
    return EXIT_SUCCESS;
    break;
default:
    goto returnQuestion;
}
system("pause");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
typedef邻接列表特征<
向量,向量,无向,列表>图形;
//类型“Vertex”唯一标识每个顶点:
typedef GraphTraits::顶点\描述符顶点;
//与每个顶点关联的特性类型:
结构顶点属性{
字符串名称;//顶点的名称(即“位置”)
顶点前置;//沿最佳路径的前置。
双倍距离;//沿最短路径到目标的距离。
默认颜色类型颜色;//供dijkstra使用。
VertexProperty(const string&aName=“”):名称(aName){};
};
//与每个边关联的特性类型:
结构边属性{
双倍重量;//沿此边移动的距离。
边属性(双aWeight=0.0):权重(aWeight){};
};
//所用图形的类型:
typedef邻接列表<
vecS,//输出存储在vector中的边
vecS,//存储在vector中的顶点
无向,//无向图(边没有特定方向)
VertexProperty,//与每个顶点关联的属性。
EdgeProperty//与每条边关联的属性。
>图形;
//创建全局图形对象“g”
图g;
//这是dijkstra算法的访问者。这位来访者没有做什么特别的事。
结构不做任何事{
模板
void初始化_顶点(顶点u,常数图&g)常数{};
模板
void检查_顶点(顶点u,常数图&g)常数{};
模板
void检查_边(边e,常数图&g)常数{};
模板
void discover_顶点(顶点u,常数图&g)常数{};
模板
空边_松弛(边e,常数图&g)常数{};
模板
空边不松弛(边e,常数图&g)常数{};
模板
void finish_顶点(顶点u,常数图&g)常数{};
};
int main(){
字符串tempName1;
字符串tempName2;
字符串tempString;
字符串数据2;
双倍重量;
cout>strFileName;
//准备数据
流鳍;
财务公开(strFileName);
如果(!fin){

cerr检查文件中是否有空行。如果边填充循环发现空行,当您试图擦除不存在的位置上的字符时,它会给出准确的错误信息。

错误是什么?“它会崩溃”与它得到的一样没有说明性,并且在尝试诊断问题时完全没有帮助。很抱歉,这一点是正确的。标题是错误。这是一个调试断言错误,说明字符串迭代器+偏移量超出范围缩进您的代码,如果只是为了增加您获得帮助的几率。请编辑您的问题以包括这些相关详细信息。谢谢