对于容器map的使用---map::value_type

转自微博:

http://blog.sina.com.cn/s/blog_74112f030101cm1j.html

http://msdn.microsoft.com/en-us/library/y6ec19ye(v=vs.80).aspx


typedef pair value_type;
这个value_type实际上就是一个pair用它可以给map插入一些值,具体例子如下
#include
#include
using namespace std;
int main()
{
    using namespace std;
    typedef paircInt2Int;
    mapm1;
    map::key_type key1;//定义了一个map的键
    map::mapped_type mapped1;//定义了一个map的值
    map::value_type value1;//定义了一个map的变量组合
    map::iterator pIter;
    //value_type can be used to pass the correct type
    //explicitly to avoid implicit type conversion
    m1.insert(map::value_type(1,10));//此处可以看出value_type到底是什么东西
    //compair other ways to insert objects into a map;
    m1.insert(cInt2Int(2,20));
    m1[3]=30;
    //Initializing key1 and mapped1
    key1=(m1.begin()->first);
    mapped1=(m1.begin()->second);
    cout<<"the key of first element in the map is "
        <<key1<<"."<<endl;
    cout<<"the data value of first element in the map is "
        <<mapped1<<"."<<endl;
    //The following line would cause an error because
    //the value_type is not assignable
    //value1=cInt2Int(4,40);//type_value不能被赋值!
    cout<<"The keys of the mapped elements are:";
    for(pIter=m1.begin();pIter!=m1.end();pIter++)
        cout<<" "<<pIter->first;
    cout<<"."<<endl;
    cout<<"The values of the mapped elements are:";
    for(pIter=m1.begin();pIter!=m1.end();pIter++)
        cout<<" "<<pIter->second;
    cout<<"."<<endl;
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值