map是一种关联容器,它定义了一种数据结构,类似查表功能,每一个数据都有自己的键值,通过键值可以查找和排序相应的数据(或者说每个元素都有两个值,一个叫“键”,一个叫“值”,可以通过“键”找到相关的“值”)。
Map 待学的内容
包含了#include <map>这个头文件,就可以使用map这种数据结构。
下面的程序有些东西还需深入学习:
#include <stdlib.h>
#include <conio.h>
#include <map>
#include <functional>
#include <algorithm>
#include <iostream>
using namespace std;
typedef map<int, int*>m_iip;
typedef map<int, char*>m_icp;
class f_C
{
int _i;
public:
f_C(int i) :_i(i){}
void operator()(m_iip::value_type ite)
{
cout << _i++ << "\t" << ite.first << "\tshi" << endl;
}
void operator()(m_icp::value_type ite)
{
cout << _i++ << "\t" << ite.first << "\tyang" << endl;
}
f_C();
~f_C();
private:
};
f_C::f_C()
{
}
f_C::~f_C()
{
}
int main()
{
m_icp icp;
m_iip iip;
int i = 0;
iip.insert(make_pair(34, &i));
iip.insert(make_pair(67, &i));
iip.insert(make_pair(5, &i));
iip.insert(make_pair(342, &i));
char d = 0;
icp.insert(make_pair(12, &d));
icp.insert(make_pair(54, &d));
icp.insert(make_pair(6, &d));
icp.insert(make_pair(92, &d));
for_each(iip.begin(), iip.end(), f_C(8));
cout << endl;
for_each(icp.begin(), icp.end(), f_C(65));
return 0;
}
输出:
template <
class Key,
class Type,
class Traits = less<Key>,
class Allocator=allocator<pair <const Key, Type> >
> class map;
Parameters:
Key
The key data type to be stored in the map.
Type
The element data type to be stored in the map.
Traits
The type that provides a function object that can compare two element values as sort keys to determine their relative order in the map. This argument is optional and the binary predicate less<Key> is the default value.
Allocator
The type that represents the stored allocator object that encapsulates details about the map's allocation and deallocation of memory. This argument is optional and the default value is allocator<pair <constKey ,Type> >.
***************************
Map 待学的内容