一、Map和Multimap
/*代码大致思路:
函数部分
print 输出一个map
printM 输出一个multimap
printG
printG_M
printL_M
*/
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, double> CustomPair;
void print(map<int, double> &m)
{
map<int, double>::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
CustomPair p1 = (CustomPair)(*it);
cout << p1.first << ",";
cout << std::fixed << cout.precision(2) << "," << p1.second << ';' << endl;
}
}
void printM(multimap<int, double> &m)
{
multimap<int, double>::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
CustomPair p1 = (CustomPair)(*it);
cout << p1.first << ",";
cout << std::fixed << cout.precision(2) << "," << p1.second << ';' << endl;
}
}
void printG(map<int, double, greater<int> > &m)
{
map<int, double, greater<int> >::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
CustomPair p1 = (CustomPair)(*it);
cout << p1.first << ",";
cout << std::fixed << cout.precision(2) << "," << p1.second << ';' << endl;
}
}
void printG_M(multimap<int, double, greater<int> > &m)
{
multimap<int, double, greater<int> >::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
CustomPair p1 = (CustomPair)(*it);
cout << p1.first << ",";
cout << std::fixed << cout.precision(2) << "," << p1.second << ';' << endl;
}
}
void printL_M(map<int, double, less<int> > &m)
{
map<int, double, greater<int> >::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
CustomPair p1 = (CustomPair)(*it);
cout << p1.first << ",";
cout << std::fixed << cout.precision(2) << "," << p1.second << ';' << endl;
}
}
int main()
{
map<int, double>::iterator itm;
map<int, double, greater<int> >::iterator itmG;
map<int, double, less<int> >::iterator itmL;
map<int, double> m1;
map<int, double, greater<int> > m2;
multimap<int, double> m3;
multimap<int, double, greater<int> > m4;
m1.insert(CustomPair(1, 2.0));
m1.insert(CustomPair(5, 5.0));
m1.insert(CustomPair(3, 7.0));
m1.insert(CustomPair(4, 8.0));
m1.insert(CustomPair(5, 11.0));
m1.insert(CustomPair(6, 6.0));
cout << "m1:" << endl;
print(m1);
m2.insert(CustomPair(1, 2.0));
m2.insert(CustomPair(5, 5.0));
m2.insert(CustomPair(3, 7.0));
m2.insert(CustomPair(4, 8.0));
m2.insert(CustomPair(5, 11.0));
m2.insert(CustomPair(6, 6.0));
cout << "m2:" << endl;
printG(m2);
m3.insert(CustomPair(1, 2.0));
m3.insert(CustomPair(5, 5.0));
m3.insert(CustomPair(3, 7.0));
m3.insert(CustomPair(4, 8.0));
m3.insert(CustomPair(5, 11.0));
m3.insert(CustomPair(6, 6.0));
cout << "m3:" << endl;
printM(m3);
m4.insert(CustomPair(1, 2.0));
m4.insert(CustomPair(5, 5.0));
m4.insert(CustomPair(3, 7.0));
m4.insert(CustomPair(4, 8.0));
m4.insert(CustomPair(5, 11.0));
m4.insert(CustomPair(6, 6.0));
cout << "m4:" << endl;
printG_M(m4);
map<int, double>::allocator_type ma;
ma = m2.get_allocator();
map<int, double> m5(less<int>(), ma);
m5.insert(CustomPair(16, 1.0));
m5.insert(CustomPair(15, 7.0));
m5.insert(CustomPair(24, 9.0));
m5.insert(CustomPair(23, 12.0));
m5.insert(CustomPair(32, 21.0));
m5.insert(CustomPair(11, 27.0));
cout << "m5:" << endl;
printL_M(m5);
cout << m1.size() << m1.max_size() << endl;
return 0;
}
运行代码结果如下:
m1:
1,6,2.00;
3,2,7.00;
4,2,8.00;
5,2,5.00;
6,2,6.00;
m2:
6,2,6.00;
5,2,5.00;
4,2,8.00;
3,2,7.00;
1,2,2.00;
m3:
1,2,2.00;
3,2,7.00;
4,2,8.00;
5,2,5.00;
5,2,11.00;
6,2,6.00;
m4:
6,2,6.00;
5,2,5.00;
5,2,11.00;
4,2,8.00;
3,2,7.00;
1,2,2.00;
m5:
11,2,27.00;
15,2,7.00;
16,2,1.00;
23,2,12.00;
24,2,9.00;
32,2,21.00;
5134217727
1. 定义形式
map< key, Elem > m;
multimap< key, Elem > m;
2. pair类型
定义形式 | 说明 |
---|---|
pair p1; | 创建一个空的pair对象,它的两个元素对象分别是T1和T2类型,采用值初始化 |
pair p1(v1, v2); | 创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,而secon成员初始化为v2。 |
make_pair(v1, v2) | 以v1和v2值创建一个新的对象,琪元素类型分别是v1和v2的类型 |
p1 < p2 | 两个pair对象之间的小于运算,其定义遵循字典次序:如果p1.first < p2.first 或者 p1.first >= p2.first && p1.second < p2.second,则返回true。 |
p1 == p2 | 两个pair对象的frist和second成员依次相等 |
p.first | 返回p中的frist的数据成员 |
p.second | 返回p中的second的数据成员 |
3. precision() 函数
- precision()返回当前的浮点数的精度值,精度字段指定在小数点之前和之后的总数中要显示的最大有效位数。请注意,它不是最小值,因此如果数字显示的位数小于精度,则不会显示尾随零。
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double n;
cout<<"请输入一个数:"<<endl;
cin>>n;
cout<<"对于"<<n<<"开平方的求解:"<<endl;
double value = sqrt(n);
cout<<value<<endl;
for(int i=1;i<=9;i++)
{
cout.precision(i);
cout<<value<<endl;
}
return 0;
}
运行代码结果如下:
请输入一个数:
3.0
对于3开平方的求解:
1.73205
2
1.7
1.73
1.732
1.7321
1.73205
1.732051
1.7320508
1.73205081
4. fixed函数
控制小数点后面的位数。
#include <iostream>
int main () {
double a = 3.1415926534;
double b = 2006.0;
double c = 1.0e-10;
std::cout.precision(5);
std::cout << "default(默认):\n";
std::cout << a << '\n' << b << '\n' << c << '\n';
std::cout << '\n';
std::cout << "fixed(固定):\n" << std::fixed;
std::cout << a << '\n' << b << '\n' << c << '\n';
std::cout << '\n';
std::cout << "scientific(科学):\n" << std::scientific;
std::cout << a << '\n' << b << '\n' << c << '\n';
return 0;
}
运行结果如下:
default(默认):
3.1416
2006
1e-010
fixed(固定):
3.14159
2006.00000
0.00000
scientific(科学):
3.14159e+000
2.00600e+003
1.00000e-010
5. 成员函数
判空函数 empty( ) 若空返回true
遍历容器 begin() end( ) rbegin( ) rend( )
#include <iostream>
#include <map>
using namespace std;
typedef pair <int, double> mypair;
void Origin(map<int, double, greater<int> > &m, multimap<int, double, greater<int> > &mm)
{
m.insert(mypair(1, 5.0));
m.insert(mypair(2, 7.0));
m.insert(mypair(3, 7.0));
m.insert(mypair(4, 17.0));
m.insert(mypair(5, 11.0));
mm.insert(mypair(1, 4.0));
mm.insert(mypair(2, 8.0));
mm.insert(mypair(3, 9.0));
mm.insert(mypair(4, 12.0));
mm.insert(mypair(5, 19.0));
}
void print(map<int, double, greater<int> > &m, multimap<int, double, greater<int> > &mm)
{
map<int, double, greater<int> >::iterator it;
multimap<int, double, greater<int> >::iterator itM;
int msize = m.size();
cout << "map:" << msize << endl;
for(it = m.begin(); it != m.end(); it++)
{
mypair temp = *it;
cout << temp.first << "," <<temp.second << "," <<endl;
}
int mmsize = mm.size();
cout << "multimap:" << mmsize << endl;
for(itM = mm.begin(); itM != mm.end(); itM++)
{
mypair temp = *itM;
cout << temp.first << "," <<temp.second << "," <<endl;
}
cout << endl;
}
int main()
{
map<int, double, greater<int> > m1;
multimap<int, double, greater<int> > m2;
map<int, double, greater<int> >::iterator it;
map<int, double, greater<int> >::reverse_iterator itR;
multimap<int, double, greater<int> >::iterator itM;
multimap<int, double, greater<int> >::reverse_iterator itRM;
Origin(m1, m2);
print(m1, m2);
it = m1.begin();
cout << (*it).first << "," << (*it).second << ";" << endl;
it = m1.end();
mypair temp = *(--it);
cout << temp.first << "," << temp.second << ";" << endl;
itM = m2.begin();
cout << (*itM).first << "," << (*itM).second << ";" << endl;
itM = m2.end();
temp = *(--itM);
cout << temp.first << "," << temp.second << ";" << endl;
itR = m1.rbegin();
cout << (*itR).first << "," << (*itR).second << ";" << endl;
itR = m1.rend();
temp = *(--itR);
cout << temp.first << "," << temp.second << ";" << endl;
itRM = m2.rbegin();
cout << (*itRM).first << "," << (*itRM).second << ";" << endl;
itRM = m2.rend();
temp = *(--itRM);
cout << temp.first << "," << temp.second << ";" << endl;
return 0;
}
运行结果如下:
map:5
5,11,
4,17,
3,7,
2,7,
1,5,
multimap:5
5,19,
4,12,
3,9,
2,8,
1,4,
5,11;
1,5;
5,19;
1,4;
1,5;
5,11;
1,4;
5,19;