0

私は次のようなメソッドシグネチャを持っています:

vector<int> findRow(string filename);

さまざまなファイルを入力し、特定の行を返します。このメソッドをさまざまなファイルで使用する予定であり、どのファイルがどの行を返すかを追跡する必要があります。そのため、各ファイル名に一意のファイル番号を割り当て、このファイル番号を使用してファイルとそれに対応する戻り値を参照したいと考えました。どうすればいいですか?私は完全に迷っています!

4

2 に答える 2

0

infoファイル名と行番号を属性として取る構造 (またはクラス) を定義するのはどうですか? のベクトルを定義しinfoます。すべてのファイルについて、名前と行を new に保存infoし、ベクターに追加します。このベクトルへのインデックスは、必要な数です。

于 2012-11-29T18:02:34.537 に答える
0

静的ローカル変数をカウンターとして使用し、1 つまたは 2 つを使用std::unordered_mapして ID をベクターにマップし、場合によっては ID をファイル名にマップできます。

std::unordered_map<int, std::vector<int>> id_to_row;
std::unordered_map<int, std::string> id_to_file;

// This will be the next id to assign to a row/file
// I.e. the current row-id is `id_counter - 1` if `id_counter > 0`
// It can also be seen as the number of mappings made (or rows loaded)
int id_counter = 0;

std::vector<int>& findRow(std::string filename)
{
    // Do your stuff
    for (...)
    {
        ...

        id_to_row[id_counter].push_back(row_value);

        ...
    }

    // Map the id to the filename as well
    id_to_file[id] = filename;

    // Return a reference to the actual vector, while incrementing id value
    return id_to_row[id_counter++];
}
于 2012-11-29T17:50:34.037 に答える