map && multimap

作者在 2008-08-17 14:34:04 发布以下内容
//the usage of multimap is as same as the map
#include<iostream>
#include<map>
#include<string>
using namespace std;

typedef map<int,string> MAP_INT_STR;
typedef pair<int,string> PAIR_INT_STR;
template<typename ITERATOR>
void print_map_item(ITERATOR it)
{
   
cout<<(*it).first<<","<<(*it).second<<endl;
}

int main(void)
{
   
MAP_INT_STR map1;
    PAIR_INT_STR pairs[6] =
    {
        
PAIR_INT_STR(1,string("one")),
        //PAIR_INT_STR(1,string("one")),          //just a test
        
PAIR_INT_STR(2,string("two")),
        PAIR_INT_STR(3,string("three")),
        PAIR_INT_STR(4,string("four")),
        PAIR_INT_STR(5,string("five"))
    };
    MAP_INT_STR map2(pairs,pairs+6);
    MAP_INT_STR map3(map2);
    if (map1.empty()) cout<<"map1 is empty"<<endl;
    else cout<<"map1 is not empty"<<endl;
    cout<<"map2(using begin,end) is "<<endl;
    MAP_INT_STR::iterator Iter;
    for (Iter = map2.begin();Iter != map2.end();Iter++) print_map_item(Iter);
    MAP_INT_STR::reverse_iterator RevIter;
    cout<<"map2 using(rbegin,rend) is "<<endl;
    for (RevIter = map2.rbegin();RevIter != map2.rend();RevIter++) print_map_item(RevIter);
    pair<MAP_INT_STR::iterator,bool> result;
    result = map1.insert(MAP_INT_STR::value_type(6,string("six")));
    if (result.second == true) cout<<"*(result.first) is ",print_map_item(result.first);
    else cout<<"pair(6,\"six\")was not inserted in map1"<<endl;
    map1.insert(pairs,pairs+6);
    map1.insert(map1.begin(),PAIR_INT_STR(0,string("zero")));
    cout<<"does map1 contain any pair with key = 6? "<<endl;
    Iter = map1.find(6);
    if (Iter != map2.end()) cout<<"map1 contains pair : ",print_map_item(Iter);
    else cout<<"map1 does not contain any element with key = 6"<<endl;
    map1[8] = "eight";  //assign value by operator[]
   
cout<<"Last key/data pair in map1 is ";
    print_map_item(map1.rbegin());
    cout<<"maxSize/size is : "<<map1.max_size()<<"/"<<map1.size()<<endl;
    map1.swap(map2);
    cout<<"Last key/data pair in map1 is ";
    print_map_item(map1.rbegin());
    map3.clear();
    cout<<"After calling map3.clear(),number of elements in map3 is "<<map3.size()<<endl;
    MAP_INT_STR::allocator_type Alloc = map3.get_allocator();
    MAP_INT_STR::key_compare kc = map1.key_comp();
    cout<<"use function object kc to find less of (10,4)..."<<endl;
    if (kc(10,4) == true) cout<<"kc(10,4) == true,which means 10<4"<<endl;
    else cout<<"kc(10,4) == false,which means 10>4"<<endl;
    //value_comp,比较实值
   
MAP_INT_STR::value_compare vc = map1.value_comp();
    cout<<"use function object vc to compare int-string pairs..."<<endl;
    cout<<"pairs[0] = ("<<pairs[0].first<<","<<pairs[0].second<<")"<<endl;
    cout<<"pairs[1] = ("<<pairs[1].first<<","<<pairs[1].second<<")"<<endl;
    if (vc(pairs[0],pairs[1]) == true) cout<<"pairs[0] < pairs[1]"<<endl;
    else cout<<"pairs[0] > pairs[1]"<<endl;
    //use count to calculate the total  elements
   
cout<<"does map2 contain an element with key 8?"<<endl;
    if (map2.count(8) == 1) cout<<"map2 contains element with key 8"<<endl;
    else cout<<"map2 does not contains element with key 8"<<endl;
    return 0;
}
STL | 阅读 4390 次
文章评论,共0条
游客请输入验证码
浏览56293次
最新评论