0

テキスト ファイルから個々の行を読み取る関数を作成しようとしています。各行には 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;
    }
}

概要:

  1. 問題: セパレーターが異なる。

  2. その他のエレガントなソリューション: 一般的に、誰かが問題に対して他のエレガントでクリーンなソリューションを持っていますか?

4

5 に答える 5

1

指定できる複数のセパレーターで文字列を分割できるブースト分割を使用できます。

std::string = line;
std::vector<std::string> parts;

boost::split(parts, line, boost::is_any_of("\t\n,; "));
于 2013-09-18T18:58:42.303 に答える
0

次のコードが役立つ場合があります。

int t1,t2;
double t3;//global variables...
void parse_Vertex_Line(char *str)
{
     int tmp=0;
     char *p=str;
     //extract the vertex-id
     while(*p >='0' && *p <='9')
        tmp = tmp*10 + *(p++) -'0';
     t1=tmp;
     tmp=0;
     p++;
     //now extract the vertex-name..
     while(*p >='0' && *p <='9')
        tmp = tmp*10 + *(p++) -'0';
     t2=tmp;
     return;
}

void parse_Edge_Line(char *str)
{
     //extracting the first two numbers is just the same...
     int tmp=0;
     char *p=str;
     //extract the first vertex-id
     while(*p >='0' && *p <='9')
        tmp = tmp*10 + *(p++) -'0';
     t1=tmp;
     tmp=0;
     p++;
     //now extract the second vertex-id..
     while(*p >='0' && *p <='9')
        tmp = tmp*10 + *(p++) -'0';
     t2=tmp; 
     p++;
     //but extracting a double value is a bit different...
     //extract the weight...
     int before_decimal=0, after_decimal=0;
     while(*p!='.')
         before_decimal = before_decimal*10 + *(p++) -'0';
     p++;
     int no_of_digits=0;
     while(*p>='0' && *p<='9')
     {
         after_decimal = after_decimal*10 + *(p++) -'0';
         no_of_digits++;
     }
     //assign it to the global double variable...
     t3 = before_decimal + (after_decimal/pow(10.0, no_of_digits));
}

ここで行うことは、まず を取得することnumber of vertices(n)です。次に各行を読んでくださいnparse_Vertex_Line毎回関数を呼び出します。次に、を読み、number of edges同様にparse_Edge_Line毎回呼び出します。値を抽出して保存します。

このコードは、ほぼすべての区切り記号で機能します。これがエレガントに見えることを願っています。

于 2013-09-18T16:39:14.707 に答える
0

セパレーターが空白ではないことが確実な場合は、それらをガベージ文字列に投げ込むことができます(例: 以下の場合のセパレーター)

is >> vertexId >> separator >> neighborId >> separator >> weight;
于 2013-09-18T16:33:07.103 に答える