相关疑难解决方法(0)

检查类型是否为地图

我有时会发现需要编写可以应用于对象容器的通用例程,或者这些容器的映射(即处理映射中的每个容器).一种方法是为地图类型编写单独的例程,但我认为有一个例程适用于两种类型的输入可能更自然,更简洁:

template <typename T>
auto foo(const T& items)
{ 
    return foo(items, /* tag dispatch to map or non-map */);
}
Run Code Online (Sandbox Code Playgroud)

什么是安全,干净的方式来执行此标签发送?

c++ dictionary type-traits c++14

11
推荐指数
2
解决办法
2279
查看次数

具有自定义value_type的C ++映射

对于std::map<K, V>默认值value_typestd::pair<const K, V>。有没有使用自定义的方法value_type?据我所知你做不到。

编辑:要清楚,自定义value_type可能是这样的:

struct Edge {
  K from;
  V to;

  int calculate_thing();
  void print_debug();
};
Run Code Online (Sandbox Code Playgroud)

例如,假设我有一些不想更改的现有功能:

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());
  }
}
Run Code Online (Sandbox Code Playgroud)

c++ dictionary

1
推荐指数
1
解决办法
116
查看次数

标签 统计

c++ ×2

dictionary ×2

c++14 ×1

type-traits ×1