C++之map容器
map构造和赋值
#include<iostream>
#include<string>
using namespace std;
#include<map>void printMap(map<int,int>&m)
{for (map<int,int>::iterator it = m.begin();it != m.end();it++){//cout <<"key is: "<< (*it).first << " value is "<<(*it).second<<endl;cout << "key is: " << it->first << " value is " << it->second << endl;}cout << endl;
}void test()
{//创建map容器map<int, int>m;m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(2, 50));m.insert(pair<int, int>(3, 30));m.insert(pair<int, int>(4, 20));m.insert(pair<int, int>(5, 40));printMap(m);//拷贝构造map<int, int>m2(m);printMap(m2);//赋值map<int, int>m3;m3 = m;printMap(m3);
}int main()
{test();system("pause");return 0;
}
map大小和交换
#include<iostream>
#include<string>
using namespace std;
#include<map>void printMap(map<int,int>&m)
{for (map<int,int>::iterator it = m.begin();it != m.end();it++){//cout <<"key is: "<< (*it).first << " value is "<<(*it).second<<endl;cout << "key is: " << it->first << " value is " << it->second << endl;}cout << endl;
}void test()
{//创建map容器map<int, int>m;m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(2, 50));m.insert(pair<int, int>(3, 30));m.insert(pair<int, int>(4, 20));m.insert(pair<int, int>(5, 40));printMap(m);if (m.empty()){cout << "m is empty" << endl;}else{cout << "m is not empty" << endl;cout << "m's size is" << m.size() << endl;}map<int, int>m2;m2.insert(pair<int, int>(1, 100));m2.insert(pair<int, int>(2, 500));m2.insert(pair<int, int>(3, 300));m2.insert(pair<int, int>(4, 200));m2.insert(pair<int, int>(5, 400));cout << "before swap" << endl;printMap(m);printMap(m2);m2.swap(m);cout << "after swap" << endl;printMap(m);printMap(m2);
}int main()
{test();system("pause");return 0;
}
map插入和删除
#include<iostream>
#include<string>
using namespace std;
#include<map>void printMap(map<int,int>&m)
{for (map<int,int>::iterator it = m.begin();it != m.end();it++){//cout <<"key is: "<< (*it).first << " value is "<<(*it).second<<endl;cout << "key is: " << it->first << " value is " << it->second << endl;}cout << endl;
}void test()
{//创建map容器map<int, int>m;//插入//第一种m.insert(pair<int, int>(1, 10));//第二种m.insert(make_pair(2, 50));//第三种m.insert(map<int, int>::value_type(3, 30));//第四种 不建议使用 用途:使用Key找到valuem[4] = 20;printMap(m);//删除m.erase(m.begin());printMap(m);m.erase(2);//按照key值删除printMap(m);//清空//m.erase(m.begin(),m.end());m.clear();printMap(m);
}int main()
{test();system("pause");return 0;
}
map查找和统计
#include<iostream>
#include<string>
using namespace std;
#include<map>void printMap(map<int,int>&m)
{for (map<int,int>::iterator it = m.begin();it != m.end();it++){//cout <<"key is: "<< (*it).first << " value is "<<(*it).second<<endl;cout << "key is: " << it->first << " value is " << it->second << endl;}cout << endl;
}void test()
{//创建map容器map<int, int>m;//插入m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(2, 50));m.insert(pair<int, int>(3, 30));m.insert(pair<int, int>(4, 20));printMap(m);map<int,int>::iterator pos= m.find(2);if (pos != m.end()){cout << "找到了:key is " << pos->first << " value is " << (*pos).second << endl;}else{cout << "没有找到" << endl;}
//map不允许插入重复key 元素 ,count统计而言 结果要么是0 要么是1
//multimap的count统计可能大于1//统计int num = m.count(3);//按照key查找cout << "num is " << num << endl;
}int main()
{test();system("pause");return 0;
}
map容器排序
#include<iostream>
#include<string>
using namespace std;
#include<map>class MyCompare
{
public:bool operator()(int v1, int v2){return v1 > v2;}
};void test()
{//创建map容器map<int, int, MyCompare>m;//插入m.insert(make_pair<int, int>(1, 10));m.insert(make_pair<int, int>(2, 50));m.insert(make_pair<int, int>(3, 30));m.insert(make_pair<int, int>(4, 20));for (map<int, int, MyCompare>::iterator it = m.begin();it != m.end();it++){//cout <<"key is: "<< (*it).first << " value is "<<(*it).second<<endl;cout << "key is: " << it->first << " value is " << it->second << endl;}cout << endl;}int main()
{test();system("pause");return 0;
}