我有一个占用太多内存的
我想使用std::map::size_type代替
当然,这段代码无法编译,因为我需要为 map 提供参数列表:
以下是(预期的)编译器错误信息:
std::map<std::pair<std::string, std::string>, float>
,为了使用更少的内存,我决定将唯一的字符串映射到整数上(例如,std::map<std::string, int>
,其中每个新的唯一字符串被映射到当前 map 的 size()
),并将这些整数值用作 map 的成对键(例如,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;
当然,这段代码无法编译,因为我需要为 map 提供参数列表:
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
是 OP 的value_type
的一部分。 - Some programmer dudesize_t
(通常会成为size_type
的类型)? - Some programmer dudestd::map<K, V>::size_type
很可能与K
和V
完全独立。如果你真的关心,可以使用static_assert(std::is_same_v<Map::size_type, Map::mapped_type>, "Unexpected size_type")
进行断言检查。 - Calethstd::map<std::string, X>
不如使用boost::flyweight更好吗? - felix