HDF5 Cpp - 获取 h5 文件中所有组的名称

HDF5 Cpp - getting names of all groups in h5 file

我想从 h5 文件中获取所有组名。我能够使用收集名称的全局变量来做到这一点。我的问题是没有全局变量怎么办?我不想有任何全局变量,但我有一个需要包含所有组名的向量。

//this is the global variable i am trying to avoid.
std::vector<std::string> myVec;

herr_t op_func(hid_t loc_id, const char* name, const H5O_info_t* info, void opdata)
{
   if(info->type == H5O_TYPE_GROUP)
   {
       myVec.push_back(std::string(name));
   }
}
//here is my function and i pass a vector that i want the names to be in via reference. 
void getGroupNames(hid_t fileId, std::vector<std::string>& someVec)
{
    herr_t status;
    status = H5Ovisit(fileId, H5_INDEX_NAME, H5_ITER_NATIVE, op_func, NULL);
    someVec = myVec;
}

一种方法是在最后一个参数中将 someVec 传递给 H5Ovisit,即:

status = H5Ovisit(fileId, H5_INDEX_NAME, H5_ITER_NATIVE, op_func, static_cast<void*>(&someVec);

然后在op_func直接push进去:

auto vec = static_cast<std::vector<std::string>*>(opdata);
vec->push_back(std::string(name));

(注意op_func的最后一个参数应该是void*