STL之map的基本使用

来源:互联网 发布:qq群排名软件 编辑:程序博客网 时间:2024/05/27 14:12
#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<cstring>#include<map>using namespace std;/****************************** map是双值的STL,第一个数据为数据关键字,第二个为排序的关键字* 数据关键字是唯一的*****************************//*****************************  函数语法  :iterator begin()                const_iterator begin() const*  函数说明  :返回值为const_iterator时,set不可修改;*  函数功能  :返回第一个元素的定位器的地址*  函数返回值:返回一个指向第一个元素的双向定位器地址******************************//*****************************  函数语法  :iterator end()                const_iterator end() const*  函数说明  :map为空时,结果没定义*  函数功能  :测试iterator是否到达尾部*  函数返回值:返回一个指向最后一个元素的双向定位器地址******************************//*****************************  函数语法  :void clear()*  函数功能  :删除map中所有的元素******************************//*****************************  函数语法  :size_type count(const Key& _Key)const*  函数说明  :_Key是要进行匹配的关键字的数值;*  函数功能  :返回_Key的个数*  函数返回值:返回_Key的个数******************************//*****************************  函数语法  :bool empty()const*  函数功能  :判断map是否为空*  函数返回值:为空返回true,反之返回false******************************//*****************************  函数语法  : iterator erase(iterator _Where)               iterator erase(iterator _First,iterator _Last)               iterator erase(const Key_type & _Key)*  函数说明  : _Key是要删除的关键字的数值;               _Where 删除元素的位置               _First-_Last-1 删除元素的范围*  函数功能  :删除一个挥着一定范围的元素*  函数返回值:前两个函数返回指向第一个没被删除的元素的iterator                第三个函数返回被删除的元素的个数******************************//*****************************  函数语法  : pair<iterator,bool>insert(const value_type& _Val)               iterator erase(iterator _Where,const value_type& _Val)               template<class InputIterator>void insert(InputIterator _First,InputIterator _Last)*  函数说明  : _Val是要插入的关键字的数值;               _Where 第一个插入元素的位置               _First-_Last-1 插入元素的范围*  函数功能  :插入一个或者一定范围的元素*  函数返回值:第一个函数返回一对值,插入成功bool=true,如果元素已经存在 bool=false,iterator返回插入的位置或者已经存在的元素的位置                第三个函数返回插入位置的定位器******************************//*****************************  函数语法  :iterator find(const Key& _Key)                const_iterator find(const Key& _Key) const*  函数功能  :返回第一个指向关键字的定位器*  函数返回值:返回第一个指向关键字的定位器******************************//*****************************  函数语法  iterator lower_bound(const Key&_Key)const              iterator upper_bound(...)  同上,只是大于*  函数功能  :返回一个指向第一个关键字的值是大于等于_Key的iterator*  函数返回值:返回一个指向第一个关键字的值是大于等于_Key的iterator~返回第一个大于等于_Key的位置******************************//*****************************  函数语法  :size_type max_size()const*  函数功能  :返回容器的最大可能长度***//*****************************  函数语法  :bool operator!=(                  const map<Key,Type,Traits,Allocator>&_Left,                  const map<Key,Type,Traits,Allocator>&_Right                  )                bool operator<(                  const map<Key,Type,Traits,Allocator>&_Left,                  const map<Key,Type,Traits,Allocator>&_Right                  )                同理还有==、<=、>=、>*  函数功能  :(1)判断两个map:_Left和_Right是否不等               (2)判断map:_Left是否小于_Right*  函数返回值:(1)两个map不同时返回true,反之返回false               (2)左边小于时返回true,反之返回false***//*****************************  函数语法  Type & operator[](const Key&_Key)*  函数功能  :(1)将一个给定的值插入到map*  函数返回值:(1)返回Key插入的地址***//*****************************  函数语法  :reverse_iterator rbegin()                const_reverse_iterator begin() const*  函数说明  :返回值为const_iterator时,map不可修改;*  函数功能  :返回反向map的第一个元素的定位器的地址*  函数返回值:返回一个反向map的指向第一个元素的双向定位器地址******************************//*****************************  函数语法  :reverse_iterator rend()                const_reverse_iterator end() const*  函数说明  :map为空时,结果没定义*  函数功能  :返回一个指向反向map的最后元素后面的定位器*  函数返回值:返回一个反向map的指向最后一个元素的双向定位器地址******************************//*****************************  函数语法  :size_type size()const*  函数功能  :计算当前容器的元素个数*  函数返回值:返回当前容器的长度******************************//*****************************  函数语法  :void swap(set &_Right)*  函数功能  :交换两个容器的元素******************************//*****************************  函数语法  :pair<const_iterator,const_iterator>equal_range(               const Key&_Key)const               pair<iterator,iterator>equal_range(               const Key&_Key)const*  函数说明  :_Key是用于排序的关键字*  函数功能  :返回一对定位器,他们分别指向第一个大于或者等于给定的关键字的元素和第一个比给你定的关键字打的元素*  函数返回值:返回一对定位器,分别是第一个>=_Key的元素和>_key的元素                取得第一个和第二个元素用pr.frist和pr.second******************************/map<int,int>m_map;map<int,int>m_map1;void print(map<int,int> m){    map<int,int>::iterator it;    for(it=m.begin();it!=m.end();it++)    {        cout<<it->second<<" ";    }    cout<<endl;}int main(){    map<int,int>::iterator it;    m_map.insert(pair<int,int>(1,1));    m_map.insert(pair<int,int>(2,2));    it=m_map.begin();    cout<<it->second<<endl;    //m_map.clear();    print(m_map);    cout<<m_map.count(1)<<endl;    if(m_map.empty())    {        cout<<"empty"<<endl;    }    else    {        cout<<"Has Item"<<endl;    }    it=m_map.end();    it--;    cout<<it->second<<endl;    m_map.erase(1);    print(m_map);    it=m_map.find(1);    if(it!=m_map.end())    {        cout<<"the number 1 is "<<it->second<<endl;    }    else    {        cout<<"no such number"<<endl;    }    m_map.insert(pair<int,int>(3,3));    m_map.insert(pair<int,int>(4,4));    it=m_map.lower_bound(2);    if(it!=m_map.end())    {        cout<<it->first<<" "<<it->second<<endl;    }    it=m_map.upper_bound(2);    if(it!=m_map.end())    {        cout<<it->first<<" "<<it->second<<endl;    }    cout<<"map max_size is "<<m_map.max_size()<<endl;    for(int i=0;i<4;i++)    {        m_map1.insert(pair<int,int>(i*3,i*4));    }    print(m_map1);    if(m_map!=m_map1)cout<<"!="<<endl;    if(m_map==m_map1)cout<<"=="<<endl;    if(m_map<=m_map1)cout<<"<="<<endl;    if(m_map>=m_map1)cout<<">="<<endl;    if(m_map<m_map1)cout<<"<"<<endl;    if(m_map>m_map1)cout<<">"<<endl;    m_map1[4]=6;    print(m_map1);    map<int,int>::reverse_iterator rit;    for(rit=m_map1.rbegin();rit!=m_map1.rend();rit++)    {        cout<<rit->first<<"+"<<rit->second<<endl;    }    cout<<endl;    cout<<"map size is "<<m_map.size()<<endl;    cout<<"map1 size is "<<m_map1.size()<<endl;    m_map.swap(m_map1);    print(m_map);    print(m_map1);    pair<map<int,int>::const_iterator,map<int,int>::const_iterator>pr;    pr=m_map1.equal_range(4);    if(pr.first!=m_map1.end())    {        cout<<"this first "<<pr.first->first<<" ";    }    if(pr.second!=m_map1.end())    {        cout<<"the second "<<pr.second->second<<" ";    }    cout<<endl;    return 0;}

0 0
原创粉丝点击