c++ - 使用 STL 类的 typdef,如 map 、列表等

标签 c++ list stl dictionary types

在当前项目中,我正在编写 C++ 程序,而且我经常使用 STL 类映射、集合和列表。现在我很感兴趣是否有一种方法可以通过使用内部数据类型来清理一些代码。例如:

std::map<uint64_t, std::list<int32_t> > mymap;
// add something to the map
for (std::map<uint64_t, std::list<int32_t> >::const_iterator it = mymap.begin (); it != mymap.end (); it++) {
    // iterate here
}

我的问题是我是否可以替换 std::map<uint64_t, std::list<int32_t> >::const_iterator例如通过 mymap.const_iterator ,但这不编译。在这里引用 g++:

error: invalid use of ‘std::map<long long unsigned int, std::list<int, std::allocator<int> >, std::less<long long unsigned int>, std::allocator<std::pair<const long long unsigned int, std::list<int, std::allocator<int> > > > >::const_iterator’

关于如何做到这一点的任何想法?还是不可能?

最佳答案

typedef std::map<uint64_t, std::list<int32_t> > mymaptype;
mymaptype mymap;
for (mymaptype::const_iterator ...

关于c++ - 使用 STL 类的 typdef,如 map 、列表等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5457806/

相关文章:

c++ - std::map 和性能,相交集

c++ - 相互依赖的类模板和 std::is_base_of 特化

python - 打乱列表并返回副本

python - 如果列表包含字符串,则打印列表中包含它的所有索引/元素

java - Java中的列表列表

c++ - 无法在 Visual Studio 2015 中将 typedef 转换为 std::pair

c++ - 为 std::unordered_map 中的 const std::reference_wrapper 重载 operator==

c++ - 确定两个字符串是否因相邻字符交换而不同的最快方法? C++

c++ - destroyWindow(来自 opencv)关闭所有窗口并停止 c++ 程序

c++ - 使用 Visual C++ 在单独的文件中提取调试信息