map用法
来源:互联网 发布:php记账系统源码 编辑:程序博客网 时间:2024/06/05 00:35
1、map的基本操作函数:
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
2、
#include <iostream> #include <map> using namespace std; int main() { /************************************************************************/ /* map的常用方法示例 */ /************************************************************************/ // map是一种关联容器类,里面存储的元素类型为pair<const KEY, DATA>。不同的元素KEY值不同。 // 定义map及其对应的迭代器 map<char, int> mapTest; map<char, int>::iterator iterTest; // 在map中插入元素 // 这种利用下标值的插入方式,当map中没有对应键值的元素时,插入。当map中存在对应键值的元素时,修改其值或者获取其值。 mapTest['a'] = 10; mapTest['b'] = 20; mapTest['c'] = 30; mapTest['a'] = 15; // 这种使用insert的插入方式,当map中没有对应键值的元素时,插入。当map中存在对应键值的元素时,不插入元素。 pair<map<char, int>::iterator, bool> ret; mapTest.insert(map<char, int>::value_type('d', 40)); ret = mapTest.insert(make_pair('d', 50)); mapTest.insert(make_pair('e', 50)); // 当使用insert函数后会返回pair<map<char, int>::iterator, bool>类型的值,bool值表示是否插入成功。迭代器指向插入的元素。 cout << ret.second << endl; // map中查找某个指定键值的元素,查找成功则返回对应的迭代器。查找失败则返回.end()对应的容器边界迭代器。 iterTest = mapTest.find('f'); cout << (iterTest == mapTest.end()) << " find: 0 means success, 1 means failure"<< endl; // 正向遍历 cout << "正向" << endl; for (iterTest = mapTest.begin(); iterTest != mapTest.end(); iterTest++) { cout << iterTest->first << " " << iterTest->second << endl; } // 反向遍历 cout << "反向" << endl; map<char, int>::reverse_iterator iter; for (iter = mapTest.rbegin(); iter != mapTest.rend(); iter++) { cout << iter->first << " " << iter->second << endl; } // 使用size获取容器中元素个数 int num; num = (int)mapTest.size(); cout << num << endl; // 使用clear清空容器 mapTest.clear(); // 使用empty判断容器是否为空 if (mapTest.empty()) { cout << "The map is empty" << endl; } return 0; }
阅读全文
0 0
- map用法
- map用法
- map用法
- Map用法
- map用法
- Map用法
- map用法
- map用法
- MAP用法
- map用法
- map用法
- map用法
- map 用法
- MAP用法
- map用法
- map用法
- map用法
- map用法
- 使用Base64加密解密文件
- Guice Providers注解和Names模板
- 简单理解Socket及TCP/IP、Http、Socket的区别
- java.lang.IllegalArgumentException: Illegal character in query at index
- 从执行角度解释:return与finally执行顺序
- map用法
- C++抽象编程——指针(3)——指针运算
- 多线程共享数据
- 在C#中调用EVAL函数方法,通过字符串计算
- ubuntu14.04英文环境下安装中文输入法
- 基础数学
- 利用xinetd进行时间同步
- 数组名和取地址数组名的区别
- lakeshore自学笔记(一)