读取输入文件时的无限循环? [英] Infinite loop when reading in input file?
问题描述
我在代码中得到一个无限循环 while(input.find('',pos1)!= string :: npos)
通过重定向的输入并创建顶点的映射和图形的字符的向量。它不是很优雅,所以如果你想建议一个更有效的方式阅读输入,那么这也是好的。感谢!
void MSTapp :: processFile()
{
int pos1;
int pos2;
map< char,Vertex *> adjacencyList;
矢量< char> listOrder;
string input;
bool test = false;
while(getline(cin,input)){
pos1 = pos2 = 0;
if(std :: string :: npos!= input.find_first_of(0123456789))
{
char source = input [0];
char destination = input [2];
stringstream ss(input.substr(4));
int weight;
ss>>重量;
Edge newEdge(destination,weight);
adjacencyList [source] - > addEdge(destination,newEdge);
Edge roadBack(source,weight);
adjacencyList [destination] - > addEdge(source,roadBack);
}
else
{
while(input.find('',pos1)!= string :: npos)
{
pos2 = input。 find('',pos1);
char vertex = input [pos1];
listOrder.push_back(vertex);
Vertex * newVertex = new Vertex(vertex);
adjacencyList.insert(make_pair(vertex,newVertex));
pos1 = pos2 + 1;
};
};
};
图形图表(listOrder,adjacencyList);
prim(graph,adjacencyList [listOrder [0]]);
}
输入
ABCDEFG
AB 3
AE 4
BC 7
BE 6
BF 5
CD 9
CF 8
DF 9
DG 4
EF 6
FG 8
while(input.find(' ',pos1)!= string :: npos)
{
pos2 = input.find('',pos1);
char vertex = input [pos1];
listOrder.push_back(vertex);
Vertex * newVertex = new Vertex(vertex);
adjacencyList.insert(make_pair(vertex,newVertex));
pos1 = pos2;
};
更改 pos1 = pos2;
至 pos1 = pos2 + 1;
- 它从不移动,因此 while
b
您还需要确保 pos1<
。 while
条件下的字符串:: length
I get an infinite loop in the code while(input.find(' ', pos1) != string::npos)
I created this code simply to read in the input via redirection and create a map of vertexes and a vector of characters for a graph. It's not very elegant so if you want to suggest a more effective way of reading in the input then that's good too. Thanks!
void MSTapp::processFile()
{
int pos1;
int pos2;
map<char, Vertex*> adjacencyList;
vector<char> listOrder;
string input;
bool test = false;
while (getline(cin, input)) {
pos1 = pos2 = 0;
if(std::string::npos != input.find_first_of("0123456789"))
{
char source = input[0];
char destination = input[2];
stringstream ss(input.substr(4));
int weight;
ss >> weight;
Edge newEdge(destination, weight);
adjacencyList[source]->addEdge(destination, newEdge);
Edge roadBack(source, weight);
adjacencyList[destination]->addEdge(source, roadBack);
}
else
{
while(input.find(' ', pos1) != string::npos)
{
pos2 = input.find(' ', pos1);
char vertex = input[pos1];
listOrder.push_back(vertex);
Vertex* newVertex = new Vertex(vertex);
adjacencyList.insert(make_pair(vertex, newVertex));
pos1 = pos2 + 1;
};
};
};
Graph graph(listOrder, adjacencyList);
prim(graph, adjacencyList[listOrder[0]]);
}
Input
A B C D E F G
A B 3
A E 4
B C 7
B E 6
B F 5
C D 9
C F 8
D F 9
D G 4
E F 6
F G 8
Here's the problem:
while(input.find(' ', pos1) != string::npos)
{
pos2 = input.find(' ', pos1);
char vertex = input[pos1];
listOrder.push_back(vertex);
Vertex* newVertex = new Vertex(vertex);
adjacencyList.insert(make_pair(vertex, newVertex));
pos1 = pos2;
};
Change pos1 = pos2;
to pos1 = pos2+1;
-- it never moves, so the while
loop never ends.
You also need to make sure pos1 < string::length
in your while
condition.
这篇关于读取输入文件时的无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!