衡水阿里巴巴网站建设,flash网站模板下载,龙岩做网站设计公司,简繁英3合1企业网站生成管理系统V1.6map的特性是#xff0c;所有元素都会根据元素的键值自动被排序。map的所有元素都是pair#xff0c;同时拥有实值(value)和键值(key)。pair的第一元素被视为键值#xff0c;第二元素被视为实值。map不允许两个元素拥有相同的键值。下面是stl_pair.h中pair的定义
tem… map的特性是所有元素都会根据元素的键值自动被排序。map的所有元素都是pair同时拥有实值(value)和键值(key)。pair的第一元素被视为键值第二元素被视为实值。map不允许两个元素拥有相同的键值。下面是stl_pair.h中pair的定义
templateclass T1, class T2
struct pair {typedef T1 first_type;typedef T2 second_type;T1 first;T2 second;pair() : first(T1()), second(T2()) {}pair(const T1a, const T2b) : first(a), second(b) {}
}; 我们可以通过map的迭代器改变map的元素内容吗如果想要修正元素的键值答案是不行的因为map元素的键值关系到map元素的排列规则。任意改变map元素键值将会严重破坏map组织。如果想要修改元素的实值答案是可以的因为map元素的实值并不影响map元素的排列规则。因此map iterators既不是一种constant iterators,也不是一种mutble iterators. map拥有和list相同的某些性质当客户端对它进行元素新增操作(insert)或删除操作(erase)时操作之前的所有迭代器在操作完成之后都依然有效。当然被删除的那个元素的迭代器必然是个例外。 由于RB-tree是一种平衡二叉搜索树自动排序的效果很不错所以标准的STL map即以RB-tree为底层机制。又由于map所开放的各种操作接口RB-tree也都提供了所以几乎所有的map操作行为都只转调用RB-tree的操作行为而已。 下图所示map的架构 pair的第一个元素视为键值(key)第二个键值视为实值(value)。源码摘录如下
// 注意以下Key为键值key型别T为实值(value)型别
template class Key, class T, class Compare lessKey, class Allocalloc
class map {
public:typedef Key key_type; // 键值型别typedef T data_type;typedef T mapped_type;typedef pairclass Key, T value_type;typedef Compare key_compare;class value_compre: public binary_functionvalue_type, value_type, bool {friend class mapKey, T, Compare, Alloc;protected:Compare comp;value_compare(Compare c) : comp(c) {}public:bool operator() (const value_type x, const value_typey) const {return comp(x.first, y.first);}};private:typedef rb_treekey_type, value_type, select1stvalue_type, key_compare, Alloc rep_type;rep_type t;public:typedef typename rep_type::pointer pointer;typedef typename rep_type::const_pointer const_pointer;typedef typename rep_type::reference reference;typedef typename rep_type::const_reference const_reference;typedef typename rep_type::iterator iterator;typedef typename rep_type::const_iterator const_iterator;typedef typename rep_type::reverse_iterator reverse_iterator;typedef typename rep_type::const_reverse_iterator const_reverse_iterator;typedef typename rep_type::size_type size_type;typedef typename rep_type::difference_type difference_type;
// 注意 map底层使用的插入函数为insert_uniqueue;multimap底层会使用insert_equal进行插入map() t(Compare()) {}explicit map(const Compare comp) : t(comp) {}template class InputIteratormap(InputIterator first, InputIterator last) : t(Compare()) {t.insert_unique(first, last);}template class InputIteratormap(InputIterator first, InputIterator last, const Compare comp) : t(comp) {t.insert_unique(first, last);}map(const mapKey, T, Compare, Allocx) :t(x.t) {}mapKey, T, Compare, Alloc operator(cosnt mapKey, T, Compare, Allocx) {t x.t;return *this;}key_compare key_comp() const { return t.key_comp(); }value_compare value_comp() cosnt { return t.value_comp(); }iterator begin() { return t.begin(); }const_iterator begin() const { return t.begin(); }iterator end() { return t.end(); }iterator end() const { return t.end(); }reverse_iterator rbegin() { return t.rbegin(); }const_reverse_iterator rbegin() { return t.rbegin(); }bool empty() { return t.empty(); }size_type size() { return t.size(); }size_type max_size() { return t.max_size(); }T operator[](const key_typek) {return (*(insert(value_type(k, T())).first)).second;}void swap(mapKey, T, Compare, Allocx) { t.swap(x.t); }pairiterator, bool insert(const value_typex) {return t.insert_unique(x);}iterator insert(iterator position, cosnt value_typex) {return t.insert_unique(position, x);}templateclass InputIterator void insert(InputIterator first, InputIterator last) {return t.insert_unique(first, last);}void erase(iterator position) { t.erase(position); }size_type erase(const key_typex) { return t.erase(x); }void erase(iterator first, iterator last) { t.erase(first, last); }void clear() { t.clear(); }iterator find(cosnt key_typex) { return t.find(x); }const_iterator find(const key_typex) const { return t.find(x); }size_type count(const key_typex) const { t.count(x); }iterator lower_bound(const key_typex) { return t.lower_bound(x); }const_iterator lower_bound(const key_typex) const { return t.lower_bound(x); }iterator upper_bound(const key_typex) { return t.upper_bound(x); }const_iterator upper_bound(const key_typex) const { return t.upper_bound(x); }pairiterator, iterator equal_range(const key_typex) {return t.equal_range(x);}pairconst_iterator, const_iterator equal_range(const key_typex) const {return t.equal_range(x);}friend bool operator STL_NULL_TMPL_ARGS (const map, const map);friend bool operator STL_NULL_TMPL_ARGS (const map, const map);};
template class Key, class T, class Compare, class Alloc
inline bool operator (const mapKey, T, Compare, Allocx, const mapKey, T, Compare, Allocy) {return x.t y.t;
}template class Key, class T, class Compare, class Alloc
inline bool operator (const mapKey, T, Compare, Allocx, const mapKey, T, Compare, Allocy) {return x.t y.t;
} 文章转载自: http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.qmzwl.cn.gov.cn.qmzwl.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.khclr.cn.gov.cn.khclr.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.kongpie.com.gov.cn.kongpie.com http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.srhqm.cn.gov.cn.srhqm.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn