C++ Map快速入门
来源:互联网 发布:在哪里下载软件 编辑:程序博客网 时间:2024/06/05 22:27
Map是一个关联性容器
map的存储类型是pair<const key,value>的类型
任何两个元素没有相同的key值
map是
1 、构造函数
map<const char*,int>
map<const char*,int,status>
其中status为Comparison类,定义key的排序顺序,也就定义了整个map的key->value对存储顺序
bool status(const char *s1, const char* s2)
{
return strcmp(s1,s2)<0;
}
2、 迭代器
map<const char *key,const char *value>::iterator it;
it -> first; //访问key
it -> second;//访问value
3、常用函数
find(class key)//查找指定的key,返回相应的迭代器位置,找不到则迭代器处于末尾
insert(pair<class key,class value>)//插入一个pair类型的数据
clear()//清空整个map的数据
empty()//判断map是否为空,返回bool类型值
begin()/rbgeion() // 返回迭代器指向第一个/最后一个元素位置
end()/rend( ) //返回最后一个元素下一个位置/第一个元素的下一个位置
4 示例代码
#include "stdafx.h"#include <iostream>#include <map>using namespace std;int _tmain(int argc, _TCHAR* argv[]){map<int,const char *> mymap;mymap[0] = "Jack";mymap[1] = "Lucy";mymap.insert(pair<int,const char *>(3,"Mike"));mymap.insert(pair<int,const char *>(4,"Lily"));cout<<"Use iterator"<<endl;map<int,const char*>::iterator it = mymap.begin();while (it != mymap.end()){cout<<it->first<<"->"<<it->second<<endl;it++;}cout<<"Use reverse_iterator"<<endl;map<int,const char*>::reverse_iterator reit = mymap.rbegin();while (reit != mymap.rend()){cout<<reit->first<<"->"<<reit->second<<endl;reit++;}it = mymap.find(1);if ( it != mymap.end()){cout<<"use find(1):"<<it->second<<endl;>}else{cout<<"Cannot find(1)"<<endl;}mymap.clear();mymap[5] = "New";mymap[6] = "Temp";cout<<"Current size"<<mymap.size()<<endl;}
5 所有关于map的函数如下
template <class InputIterator>map(InputIterator f, InputIterator l)Unique Sorted Associative ContainerCreates a map with a copy of a range.
template <class InputIterator>map(InputIterator f, InputIterator l, const key_compare& comp)Unique Sorted Associative ContainerCreates a map with a copy of a range, using compas thekey_compare object.map(const map&)ContainerThe copy constructor.map& operator=(const map&)ContainerThe assignment operatorvoid swap(map&)ContainerSwaps the contents of two maps.
pair<iterator, bool>insert(const value_type& x)Unique Associative ContainerInserts x into the map.
iterator insert(iterator pos, const value_type& x)Unique Sorted Associative ContainerInserts x into the map, using pos as a hint to where it will be inserted.
template <class InputIterator>void insert(InputIterator, InputIterator)[2]Unique Sorted Associative ContainerInserts a range into the map.void erase(iterator pos)Associative ContainerErases the element pointed to by pos.size_type erase(const key_type& k)Associative ContainerErases the element whose key is k.void erase(iterator first, iterator last)Associative ContainerErases all elements in a range.void clear()Associative ContainerErases all of the elements.iterator find(const key_type& k)Associative ContainerFinds an element whose key is k.const_iterator find(const key_type& k) constAssociative ContainerFinds an element whose key is k.size_type count(const key_type& k)Unique Associative ContainerCounts the number of elements whose key is k.iterator lower_bound(const key_type& k)Sorted Associative ContainerFinds the first element whose key is not less thank.const_iterator lower_bound(const key_type& k) constSorted Associative ContainerFinds the first element whose key is not less thank.iterator upper_bound(const key_type& k)Sorted Associative ContainerFinds the first element whose key greater than k.const_iterator upper_bound(const key_type& k) constSorted Associative ContainerFinds the first element whose key greater than k.
pair<iterator, iterator> equal_range(const key_type& k)Sorted Associative ContainerFinds a range containing all elements whose key is k.
pair<const_iterator, const_iterator> equal_range(const key_type& k) constSorted Associative ContainerFinds a range containing all elements whose key is k.
data_type& operator[](const key_type& k) [3]mapSee below.
bool operator==(const map&, const map&)Forward ContainerTests two maps for equality. This is a global function, not a member function.
bool operator<(const map&, const map&)Forward ContainerLexicographical comparison. This is a global function, not a member function.
0 0
- C++ Map快速入门
- map结构快速入门
- object-c 快速入门
- Objective-C 快速入门
- object-c快速入门
- Objective-C 快速入门
- Object-c 快速入门
- ECMAscript6快速入门-set和map
- C#--快速入门(一)
- 快速入门学好C语言
- Objective C 快速入门诗
- 《C++Primer》 第一章 快速入门
- Objective-C语法快速入门
- Objective-C语法快速入门
- Object+C语法快速入门
- c++Primer,一,快速入门
- Object+C语法快速入门
- Object C语法快速入门
- 阿里巴巴之孕育、裂变及归因
- dll加载问题的解决方法
- 腾讯2014实习生招聘经历谈
- 通讯录图库界面,两种cell的定制
- 在控制台中动态引入jquery的方法
- C++ Map快速入门
- IOS 6 自动布局 入门-1
- Perl、PHP、Python、Java和Ruby的比较
- 平台商业模式成就了阿里巴巴
- 网页基础第1课之“ 利用 session 实现循环3次登录验证”
- Eclipse衍生版本之ADT Bundle
- 腾讯2014年实习生招聘笔试面试经历
- hdu 5003 Osu! 水题 2014 ACM/ICPC Asia Regional Anshan Online
- intellij快捷键