【发布时间】:2019-04-25 03:45:41
【问题描述】:
我有一个std::map<std::pair<std::string, std::string>, float>,它占用了太多内存,为了使用更少的内存,我决定将唯一字符串映射到整数(例如,std::map<std::string, int>,其中每个新的唯一字符串都被映射映射到当前的size()),并将这些整数值用作映射的成对键(例如,std::map<std::pair<int, int>, float>)。
我想用std::map::size_type代替int:
using map_index = std::map::size_type;
std::pair<map_index, map_index> key;
当然,这不会编译,因为我需要为地图提供参数列表:
vector.cc:14:19: error: invalid use of template-name `std::map' without an argument list
using map_index = std::map::size_type;
这(理论上)是我想要实现的目标:
using map_index = std::map<std::string, map_index>::size_type;
它给出了以下(预期的)编译器错误:
vector.cc:15:41: error: `map_index' was not declared in this scope
using map_index = std::map<std::string, map_index>::size_type;
让编译器为 std::map 推断正确的 value_type 的正确方法是什么,而 value_type 是它自己的 size_type?
【问题讨论】:
-
次要吹毛求疵:我认为你的最后一句话是错误的。要知道
size_type是什么,您首先需要知道value_type是什么,而不是相反。一旦你知道地图的类型,得到它的size_type就很简单了 -
@user463035818 问题似乎是
size_type是OP 的value_type的一部分。 -
你有一个循环依赖。为什么你不能使用
size_t(这通常是size_type)? -
你能解释一下你为什么想要那个吗? afaik
map::size_type只是一个typedef别名无论如何总是相同的类型 -
std::map<K, V>::size_type最有可能完全独立于K和V。如果你真的在意,可以static_assert(std::is_same_v<Map::size_type, Map::mapped_type>, "Unexpected size_type")
标签: c++ c++11 stl stdmap size-type