gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:25 25 4
gpt4 key购买 nike

在当前项目中,我正在编写 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com