2013-12-15 5 views
0

В чем причина этой ошибки? Я не могу понять, откуда он. Код компилируется и запускается, но после того, как он говорит, что файл загружен, он угасает. Запуск через него в отладчике не принесет пользы, он не находит ошибок.строка итератор + смещение вне диапазона

#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"); 
} 
+5

Какая ошибка? «это волнует» примерно так же непривычно, как и получается, и совершенно бесполезно в попытке диагностировать проблему. – nhgrif

+0

Действительный пункт, извините. Название было ошибкой. его ошибка утверждения отладки, заявляющая, что итератор строки + смещение за пределами диапазона – user2896852

+1

Отклоните свой код, чтобы увеличить ваши шансы получить помощь. –

ответ

1

Проверьте свой файл на наличие пустых строк. Если ваш цикл заполнения края находит пустую строку, это дает вам именно эту ошибку, поскольку вы пытаетесь стереть символы в несуществующих позициях.

Смежные вопросы