探索C++ STL Map模板类[额外发现.max_size 的值如何计算]
首先说下 .max_size() 的值 ,经过测试,发现 应该 是 这样的,看代码和注释就行。
/**** .max_size 取决于 <key Value> 所占字节比较大的一个。然后用4个字节的数字(unsigned_int_max=40亿左右) 除以2除以 所占字节就是这个值了。 ****/ int map_max_size () { int i; map<int,int> mymap; cout<<"mymap.max_size()=" << mymap.max_size() <<endl; if (mymap.max_size()>1000) { for (i=0; i<1000; i++) mymap[i]=0; cout << "The map contains 1000 elements.\n"; } else cout << "The map could not hold 1000 elements.\n"; cout<<"mymap2<char,char 11>.max_size()=" << map<char,char>().max_size() <<endl; cout<<"mymap2<char,short 12>.max_size()=" << map<char,short>().max_size() <<endl; cout<<"mymap2<short,char 21>.max_size()=" << map<short,char>().max_size() <<endl; cout<<"mymap2<short,short 22>.max_size()=" << map<short,short>().max_size() <<endl; cout<<"mymap2<int,int 44>.max_size()=" << map<int,int>().max_size() <<endl; cout<<"mymap2<unsigned int,unsigned int 44>.max_size()=" << map<unsigned int,unsigned int>().max_size() <<endl; cout<<"mymap2<long long,long long 88>.max_size()=" << map<long long,long long>().max_size() <<endl; cout<<"mymap2<long long,int 84>.max_size()=" << map<long long,int>().max_size() <<endl; cout<<"mymap2<long long,short 82>.max_size()=" << map<long long,short>().max_size() <<endl; cout<<"mymap2<long long,char 81>.max_size()=" << map<long long,char>().max_size() <<endl; cout<<"mymap2<char,long long 18>.max_size()=" << map<char,long long>().max_size() <<endl; cout<<"mymap2<short,long long 28>.max_size()=" << map<short,long long>().max_size() <<endl; cout<<"mymap2<int,long long 48>.max_size()=" << map<int,long long>().max_size() <<endl; return 0; /* @@@正在进行[map_max_size()]的测试 mymap.max_size()=536870911 The map contains 1000 elements. mymap2<char,char 11>.max_size()=2147483647 mymap2<char,short 12>.max_size()=1073741823 mymap2<short,char 21>.max_size()=1073741823 mymap2<short,short 22>.max_size()=1073741823 mymap2<int,int 44>.max_size()=536870911 mymap2<unsigned int,unsigned int 44>.max_size()=536870911 mymap2<long long,long long 88>.max_size()=268435455 mymap2<long long,int 84>.max_size() =268435455 mymap2<long long,short 82>.max_size()=268435455 mymap2<long long,char 81>.max_size() =268435455 mymap2<char,long long 18>.max_size() =268435455 mymap2<short,long long 28>.max_size()=268435455 mymap2<int,long long 48>.max_size() =268435455 @@@[map_max_size()]的测试结束 */ }
再附上对其他map模板类的测试代码
查看代码
/************************** 参考资料:http://www.cplusplus.com/reference/stl/map/ C++的FAQ http://www.cplusplus.com/info/faq/ http://topic.csdn.net/t/20040119/20/2676783.html **************************/ // constructing maps #include <iostream> #include <string> #include <map> using namespace std; int map_operate_equal () { map<char,int> first; map<char,int> second; first['x']=8; first['y']=16; first['z']=32; second=first; // second now contains 3 ints first=map<char,int>(); // and first is now empty cout << "Size of first: " << int (first.size()) << endl; cout << "Size of second: " << int (second.size()) << endl; return 0; /* @@@正在进行[map_operate_equal()]的测试 Size of first: 0 Size of second: 3 @@@[map_operate_equal()]的测试结束 */ } /**************** map Parameters ============ first, last Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last. The function template type can be any type of input iterator. x Another map object with the same class template parameters (Key, T, Compare and Allocator). comp Comparison object to be used for the strict weak ordering. Compare is the third class template parameter (see class description). unnamed Allocator parameter Allocator object to be used instead of constructing a new one. For class instantiations using their version of the default allocator class template, this parameter is not relevant. ******************/ bool fncomp (char lhs, char rhs) {return lhs<rhs;} struct classcomp { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;} }; int map_map() { map<char,int> first; first['a']=10; first['b']=30; first['c']=50; first['d']=70; map<char,int> second (first.begin(),first.end()); map<char,int> third (second); map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare return 0; /* @@@正在进行[map_map()]的测试 @@@[map_map()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Iterators ////////////////////////////////////////////////////////////////////////// int map_begin(){ map<char,int> mymap; map<char,int>::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; // show content: for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_begin()]的测试 a => 200 b => 100 c => 300 @@@[map_begin()]的测试结束 */ } int map_end() { map<char,int> mymap; map<char,int>::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; // show content: for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_end()]的测试 a => 200 b => 100 c => 300 @@@[map_end()]的测试结束 */ } int map_rbegin () { map<char,int> mymap; map<char,int>::reverse_iterator rit; mymap['x'] = 100; mymap['y'] = 200; mymap['z'] = 300; // show content: for ( rit=mymap.rbegin() ; rit != mymap.rend(); rit++ ) cout << rit->first << " => " << rit->second << endl; return 0; /* @@@正在进行[map_rbegin()]的测试 z => 300 y => 200 x => 100 @@@[map_rbegin()]的测试结束 */ } int map_rend() { map<char,int> mymap; map<char,int>::reverse_iterator rit; mymap['x'] = 100; mymap['y'] = 200; mymap['z'] = 300; // show content: for ( rit=mymap.rbegin() ; rit != mymap.rend(); rit++ ) cout << rit->first << " => " << rit->second << endl; return 0; /* @@@正在进行[map_rend()]的测试 z => 300 y => 200 x => 100 @@@[map_rend()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Capacity:empty size max_size ////////////////////////////////////////////////////////////////////////// int map_empty () { map<char,int> mymap; mymap['a']=10; mymap['b']=20; mymap['c']=30; while (!mymap.empty()) { cout << mymap.begin()->first << " => "; cout << mymap.begin()->second << endl; mymap.erase(mymap.begin()); } return 0; /* @@@正在进行[map_empty()]的测试 a => 10 b => 20 c => 30 @@@[map_empty()]的测试结束 */ } int map_size () { map<char,int> mymap; mymap['a']=101; mymap['b']=202; mymap['c']=302; cout << "mymap.size() is " << (int) mymap.size() << endl; return 0; /* @@@正在进行[map_size()]的测试 mymap.size() is 3 @@@[map_size()]的测试结束 */ } /**** .max_size 取决于 <key Value> 所占字节比较大的一个。然后用4个字节的数字(unsigned_int_max=40亿左右) 除以2除以 所占字节就是这个值了。 ****/ int map_max_size () { int i; map<int,int> mymap; cout<<"mymap.max_size()=" << mymap.max_size() <<endl; if (mymap.max_size()>1000) { for (i=0; i<1000; i++) mymap[i]=0; cout << "The map contains 1000 elements.\n"; } else cout << "The map could not hold 1000 elements.\n"; cout<<"mymap2<char,char 11>.max_size()=" << map<char,char>().max_size() <<endl; cout<<"mymap2<char,short 12>.max_size()=" << map<char,short>().max_size() <<endl; cout<<"mymap2<short,char 21>.max_size()=" << map<short,char>().max_size() <<endl; cout<<"mymap2<short,short 22>.max_size()=" << map<short,short>().max_size() <<endl; cout<<"mymap2<int,int 44>.max_size()=" << map<int,int>().max_size() <<endl; cout<<"mymap2<unsigned int,unsigned int 44>.max_size()=" << map<unsigned int,unsigned int>().max_size() <<endl; cout<<"mymap2<long long,long long 88>.max_size()=" << map<long long,long long>().max_size() <<endl; cout<<"mymap2<long long,int 84>.max_size()=" << map<long long,int>().max_size() <<endl; cout<<"mymap2<long long,short 82>.max_size()=" << map<long long,short>().max_size() <<endl; cout<<"mymap2<long long,char 81>.max_size()=" << map<long long,char>().max_size() <<endl; cout<<"mymap2<char,long long 18>.max_size()=" << map<char,long long>().max_size() <<endl; cout<<"mymap2<short,long long 28>.max_size()=" << map<short,long long>().max_size() <<endl; cout<<"mymap2<int,long long 48>.max_size()=" << map<int,long long>().max_size() <<endl; return 0; /* @@@正在进行[map_max_size()]的测试 mymap.max_size()=536870911 The map contains 1000 elements. mymap2<char,char 11>.max_size()=2147483647 mymap2<char,short 12>.max_size()=1073741823 mymap2<short,char 21>.max_size()=1073741823 mymap2<short,short 22>.max_size()=1073741823 mymap2<int,int 44>.max_size()=536870911 mymap2<unsigned int,unsigned int 44>.max_size()=536870911 mymap2<long long,long long 88>.max_size()=268435455 mymap2<long long,int 84>.max_size() =268435455 mymap2<long long,short 82>.max_size()=268435455 mymap2<long long,char 81>.max_size() =268435455 mymap2<char,long long 18>.max_size() =268435455 mymap2<short,long long 28>.max_size()=268435455 mymap2<int,long long 48>.max_size() =268435455 @@@[map_max_size()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Element access:operator[] ////////////////////////////////////////////////////////////////////////// int map_operator_element_access () { map<char,string> mymap; mymap['a']="an element"; mymap['b']="another element"; mymap['c']=mymap['b']; mymap['b']="another element1"; cout << "mymap['a'] is " << mymap['a'] << endl; cout << "mymap['b'] is " << mymap['b'] << endl; cout << "mymap['c'] is " << mymap['c'] << endl; cout << "mymap['d'] is " << mymap['d'] << endl; cout << "mymap['e'] is " << mymap['e'] << endl; cout << "mymap now contains " << (int) mymap.size() << " elements." << endl; return 0; /* @@@正在进行[map_operator_element_access()]的测试 mymap['a'] is an element mymap['b'] is another element1 mymap['c'] is another element mymap['d'] is mymap['e'] is mymap now contains 5 elements. @@@[map_operator_element_access()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Modifiers::insert erase swap clear ////////////////////////////////////////////////////////////////////////// int map_insert () { map<char,int> mymap; map<char,int>::iterator it; pair<map<char,int>::iterator,bool> ret; // first insert function version (single parameter): mymap.insert ( pair<char,int>('a',100) ); mymap.insert ( pair<char,int>('z',200) ); ret=mymap.insert (pair<char,int>('z',500) ); if (ret.second==false) { cout << "element 'z' already existed"; cout << " with a value of " << ret.first->second << endl; } // second insert function version (with hint position): it=mymap.begin(); mymap.insert (it, pair<char,int>('b',300)); // max efficiency inserting mymap.insert (it, pair<char,int>('c',400)); // no max efficiency inserting // third insert function version (range insertion): map<char,int> anothermap; anothermap.insert(mymap.begin(),mymap.find('c')); // showing contents: cout << "mymap contains:\n"; for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; cout << "anothermap contains:\n"; for ( it=anothermap.begin() ; it != anothermap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_insert()]的测试 element 'z' already existed with a value of 200 mymap contains: a => 100 b => 300 c => 400 z => 200 anothermap contains: a => 100 b => 300 @@@[map_insert()]的测试结束 */ } int map_erase () { map<char,int> mymap; map<char,int>::iterator it; // insert some values: mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; mymap['e']=50; mymap['f']=60; it=mymap.find('b'); mymap.erase (it); // erasing by iterator mymap.erase ('c'); // erasing by key it=mymap.find ('e'); mymap.erase ( it, mymap.end() ); // erasing by range // show content: for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_erase()]的测试 a => 10 d => 40 @@@[map_erase()]的测试结束 */ } int map_swap () { map<char,int> foo; map<char,int> bar; map<char,int>::iterator it; foo['x']=100; foo['y']=200; bar['a']=11; bar['b']=22; bar['c']=33; foo.swap(bar); cout << "foo contains:\n"; for ( it=foo.begin() ; it != foo.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; cout << "bar contains:\n"; for ( it=bar.begin() ; it != bar.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_swap()]的测试 foo contains: a => 11 b => 22 c => 33 bar contains: x => 100 y => 200 @@@[map_swap()]的测试结束 */ } int map_clear(){ map<char,int> mymap; map<char,int>::iterator it; mymap['x']=100; mymap['y']=200; mymap['z']=300; cout << "mymap contains:\n"; for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; mymap.clear(); mymap['a']=1101; mymap['b']=2202; cout << "mymap contains:\n"; for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_clear()]的测试 mymap contains: x => 100 y => 200 z => 300 mymap contains: a => 1101 b => 2202 @@@[map_clear()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Observers:key_comp value_comp ////////////////////////////////////////////////////////////////////////// int map_keycomp () { map<char,int> mymap; map<char,int>::key_compare mycomp; map<char,int>::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['b']=200; mymap['c']=300; cout << "mymap contains:\n"; highest=mymap.rbegin()->first; // key value of last element it=mymap.begin(); do { cout << (*it).first << " => " << (*it).second << endl; } while ( mycomp((*it++).first, highest) ); cout << endl; return 0; /* @@@正在进行[map_keycomp()]的测试 mymap contains: a => 100 b => 200 c => 300 @@@[map_keycomp()]的测试结束 */ } int map_valuecomp() { map<char,int> mymap; map<char,int>::iterator it; pair<char,int> highest; mymap['x']=1001; mymap['y']=2002; mymap['z']=3003; cout << "mymap contains:\n"; highest=*mymap.rbegin(); // last element it=mymap.begin(); do { cout << (*it).first << " => " << (*it).second << endl; } while ( mymap.value_comp()(*it++, highest) ); return 0; /* @@@正在进行[map_valuecomp()]的测试 mymap contains: x => 1001 y => 2002 z => 3003 @@@[map_valuecomp()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Operations:find count lower_bound upper_bound equal_range ////////////////////////////////////////////////////////////////////////// int map_find () { map<char,int> mymap; map<char,int>::iterator it; mymap['a']=50; mymap['b']=100; mymap['c']=150; mymap['d']=200; it=mymap.find('b'); mymap.erase (it); mymap.erase (mymap.find('d')); //测试 无法查找的情况 it=mymap.find('c'); if(it!=mymap.end()) cout<<"It(find a exist key)"<<'c'<<endl; it=mymap.find('z'); if(it==mymap.end()) cout<<"It(find non exist key)"<<'z'<<" By map.find('z') != map.end() !"<<endl; if(mymap.count('z')<=0) cout<<"It(find non exist key)"<<'z'<<" By map.count('z') method!"<<endl; // print content: cout << "elements in mymap:" << endl; cout << "just show a and c " << endl; cout << "a => " << mymap.find('a')->second << endl; cout << "c => " << mymap.find('c')->second << endl; cout << " or iterator"<<endl; for(it=mymap.begin();it!=mymap.end();it++) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_find()]的测试 elements in mymap: a => 50 c => 150 @@@[map_find()]的测试结束 */ } int map_count () { map<char,int> mymap; char c; mymap ['a']=101; mymap ['a']=102; mymap ['c']=202; mymap ['f']=303; for (c='a'; c<'h'; c++) { cout << c << "[count = " << mymap.count(c) << "]"; if (mymap.count(c)>0) cout << " is an element of mymap.\n"; else cout << " is not an element of mymap.\n"; } return 0; /* @@@正在进行[map_count()]的测试 a[count = 1] is an element of mymap. b[count = 0] is not an element of mymap. c[count = 1] is an element of mymap. d[count = 0] is not an element of mymap. e[count = 0] is not an element of mymap. f[count = 1] is an element of mymap. g[count = 0] is not an element of mymap. @@@[map_count()]的测试结束 */ } int map_lowerbound () { map<char,int> mymap; map<char,int>::iterator it,itlow,itup; mymap['a']=20; mymap['b']=40; mymap['c']=60; mymap['d']=80; mymap['e']=100; itlow=mymap.lower_bound ('b'); // itlow points to b itup=mymap.upper_bound ('d'); // itup points to e (not d!) mymap.erase(itlow,itup); // erases [itlow,itup) // print content: for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_lowerbound()]的测试 a => 20 e => 100 @@@[map_lowerbound()]的测试结束 */ } int map_upperbound () { map<char,int> mymap; map<char,int>::iterator it,itlow,itup; mymap['a']=20; mymap['b']=40; mymap['c']=60; mymap['d']=80; mymap['e']=100; itlow=mymap.lower_bound ('b'); // itlow points to b itup=mymap.upper_bound ('d'); // itup points to e (not d) mymap.erase(itlow,itup); // erases [itlow,itup) // print content: for ( it=mymap.begin() ; it != mymap.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; /* @@@正在进行[map_upperbound()]的测试 a => 20 e => 100 @@@[map_upperbound()]的测试结束 */ } int map_equalRange () { map<char,int> mymap; pair<map<char,int>::iterator,map<char,int>::iterator> ret; mymap['a']=10; mymap['b']=20; mymap['c']=30; ret = mymap.equal_range('b'); cout << "lower bound points to: "; cout << ret.first->first << " => " << ret.first->second << endl; cout << "upper bound points to: "; cout << ret.second->first << " => " << ret.second->second << endl; return 0; /* @@@正在进行[map_equalRange()]的测试 lower bound points to: b => 20 upper bound points to: c => 30 @@@[map_equalRange()]的测试结束 */ } ////////////////////////////////////////////////////////////////////////// //Allocator:get_allocator ////////////////////////////////////////////////////////////////////////// int map_getallocator () { int psize; map<char,int> mymap; pair<const char,int>* p; // allocate an array of 5 elements using mymap's allocator: p=mymap.get_allocator().allocate(5); // assign some values to array psize = (int) sizeof(map<char,int>::value_type)*5; cout << "The allocated array has a size of " << psize << " bytes.\n"; mymap.get_allocator().deallocate(p,5); return 0; /* @@@正在进行[map_getallocator()]的测试 The allocated array has a size of 40 bytes. @@@[map_getallocator()]的测试结束 请按任意键继续. . . */ } /************** 测试 内容 主体 ****************/ #define runTheFunc(str) runTheFunc_str(str,###str) typedef int (*callthefunc)() ; void runTheFunc_str(callthefunc func,string str){ cout<<"@@@正在进行["<<str<<"()]的测试"<<endl; (*func)(); cout<<"@@@["<<str<<"()]的测试结束"<<endl<<endl; } int main(){ runTheFunc(map_operate_equal); runTheFunc(map_map); runTheFunc(map_begin); runTheFunc(map_end); runTheFunc(map_rbegin); runTheFunc(map_rend); runTheFunc(map_empty); runTheFunc(map_size); runTheFunc(map_max_size); runTheFunc(map_operator_element_access); runTheFunc(map_insert); runTheFunc(map_erase); runTheFunc(map_swap); runTheFunc(map_clear); runTheFunc(map_keycomp); runTheFunc(map_valuecomp); runTheFunc(map_find); runTheFunc(map_count); runTheFunc(map_lowerbound); runTheFunc(map_upperbound); runTheFunc(map_equalRange); runTheFunc(map_getallocator); system("pause"); return 0; }
学习资料:http://www.cplusplus.com/reference/stl/map/
这个 cplusplus.com还有许多其他的STL的东西,真是不错,每一个 都有示例!标准的玩意,都应该按照这样来写文档!!
------------------------------------------------------------------------------------------------
一定要专业!本博客定位于 ,C语言,C++语言,Java语言,Android开发和少量的Web开发,之前是做Web开发的,其实就是ASP维护,发现EasyASP这个好框架,对前端后端数据库 都很感觉亲切啊。. linux,总之后台开发多一点。以后也愿意学习 cocos2d-x 游戏客户端的开发。