开发者

Use typdefs of STL classes like map, list etc

In a current project I am programming C++, and I am using the STL classes map, set and list quite often. Now I am interested if there is a way to clean up some code by using the internal data types. For example:

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
}

My question is if I could replace std::map<uint64_t, std::list<int32_t> >::const_iterator e.g. by mymap.const_iterator, but that does not compile. To quote g++ here:

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’

Any ideas of how to do this? Or is it not possible?


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


If your compiler supports the auto keyword, use that. Eg. Visual Studio 2010 and GCC 4.3 and above support it.

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


This works just fine for me

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜