-1

我有大约 100 行文本。每行具有以下格式:1 Gt 1.003Gt更改,长度为 1 到 3 个字符。如何解析这一行并将 存储Gt为字符串、存储1.003为双精度并丢弃1?

4

2 回答 2

1

很正常的东西。

ifstream file(...);
string line;
while (getline(file, line)
{
    istringstream buf(line);
    int dummy;
    string tag;
    double val;
    buf >> dummy >> tag >> val;
}

tag将是“Gt”,val将是 1.003

于 2013-04-10T06:32:04.177 回答
1
using namespace std;
int discardInt;
string strInput;
double dblInput;

vector<string> strings;
vector<double> doubles;

ifstream infile("filename.txt"); //open your file

while(infile >> discard >> strInput >> dblInput) {
  //now discard stores the value 1, which you don't use;
  //strInput stores Gt or other 1-3 character long string;
  //dblInput stores the double

 //operations to store the values that are now in strInput and dblInput
 //for example, to push the values into a vector:
   strings.push_back(strInput);
   doubles.push_back(dblInput);

}

infile.close();
于 2013-04-10T06:32:34.823 回答