Automatic Type Conversion for STL Containers
Pybind11 automatically converts C++ STL containers like `std::vector`, `std::map`, and `std::set` to their Python equivalents (lists, dictionaries, sets) and vice versa, by including `pybind11/stl.h`. This significantly reduces boilerplate code for data exchange.
The top 3
- Pybind11's Top 3 Direct STL-Python Mappings: Pybind11 automatically bridges `std::vector`, `std::list`, `std::array` to Python lists; `std::set`, `std::unordered_set` to Python sets; and `std::map`, `std::unordered_map` to Python dictionaries, by including the `pybind11/stl.h` header.
- Recursive Conversion: Pybind11's Nested STL Prowess: Pybind11 seamlessly handles arbitrarily nested STL containers, automatically converting complex C++ structures like `std::vector<std::map<std::string, int>>` to their corresponding Python nested list/dict types.
- Extending Auto-Conversion to Custom C++ Types in STL: Pybind11's automatic type conversion supports STL containers holding user-defined C++ types, provided these custom types have their own defined pybind11 bindings, enabling comprehensive data structure integration.
Sources
Open the full topic