c++ - 具有自定义 value_type 的 C++ 映射
问题描述
对于 astd::map<K, V>
默认value_type
值为std::pair<const K, V>
. 有没有办法使用自定义value_type
?据我所知,你不能。
编辑:要清楚,自定义value_type
可能是这样的:
struct Edge {
K from;
V to;
int calculate_thing();
void print_debug();
};
例如,假设我有一些我不想像这样更改的现有功能:
template<typename It>
void processEdges(It begin, It end) {
for(auto it = begin; it != end; ++it) {
do_stuff(it->from);
do_more_stuff(it->calculate_thing());
}
}
解决方案
总是这样std::pair<const K, V>
,你无法改变。
如果您需要 custom value_type
,也许您可以使用std::set
(最好使用透明的 comaprator)。
推荐阅读
- docker - 为什么即使我指定了 --sslcert 和 --sslkey,Portainer 仍会忽略我的证书?
- sql - Oracle SQL,计算未按计划进行
- c# - C# 如何将代码附加到正在运行的 .NET .EXE 的末尾,最好是从该 .EXE 内部?
- flutter - 如何检查一个字母是大写还是小写?- Flutter
- c - 从代码索引 int 数组逆向工程数组维度?
- java - 是否可以将自定义对象传递到 SpringBoot 的端点?
- aws-cdk - AWS CDK 获取 Pinpoint 项目/应用程序 ID
- android - JSON 值不会插入 ArrayList
- php - 如何获取响应对象的“变量”
- python - 推送到远程服务器时出现 Jupyter 实验室错误