簡體   English   中英

使用STL類的typdef,例如地圖,列表等

[英]Use typdefs of STL classes like map, list etc

在當前項目中,我正在編程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
}

我的問題是我是否可以用mymap.const_iterator替換std::map<uint64_t, std::list<int32_t> >::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 ...

如果您的編譯器支持auto關鍵字,請使用它。 例如。 Visual Studio 2010和GCC 4.3及更高版本支持它。

std::map<uint64_t, std::list<int32_t> > myMap;
for(auto it = myMap.begin(); it != myMap.end(); ++it){
  // iterate...
}

這對我來說很好

typedef std::map<uint64_t, std::list<int32_t> >::iterator my_map;
std::map<uint64_t, std::list<int32_t> > mymap;
for (my_map it = mymap.begin (); it != mymap.end (); it++)
{
    // iterate here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM