TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches

Detailed Description

Specialized functions to read/write a std::map from/to HDF5.

The following code writes a std::map to an HDF5 file, reads the same map and outputs it to stdout:

#include <h5/h5.hpp>
#include <iostream>
#include <map>
#include <string>
int main() {
// create file in read/write mode
h5::file file("map.h5", 'w');
// write map
h5::write(file, "mymap", std::map<std::string, int> { { "a", 1 }, { "b", 2 }, { "c", 3 } });
// read into map
std::map<std::string, int> map;
h5::read(file, "mymap", map);
// output map
for (auto [x, y] : map) std::cout << "(" << x << ", " << y << ")" << std::endl;
}
A handle to an HDF5 file.
Definition file.hpp:43
T read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:73
void write(group g, std::string const &key, T const &x, auto const &...args)
Generic implementation for writing a variable to an HDF5 dataset/subgroup.
Definition generic.hpp:105
Includes all relevant h5 headers.

Output:

(a, 1)
(b, 2)
(c, 3)

Contents of map.h5:

HDF5 "map.h5" {
GROUP "/" {
GROUP "mymap" {
ATTRIBUTE "Format" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SCALAR
DATA {
(0): "Dict"
}
}
DATASET "a" {
DATATYPE H5T_STD_I32LE
DATASPACE SCALAR
DATA {
(0): 1
}
}
DATASET "b" {
DATATYPE H5T_STD_I32LE
DATASPACE SCALAR
DATA {
(0): 2
}
}
DATASET "c" {
DATATYPE H5T_STD_I32LE
DATASPACE SCALAR
DATA {
(0): 3
}
}
}
}
}

Classes

struct  h5::hdf5_format_impl< std::map< keyT, valueT > >
 Specialization of h5::hdf5_format_impl for std::map. More...
 

Functions

template<typename keyT , typename valueT >
void h5::h5_read (group g, std::string const &name, std::map< keyT, valueT > &m)
 Read a std::map from an HDF5 subgroup.
 
template<typename keyT , typename valueT >
void h5::h5_write (group g, std::string const &name, std::map< keyT, valueT > const &m)
 Write a std::map to an HDF5 subgroup.
 

Function Documentation

◆ h5_read()

template<typename keyT , typename valueT >
void h5::h5_read ( group g,
std::string const & name,
std::map< keyT, valueT > & m )

#include <h5/stl/map.hpp>

Read a std::map from an HDF5 subgroup.

Template Parameters
keyTKey type of the std::map.
valueTValue type of the std::map.
Parameters
gh5::group containing the subgroup.
nameName of the subgroup from which the std::map is read.
mstd::map to read into.

Definition at line 88 of file map.hpp.

◆ h5_write()

template<typename keyT , typename valueT >
void h5::h5_write ( group g,
std::string const & name,
std::map< keyT, valueT > const & m )

#include <h5/stl/map.hpp>

Write a std::map to an HDF5 subgroup.

Template Parameters
keyTKey type of the std::map.
valueTValue type of the std::map.
Parameters
gh5::group in which the subgroup is created.
nameName of the subgroup to which the std::map is written.
mstd::map to be written.

Definition at line 57 of file map.hpp.