码迷,mamicode.com
首页 > 其他好文 > 详细

map的用法

时间:2014-07-25 13:49:01      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   数据   io   for   re   

/*
ACM中map的基本的用法,主要是用数组的形式实现。
1.构造map
2.数据插入
3.map的大小
4.数据的查找
5.数据的删除
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int main()
{
    map<int,string>m;//构造map
    m.clear();//数据清空
    m[1] = "abc";//数据插入
    m[2] = "def";
    m[3] = "ghi";
    cout<<m.size()<<endl;//输出map的大小
    if(m[1].size()) cout<<m[1]<<endl;//先判断该数据是否存在,存在就输出
    cout<<m[4].size()<<endl;//不出在输出0
    int  n = m.erase(1);//删除元素,成功的话返回1
    return 0;
}
//如果map的映射值类型为字符类型,那么输出这个映射值不能用printf();输出,会报错,cout就行。
/*
lower_bound(),upper_bound()函数介绍
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include <algorithm>
#include<map>
using namespace std;
int a[10] = {2,3,4,5,6,7,8,9};
int main()
{
    int n = 8;//数组长度
    int pos = lower_bound(a,a+n,5)-a;//返回大于等于5的那个数在数组中的地址
    cout<<a[pos]<<endl;
    pos = upper_bound(a,a+n,5) -a;//返回大于5的那个数在数组中的地址
    cout<<a[pos]<<endl;
    pos = lower_bound(a,a+n,100)-a;//返回的是一个越界的数值,返回8
    cout<<pos<<endl;
    return 0;
}


#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef struct tt//重载排序
{
int id;
string name;
bool operator < (const tt &a) const{
return a.id>id;
}
}; //学生信息
int main()
{
int nSize;
//用学生信息映射分数
map<tt, int>m;
map<tt, int>::iterator iter;
tt s;
s.id = 2;
s.name = "Jack";
m[s] = 100;
s.id = 1;
s.name = "Rose";
m[s] = 100;
for (iter=m.begin(); iter!=m.end(); iter++)
cout<<iter->first.id<<" "<<iter->first.name<<" "<<iter->second<<endl;
}

map的用法,布布扣,bubuko.com

map的用法

标签:style   blog   color   os   数据   io   for   re   

原文地址:http://www.cnblogs.com/llei1573/p/3867469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!