[C++STL]map容器用法介绍
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C++STL]map容器用法介绍相关的知识,希望对你有一定的参考价值。
代码如下:
#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>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
printMap(m);
map<int, int>m2(m);
printMap(m2);
map<int, int>m3;
m3 = m2;
printMap(m3);
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
map中所有元素都是成对出现,插入数据时候要使用对组。
代码如下:
#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>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
if (m.empty())
{
cout << "m empty" << endl;
}
else
{
cout << "m no empty" << endl;
cout << "m size = " << m.size() << endl;
}
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前" << endl;
printMap(m);
printMap(m2);
cout << "交换后" << endl;
printMap(m);
printMap(m2);
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
代码如下:
#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(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
printMap(m);
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
m.erase(m.begin(), m.end());
m.clear();
printMap(m);
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
代码如下:
#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>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "find key = " << (*pos).first << "value = " << (*pos).second << endl;
}
else
{
cout << "no find" << endl;
}
int num = m.count(3);
cout << "num = " << num << endl;
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
代码如下:
#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;
}
class cmp
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test01()
{
map<int, int, cmp>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, cmp>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << "value = " << it->second << endl;
}
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
以上是关于[C++STL]map容器用法介绍的主要内容,如果未能解决你的问题,请参考以下文章