STL中关联容器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;
}

输出:


MSDN上的说明:

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 待学的内容



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)提供了一个名为`std::map`的关联容器,它基于红黑树实现,用于存储键值对,并按照键的顺序进行排序。下面是`std::map`容器的一些常用方法: 1. 插入元素: ```cpp std::map<Key, Value> myMap; myMap.insert(std::make_pair(key, value)); // 使用insert方法插入键值对 myMap[key] = value; // 使用下标操作符[]插入键值对,如果键已存在,则会更新值 ``` 2. 删除元素: ```cpp myMap.erase(key); // 根据键删除元素 myMap.clear(); // 清空所有元素 ``` 3. 访问元素: ```cpp Value value = myMap[key]; // 使用下标操作符[]访问指定键对应的值 auto it = myMap.find(key); // 使用find方法查找指定键的迭代器 if (it != myMap.end()) { Value value = it->second; // 通过迭代器访问指定键对应的值 } ``` 4. 遍历容器: ```cpp for (const auto& pair : myMap) { Key key = pair.first; // 键 Value value = pair.second; // 值 // 其他操作 } ``` 5. 判断元素是否存在: ```cpp if (myMap.count(key) > 0) { // 键存在 } ``` 6. 获取容器大小和判断容器是否为空: ```cpp size_t size = myMap.size(); // 获取容器键值对的个数 bool isEmpty = myMap.empty(); // 判断容器是否为空 ``` 这些是`std::map`容器的一些常用方法,还有其他一些方法和成员函数可以进一步扩展其功能。你可以参考C++标准库的文档以获取更详细的信息。 希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值