我有一个程序,我拿一个csv文件并拆分字符串并将它们存储到vector<std::pair<string, float>>
整个程序是使用dijkstra算法,但现在不是问题。
我创建了自己的print语句来测试正确的值是否放在适当的位置。但问题是索引0在第一个中没有任何内容,在第二个中没有0,然后其他所有内容都正常工作。
在我的标题中,我声明了我的矢量:
std::vector<std::pair<std::string, float>> curr_graph; //vector that hold a pair, string = currency name, int = bribe cost
我的cpp文件中唯一使用的函数是node_input()和我的打印函数
void graph::node_input()
{
//grabs first line of csv file (aka the nodes)
string s;
getline(cin, s);
for(int i = 0; i < s.length(); i++)
if(s[i] == ',') amount_of_nodes++;
std::stringstream ss(s);
string token;
while(getline(ss, token, ',')) //splits up the string by commas, ex: Dollar (1.2),Peso (6) will be split up
{
std::stringstream sss(token);
string split;
int counter = 0;
string temp;
float bribe = 0;
while(getline(sss, split, ' '))//splits up the newly split up string bu spaces, ex: Dollar (1.2) will be split up by the space
{
if(counter % 2 == 0)//this condition is when the string split is the currency
temp = split;
else //this condition will take to bribe cost and convert to a float
{
split.pop_back();//removes )
split.erase(0,1);//removes (
bribe = std::stof(split.c_str(), 0);//converts to float
}
counter++;
}
curr_graph.push_back(std::make_pair(temp, bribe));
}
//cout << curr_graph.size();
//cout << curr_graph.capacity();
//cout << endl << curr_graph.front() << endl;
print(curr_graph);
}
void graph::print(std::vector<std::pair<std::string, float>> &v)
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i].first << " " << v[i].second << endl;
}
}
我的示例输入文件:(使用&lt;编译)
,Dollar (1.2),Peso (6),Pound (2),Euro (3),Franc (12)
Dollar (1.2),0,1.5,0,2.5,0
Peso (6),0,0,1.2,5.7,0
Pound (2),2,0,0,0,0
Euro (3),0.9,0,3,0,0
Franc (12),0,0,0,15,0
,,,,,
What is the shortest path from Pesos to Euros?,,,,,
这是我的输出,我从我的node_input()函数调用的print语句中获取:
0
Dollar 1.2
Peso 6
Pound 2
Euro 3
Franc 12