下面我有一个来自文件的名为 line 的字符串。字符串用逗号分隔,第一部分是字符串向量,第二部分是浮点向量。之后的任何内容都不会使用。
第一部分以“文本”的形式出现,这是正确的。但是第二个显示“248”,而应该说“1.234”
我需要帮助正确转换它。任何帮助将不胜感激,谢谢。
我对编程很陌生。对不起任何可怕的风格。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
string line ("test,1.234,4.3245,5.231");
string comma (",");
size_t found;
size_t found2;
string round1;
float round2;
vector<string> vector1;
vector<float> vector2;
// Finds locations of first 2 commas
found = line.find(comma);
if (found!=string::npos)
found2 = line.find(comma,found+1);
if (found2!=string::npos)
//Puts data before first comma to round1 (string type)
for (int a = 0; a < found; a++){
round1 = round1 += line[a];
}
//Puts data after first comma and before second comma to round2 (float type)
for (int b = found+1; b < found2; b++){
round2 = round2 += line[b];
}
//Puts data to vectors
vector1.push_back(round1);
vector2.push_back(round2);
cout << vector1[0] << endl << vector2[0] << endl;
return 0;
}