私は次のようなメソッドシグネチャを持っています:
vector<int> findRow(string filename);
さまざまなファイルを入力し、特定の行を返します。このメソッドをさまざまなファイルで使用する予定であり、どのファイルがどの行を返すかを追跡する必要があります。そのため、各ファイル名に一意のファイル番号を割り当て、このファイル番号を使用してファイルとそれに対応する戻り値を参照したいと考えました。どうすればいいですか?私は完全に迷っています!
info
ファイル名と行番号を属性として取る構造 (またはクラス) を定義するのはどうですか? のベクトルを定義しinfo
ます。すべてのファイルについて、名前と行を new に保存info
し、ベクターに追加します。このベクトルへのインデックスは、必要な数です。
静的ローカル変数をカウンターとして使用し、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++];
}