map--初步学习STL

// map::get_allocator
#include <iostream>
#include <map>
#include<string>

using namespace std;

int main ()
{

    int psize;
    map<char,int> mymap;
    pair<const char,int>* p;
    // allocate an array of 5 elements using mymap's allocator:
    p=mymap.get_allocator().allocate(5);
    // assign some values to array
    psize = (int) sizeof(map<char,int>::value_type)*5;
    cout << "The allocated array has a size of " << psize << " bytes.\n";
    mymap.get_allocator().deallocate(p,5);

    map<string,int> map1;
    pair<map<string,int>::iterator,bool> insert_pair;
    map1.insert(pair<string,int>("s1",1));
    map1.insert(pair<string,int>("s2",2));
    map1.insert(pair<string,int>("s3",3));
    map<string,int>::iterator iter;
    iter=map1.begin();
    while(iter!=map1.end())
    {
        cout<<"  "<<iter->first<<"  "<<iter->second<<endl;
        iter++;
    }
    map1.insert(map<string,int>::value_type("s4",4));
    map1.insert(map<string,int>::value_type("s5",5));
    map1["s6"]=6;
    cout<<"after insert: "<<endl;
    iter=map1.begin();
    while(iter!=map1.end())
    {
        cout<<"  "<<iter->first<<"  "<<iter->second<<endl;
        iter++;
    }
    insert_pair=map1.insert(pair<string,int>("s6",6));
    if (insert_pair.second==true)
    {
        //insert_pair->second 不能通过
        cout<<"insert successfully!"<<endl;
    }
    else
    {
        cout<<"insert failure"<<endl;
    }
    iter=map1.find("s1");
    if (iter!=map1.end())
    {
        cout<<"find the value is "<<iter->second<<endl;
    }
    else
    {
        cout<<"do not find"<<endl;
    }
    return 0;
}
/***

//member functions
map<Key,T>::iterator it
(*it)->first  //the key value(of type key)
(*it)->second //the mapped value(of type T)
(*it) //the element value (of type pair <const Key,T>)
it->first  //the same as (*it)->first
it->second  //the same as (*it)->second
(constructor)
(destructor)
operator =
//iterators
begin()
end()
rbegin()
rend()
//capacity
empity()
size()
max_size()
//elements access
operator []
//modifiers
insert()
erase()
swap()
clear()
//observers
key_comp()
value_comp()
//operations
find()
count()
lower_bound()
upper_bound()
equal_range()
//allcoator
get_allocator()

***/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值