map函数
来源:互联网 发布:九宫图算法应用 编辑:程序博客网 时间:2024/06/06 14:14
以前搜集的知识,现在拿出来分享一下,在这标注个原创的声明,其实是找不到最初文档的链接了,无论如何,向那些原创人致敬
/******************************************************************
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() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
====================================================================
1、map构造
map<int, string> mapStudent;
2、map添加数据
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(map<int, string>::value_type(2, "student_two"));
mapStudent[3] = "student_three";
********************************************************************/
#pragma warning (disable:4786)
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
cout<<"三种插入方式:"<<endl;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(map<int, string>::value_type(2, "student_two"));
mapStudent[3] = "student_three";
mapStudent.insert(map<int, string>::value_type(4, "student_four"));
pair<map<int,string>::iterator,bool> InsertPair; //判断是否插入成功
InsertPair = mapStudent.insert(map<int,string>::value_type(5,"student_five"));
if(InsertPair.second == true)
{
//cout<<InsertPair.first.operator++<<endl; //求解??不知道怎么应用第一个数据
}
cout<<"三种遍历方式:"<<endl;
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
map<int, string>::reverse_iterator iters;
for(iters = mapStudent.rbegin(); iters != mapStudent.rend(); iters++)
{
cout<<iters->first<<" "<<iters->second<<endl;
} //逆序输出
cout<<"数组的输出形式:"<<endl;
for(int iIndex=0;iIndex < mapStudent.size();iIndex++) //size()返回成员的个数
{
cout<<mapStudent[iIndex]<<endl;
}
cout<<mapStudent.count(1)<<endl; //count()判断关键字是否存在,返回1表示存在,0
iter = mapStudent.find(1); //find()关键字存在时,返回数据所在位置的迭代器,否则返回end()返回的迭代器
if( iter != mapStudent.end() )
{
cout<<"数据存在:"<<iter->first<<" "<<iter->second<<endl;
mapStudent.erase(iter); //用迭代删除数据
}
else
{
cout<<"数据不存在!"<<endl;
}
int n = mapStudent.erase(3); //用关键字删除,如果删除了会返回1,否则返回0
iter = mapStudent.lower_bound(2); //返回2的迭代器
cout<<iter->second<<endl;
iter = mapStudent.upper_bound(2); //返回3的迭代器
cout<<iter->second<<endl;
/*Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字*/
pair<map<int,string>::iterator,map<int,string>::iterator> MapPair;
MapPair = mapStudent.equal_range(2);
if( MapPair.first == MapPair.second )
{
cout<<"Do not find"<<endl;
}
else
{
cout<<"Find"<<endl;
}
//删除一个前闭后开的集合,这是STL的特性
mapStudent.earse(mapStudent.begin(), mapStudent.end());
- map 函数
- Map 函数
- map函数
- map函数
- map函数
- map()函数
- 【map函数】
- map函数
- map()函数
- map函数
- map函数
- filter()函数 map()函数
- 函数编程-map()
- perl map函数使用
- map自定义比较函数
- map自定义比较函数
- c++ map 函数
- perl map函数学习
- 程矢Axure夜话:程序员眼中的原型设计视频教程之字符数范围
- Android的SDK中相关资源下载及说明
- 直接拿来用!最火的Android开源项目(一)
- Codeforces Round #276 (Div. 1) B. Maximum Value
- 程矢Axure夜话: Axure嵌入代码系列视频教程汇总贴
- map函数
- Java String类涉及到UTF16编码的几个概念
- 【打表】POJ-2739 Sum of Consecutive Prime Numbers
- linux中sed的用法详解(对行数据的添加、删除等)
- 直接拿来用!最火的Android开源项目(二)
- 第1章 开始
- 程矢Axure夜话:嵌入代码系列视频教程之QQ一键加群
- linux 截图工具scrot
- RabbitMQ学习之spring整合发送异步消息(注解实现)