テキスト ファイルから個々の行を読み取る関数を作成しようとしています。各行には 2 つまたは 3 つの列があります。そのための最もエレガントでクリーンなアプローチを知りたいです。さまざまなセパレーターを操作する機能が必要です(\t,\n,' ',',',';')
。
私のアプローチは、異なるセパレーターを除いて正しく機能します。
例: 入力:
6
0 0
1 1
2 2
3 3
4 4
5 5
10
0 1 0.47
2 0 0.67
3 0 0.98
4 0 0.12
2 1 0.94
3 1 0.05
4 1 0.22
3 2 0.24
4 2 0.36
4 3 0.69
パターン入力:
[total number of vertices]
[id-vertex][\separetor][name-vertex]
...
[total number of edges]
[id-vertex][\separator][id-neighbor][\separetor][weight]
...
*\separetor=\t|\n|' '|','|';'
私のアプローチ:
void readStream(istream& is, const char separator) {
uint n, m;
is >> n;
cout << n << endl;
string name;
uint vertexId, neighborId;
float weight;
while(!is.eof()) {
for(uint i = 0; i < n; i++) {
is >> vertexId >> name;
cout << vertexId;
cout << " " << name << endl;
}
is >> m;
cout << m << endl;
for(uint j = 0; j < n; j++) {
is >> vertexId >> neighborId >> weight;
cout << vertexId;
cout << " " << neighborId;
cout << " " << weight << endl;
}
break;
}
}
概要:
問題: セパレーターが異なる。
その他のエレガントなソリューション: 一般的に、誰かが問題に対して他のエレガントでクリーンなソリューションを持っていますか?