map<,>Map
map<long long, long long>Map;
Map.insert(pair<long long ,long long>(i1,i2));
1. long长整型
1、占用内存字节数不同:
long 类型所占字节数为:4,是一个32位的存储单元。long long类型所占字节数为:8是一个64位的存储单元。对于大型计算,常常会遇到很大的整数,并超出int所表示的范围,这时要使用long类型。long long类型则涉及更大的整数。
2、数值范围不同:
long 类型表示范围为:-2147483648~2147483647。
long long类型表示范围为:9223372036854775808~+9223372036854775807
3、浮点变量类型:
浮点变量也称实数变量,用于需要精确到小数的函数运算中,有float和double两种类型说明符。
float类型。float类型是一个位数为32位的单精度浮点数。它具有运行速度较快,占用空间较少的特点。
double类型。double类型是一个位数为64的双精度浮点数。双精度数在某些具有优化和高速运算能力的现代处理机上运算比单精度数快。双精度类型double比单精度类型float具有更高的精度和更大表示范围,常常使用。
map<,>Map
一、
#include
#include
#include
Using namespace std;
Int main()
{
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
Cout<<”Find, the value is ”<<iter->second<<endl;
}
Else
{
Cout<<”Do not Find”<<endl;
}
}
二、map表插入键值对时有两种方法:
map<int,int> m;
m.insert(make_pair(3,4));
m.insert(pair<int,int>(1,3));
map<int,int>::iterator it = m.begin();
while(it != m.end())
{
cout<<"it->first : "<<it->first<<" it->second : "<<it->second<<endl;
it++;
}
注意:pair是类模板,所以需要模板参数列表,而make_pair是函数,则不需要。
三、map的遍历
用迭代器遍历:则根据key的有序遍历
用key值遍历:是在已知key的基础上
map<char,int> m;
int value = 1;
for(char i = 'a';i <= 'z';i++)
{
m.insert(pair<char,int>(i,value++));
}
//利用key值 直接找到value
for(char i = 'a';i <= 'z';i++)
{
cout<<i<<" : "<<m[i]<<endl;
}