1
因此,我正在讀入我的程序中的txt文件input.txt。我能夠使用getline
函數獲得第一行數字,現在我正試圖獲取每個數字。我會將這些數字加入BST,但我不關心這部分,只是如何獲得每個數字。從我可以收集我需要使用stringstream,但我堅持如何提取每個數字,並能夠單獨添加它們。使用stringstream從字符串中提取單個值
BST.cpp
#include "stdafx.h"
#include "BST.h"
#include <iostream>
#include <cstddef>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
BST<int> B;
fstream input;
input.open("input.txt", ios::in);
string number;
stringstream ss;
if(input.fail())
cout << "File was not opened successfully" << endl;
else
cout<< "File was opened succesffuly" << endl;
getline(input,number,'\n');
cout << number << endl;
ss << number;
return 0;
}
input.txt中
12 6 9 45 3 7 10 2 4 13
4
9
55 18 3 6 78 9
13
66
808 707 909 1001 505 1200 499 644 1190 1592
707
78
只需在循環中使用正常的輸入運算符'>>',就像使用'std :: cout'之類的任何其他流一樣。 –
@JoachimPileborg所以像ss <<數字?這給了我整條線。 – Bryan