map的用法
/*
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;//構造mapm.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;//不出在輸出0int n = m.erase(1);//刪除元素,成功的話返回1return 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;//返回的是一個越界的數值,返回8cout<<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;
}
轉載于:https://www.cnblogs.com/llei1573/p/3867469.html
總結
- 上一篇: HDU 1950 Bridging si
- 下一篇: Atitit. 提升软件开发效率and