简体   繁体   English

C++:扩展已创建的 C 结构与自定义 hash 和可在 unordered_map 中使用的相等功能

[英]C++: Extend already created C struct with custom hash and equal functions to be usable in unordered_map

I have two projects, one is written in C and the other one in C++.我有两个项目,一个写在 C 中,另一个写在 C++ 中。 I need to use a C-struct in my C++ program in a unordered_map.我需要在我的 C++ 程序中的 unordered_map 中使用 C 结构。 How can I extend the struct by a hash and compare function such that I don't always have to pass those functions to the unordered_map?如何通过 hash 扩展结构并比较 function 以便我不必总是将这些函数传递给 unordered_map?

Define the == operator outside the struct (that's allowed) and specialize std::hash for your struct in a C++ file.在结构外部定义==运算符(这是允许的)并在 C++ 文件中为您的结构专门std::hash See an example at cppreference: https://en.cppreference.com/w/cpp/utility/hash请参阅 cppreference 中的示例: https://en.cppreference.com/w/cpp/utility/hash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM