2010-11-20 12:23:40| 分类: C++ | 标签: |字号大中小 订阅
1.map基本用法:
样例代码:
#include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#include#include#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#include#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#includeusing namespace std;int main(){ map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
using namespace std;
int main()
{
map mymap; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
for(it=mymap.begin();it!=mymap.end();++it)
cout<<(*it).first<<"=>"<<(*it).second< return 0;}2.count用法: size_type count(key_type x) const 注:如果mymap的key中有x,则函数返回1,否则返回0.3.equal_range的用法:pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
}
2.count用法:
size_type count(key_type x) const
注:如果mymap的key中有x,则函数返回1,否则返回0.
3.equal_range的用法:
pair equal_range ( const key_type& x );pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
equal_range ( const key_type& x );
pair equal_range ( const key_type& x ) const;可以理解为该函数返回指向key为x的iterator.样例如下: map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
equal_range ( const key_type& x ) const;
可以理解为该函数返回指向key为x的iterator.样例如下:
map mymap; pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
pair::iterator,map::iterator> pRet; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; pRet = mymap.equal_range('a'); cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
pRet = mymap.equal_range('a');
cout<<"lower bound points to : "< cout<first<<"=>"<second< cout<first<<"=>"<second<4.erase与find的用法: erase: void erase ( iterator position ); size_type erase ( const key_type& x ); void erase ( iterator first, iterator last ); find: iterator find ( const key_type& x ); const_iterator find ( const key_type& x ) const; 样例代码: mymap.erase(mymap.find('b')); //删除map中key为'b'的记录5.insert用法: pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
erase:
void erase ( iterator position );
size_type erase ( const key_type& x );
void erase ( iterator first, iterator last );
find:
iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;
mymap.erase(mymap.find('b')); //删除map中key为'b'的记录
5.insert用法:
pair insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); void insert ( iterator first, iterator last ); 注: x为map的一条记录,包括key和value,而不单指value. 样例代码: #include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
iterator insert ( iterator position, const value_type& x );
void insert ( iterator first, iterator last );
注: x为map的一条记录,包括key和value,而不单指value.
#include#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#includeusing namespace std;int main(){ map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map mymap; pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
pair::iterator,bool> pRet; map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map::iterator it; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
mymap.insert(pair('d',400)); mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
mymap.insert(pair('e',500)); pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
pRet = mymap.insert(pair('f',600)); if(pRet.second) cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
if(pRet.second)
cout<<"Perfect!"< it = mymap.begin(); mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
mymap.insert(it,pair('g',700)); mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
mymap.insert(it,pair('h',800)); map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map anothermap; anothermap.insert(mymap.begin(),mymap.find('d')); cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
anothermap.insert(mymap.begin(),mymap.find('d'));
cout<<"mymap:"< for(it=mymap.begin();it!=mymap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
cout<<(*it).first<<"=>"<<(*it).second< cout<<"anothermap:"< for(it=anothermap.begin();it!=anothermap.end();++it) cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
cout<<(*it).first<<"=>"<<(*it).second< return 0;}6.key_comp和value_comp用法: key_compare key_comp ( ) const; value_compare value_comp ( ) const; 样例代码:(key_comp) #include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
6.key_comp和value_comp用法:
key_compare key_comp ( ) const;
value_compare value_comp ( ) const;
样例代码:(key_comp)
#include #include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include using namespace std;int main(){ map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map mymap; map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map::key_compare mycomp; map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map::iterator it; char highest; mycomp = mymap.key_comp(); mymap['a']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
char highest;
mycomp = mymap.key_comp();
mymap['a']=100;
mymap['c']=200;
mymap['b']=300;
cout<<"mymap:"< highest = mymap.rbegin()->first; it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
it = mymap.begin();
do
cout<<(*it).first<<"=>"<<(*it).second< } while (mycomp((*it++).first,highest)); cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
cout< return 0;} 注:该函数可用于map中对key排序(可参考上面的代码) 样例代码:(value_comp) #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
注:该函数可用于map中对key排序(可参考上面的代码)
样例代码:(value_comp)
#include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#define Elem pairusing namespace std;int main(){ map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map mymap; map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map::iterator it; Elem highest; mymap['d']=100; mymap['c']=200; mymap['b']=300; cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
Elem highest;
mymap['d']=100;
cout<<"mymap:"< highest = *mymap.rbegin(); it = mymap.begin(); do { cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
cout<<(*it).first<<"=>"<<(*it).second< } while (mymap.value_comp()(*it++,highest)); cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
cout< return 0;} 注:该函数也是对map的key排序。 7.lower_bound 和upper_bound的用法 iterator lower_bound ( const key_type& x ); const_iterator lower_bound ( const key_type& x ) const; iterator upper_bound ( const key_type& x ); const_iterator upper_bound ( const key_type& x ) const; 注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator. 样例代码: #include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
注:该函数也是对map的key排序。
7.lower_bound 和upper_bound的用法
iterator lower_bound ( const key_type& x );
const_iterator lower_bound ( const key_type& x ) const;
iterator upper_bound ( const key_type& x );
const_iterator upper_bound ( const key_type& x ) const;
注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator.
样例代码:
#include #include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#include #define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
#define Elem pairusing namespace std;int main(){ map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map mymap; map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
map::iterator it,itlow,itup; Elem highest; mymap['d']=100; mymap['b']=200; mymap['c']=300; mymap['a']=400; mymap['e']=500; itlow = mymap.lower_bound('c'); itup = mymap.upper_bound('e'); mymap.erase(itlow,itup); for (it=mymap.begin();it!=mymap.end();++it) { cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
mymap['b']=200;
mymap['c']=300;
mymap['a']=400;
mymap['e']=500;
itlow = mymap.lower_bound('c');
itup = mymap.upper_bound('e');
mymap.erase(itlow,itup);
for (it=mymap.begin();it!=mymap.end();++it)
cout<<(*it).first<<"=>"<<(*it).second< } return 0;} 8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。
return 0;
8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。