HDF5存储不同大小的字符串属性
HDF5 Store string attributes with different sizes
- 语言:c++
- 软件包:HDF5,版本1.6.8
我目前正在使用以下代码来存储一个真实的数组作为属性:
oid storeStringAttribute(H5::H5Location& group, string name, vector<double>& array)
{
hsize_t dims[1];
dims[0] = array.size();
H5::DataSpace dataspace = H5::DataSpace(1, dims);
H5::Attribute attribute = group.createAttribute(name.c_str(), NATIVE_DOUBLE, dataspace);
attribute.write(H5::PredType::NATIVE_DOUBLE, vec.data());
}
我想写一个类似的代码来存储vector<string>
数组有没有一种简单的方法来存储可变大小的字符串数组
我目前正在做的是使用更大的尺寸,但这不是很有效。
void storeStringAttribute(H5::H5Location& group, string name, vector<string>& array)
{
hsize_t dims[1];
dims[0] = array.size();
size_t maxStringSize = 0;
for(size_t i=0; i<array.size(); i++)
{ maxStringSize = std::max(maxStringSize, array.size()); }
H5::StrType strdatatype(H5::PredType::C_S1, maxStringSize+1);
H5::DataSpace dataspace = H5::DataSpace(1, dims);
H5::Attribute attribute = group.createAttribute(name.c_str(), strdatatype, dataspace);
attribute.write(strdatatype, vec.data());
}
答案如下:
void storeStringAttribute(H5::H5Location& group, string name, vector<string>& array)
{
hsize_t dims[1];
dims[0] = array.size();
H5::StrType strdatatype(H5::PredType::C_S1, maxStringSize+1);
strdatatype.setSize(H5T_VARIABLE);
H5::DataSpace dataspace = H5::DataSpace(1, dims);
H5::Attribute attribute = group.createAttribute(name.c_str(), strdatatype, dataspace);
attribute.write(strdatatype, vec.data());
}
相关文章:
- C++ 命名参数习惯用语 - 未设置字符串属性
- c++ 构造函数 将 1 个字符串参数转换为 3 个属性
- 如何将字符串属性设置为 QTreeWidgetItem?
- 字符串的异常行为会追加属性 C++
- 将 Qt 属性枚举值打印为键字符串
- cocos2dx 将值强制转换为属性字符串
- 如何使用 boost 属性树来解析 boost 中 json 字符串中的数组中的元素?
- MFC SDI在更改属性时从INI读取并更新视图字符串
- 如何在 QML 文件中查找属性名称为字符串的 QML 上下文属性
- 在C++中使用字符指针将字符串保存到类属性
- 如何将标准::字符串格式的 JSON 解码为 Boost 属性树
- HDF5存储不同大小的字符串属性
- C++11 字符串属性和 gcc 版本
- 提升精神继承的字符串属性填充<int> ASCII 字符中的向量
- 如何使用tinyxml2查询字符串属性
- C++ / 类编译和字符串属性:"expected `)' before '=' token"
- 从字符串属性中查找类对象
- 写一个逗号分隔的字符串属性到HDF5数据集c++
- 将包含字符串属性的结构读取/写入二进制文件
- c ++如何在向量中的特定位置显示元素的字符串属性