3.9.2、赋值和构造
#include <iostream>
#include <string>
#include <map>
using namespace std;
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
printMap(m);
//拷贝构造
map<int, int>m2(m);
printMap(m2);
//赋值
map<int, int>m3;
m3 = m2;
printMap(m2);
}
int main()
{
test01();
system("pause");
}
3.9.3、大小和交换
#include <iostream>
#include <string>
#include <map>
using namespace std;
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大小为:" << m.size() << endl;
}
}
void test02()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
map<int, int>m2;
m2.insert(pair<int, int>(1, 100));
m2.insert(pair<int, int>(3, 300));
m2.insert(pair<int, int>(2, 200));
m2.insert(pair<int, int>(4, 400));
cout << "交换前:" << endl;
printMap(m);
printMap(m2);
m.swap(m2);
cout << "交换后:" << endl;
printMap(m);
printMap(m2);
}
int main()
{
test02();
test01();
system("pause");
}
3.9.4、插入和删除
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
//[]不建议插入,建议用第二种方法 make_pair
m[4] = 40;
#include <iostream>
#include <string>
#include <map>
using namespace std;
void printMap(map<int,int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = "<<it->first << " value = "<<it->second;
cout << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
//[]不建议插入,建议用第二种方法 make_pair
m[4] = 40;
cout << m[5] << endl;
printMap(m);
//删除
m.erase(m.begin());
printMap(m);
m.erase(3);//按照key删除
printMap(m);
m.erase(m.begin(), m.end());
printMap(m);//清空
m.clear();
printMap(m);
}
int main()
{
test01();
system("pause");
}
3.9.5、查找和统计
查找——find(返回的是迭代器)
统计——count(对于map,结果为0或者1)
#include <iostream>
#include <string>
#include <map>
using namespace std;
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
map<int, int>::iterator pos = m.find(2);
if (pos != m.end())
{
cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
//map不允许重复key元素,count统计而言 结果要么是0要么是1
//multimap的count可能大于1
int num = m.count(3);
cout << "num = " << num << endl;
}
int main()
{
test01();
system("pause");
}
3.9.6、容器排序
利用仿函数可以指定map函数的排序规则
对于自定义数据类型,map必须要指定排序规则,同set容器
#include <iostream>
#include <string>
#include <map>
using namespace std;
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
//降序
return v1 > v2;
}
};
void test01()
{
map<int, int,MyCompare>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second;
cout << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
}