转自http://blog.sina.com.cn/daylive——C++ STL map
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!
1.map最基本的构造函数;
map<string , int >mapstring; map<int ,string >mapint; map<string, char>mapstring; map< char ,string>mapchar; map<char ,int>mapchar; map<int ,char >mapint;
2. map添加数据;
map<int ,string> maplive; maplive.insert(pair<int,string>(,"aclive")); maplive.insert(map<int,string>::value_type(,"hai")); maplive[]="April";//map中最简单最常用的插入添加!
3,map中元素的查找:
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;; l_it=maplive.find(); if(l_it==maplive.end()) cout<<"we do not find 112"<<endl; else
cout<<"wo find 112"<<endl;
4,map中元素的删除: 如果删除112;
map<int ,string >::iterator l_it;; l_it=maplive.find(); if(l_it==maplive.end()) cout<<"we do not find 112"<<endl; else maplive.erase(l_it); //delete 112;
5,map中 swap的用法: Map中的swap不是一个容器中的元素交换,而是两个容器交换;
For example:
#include <map> #include <iostream> using namespace std; int main( ) { map <int, int> m1, m2, m3; map <int, int>::iterator m1_Iter; m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); m2.insert ( pair <int, int> ( , ) ); m2.insert ( pair <int, int> ( , ) ); m3.insert ( pair <int, int> ( , ) ); cout << "The original map m1 is:"; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << " " << m1_Iter->second; cout << "." << endl; m1.swap( m2 ); // This is the member function version of swap //m2 is said to be the argument map; m1 the target map cout << "After swapping with m2, map m1 is:"; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << " " << m1_Iter -> second; cout << "." << endl; cout << "After swapping with m2, map m2 is:"; for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ ) cout << " " << m1_Iter -> second; cout << "." << endl; swap( m1, m3 ); // This is the specialized template version of swap cout << "After swapping with m3, map m1 is:"; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << " " << m1_Iter -> second; cout << "." << endl; }
6.map的sort问题: Map中的元素是自动按key升序排序,所以不能对map用sort函数:
For example:
#include <map> #include <iostream> using namespace std; int main( ) { map <int, int> m1; map <int, int>::iterator m1_Iter; m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); m1.insert ( pair <int, int> ( , ) ); cout << "The original map m1 is:"<<endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << m1_Iter->first<<" "<<m1_Iter->second<<endl; } The original map m1 is: 请按任意键继续. . .
7, 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的函数
转自http://blog.sina.com.cn/daylive——C++ STL map的更多相关文章
- 转自http://blog.sina.com.cn/daylive——C++ STL set&multiset
C++ STL set和multiset的使用 1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样.所有的操作的都是严格在logn时间 ...
- http://blog.sina.com.cn/s/blog_4c3b6a070100etad.html
http://blog.sina.com.cn/s/blog_4c3b6a070100etad.html
- http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
- quartus ii13.0~16.0 调用uedit (转载http://blog.sina.com.cn/s/blog_6d5560f00102vax6.html)
转自 http://blog.sina.com.cn/s/blog_6d5560f00102vax6.html Quartus II 中的文本编辑软件不好用,比较习惯与UE(Uedit32/ultra ...
- http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html
http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html http://www.oschina.net/question/117304_51525
- http://blog.sina.com.cn/s/blog_6940cab30101hn9j.html
http://blog.sina.com.cn/s/blog_6940cab30101hn9j.html
- http://blog.sina.com.cn/s/blog_705cc5dd01012ehb.html
http://blog.sina.com.cn/s/blog_705cc5dd01012ehb.html
- 新浪博客地址 http://blog.sina.com.cn/u/2145079955
原来 新浪博客地址 http://blog.sina.com.cn/u/2145079955
- http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html
http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html
随机推荐
- 非空验证(源代码Java版)
import java.util.Map; /** * 非空验证工具类 */ public class UntilEmpty { /** * @see: 验证string类型的是否为空 */ publ ...
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- 【算法】最长公共子序列(nlogn)
转载注明出处:http://blog.csdn.net/wdq347/article/details/9001005 (修正了一些错误,并自己重写了代码) 最长公共子序列(LCS)最常见的算法是时间复 ...
- php SESSION 不能跨页面传递
今天想用一个session来实现用户登录判断,也算是对之前session的探究,查了下资料session的运行机制如下: session是服务器端的一种会话机制,当客户端的请求服务器创建一个sessi ...
- JavaScript—window对象使用
window对象是JavaScript浏览器对象模型中的顶层对象,包含多个常用方法和属性: 1. 打开新窗口 window.open(pageURL,name,parameters) 其中:pageU ...
- 关于——GCD
GCD全称是Grand Central Dispatch,可译为“牛逼的中枢调度器”,纯C语言提供了强大的函数. GCD中2个核心概念 任务:执行什么操作. 队列:用来存放任务.(说白点,任务只有 ...
- Objective-C学习篇06—NSString与NSMutableString
NSString OC提供了定义字符串对象的方法,也就是将想要表达的字符串用一对双引号引起来,并在开头加上@.@是OC中的指令符,它告诉编译器@以后的内容为OC中的语法.比如@”Harbingwang ...
- 花非花-记一次linux上运行时报找不到库函数错误
简介: --->:表示依赖 exe ---> a.so ---> utility.so 问题描述: exe运行起来报a.so中的函数f未定义. 解决过程: 一·nm a.so nm ...
- 事后调试之MiniDump转储
程序发布后,针对在用户使用过程中出现的一些问题进行调试,这个过程可以称为是事后调试.在程序Crash时转储MiniDump文件供软件开发工程师分析是一种比较常用的方法.下面介绍两种常用的在程序Cras ...
- iOS开发——常用宏的定义
有些时候,我们需要将代码简洁化,这样便于读代码.我们可以将一些不变的东东抽取出来,将变化的东西作为参数.定义为宏,这样在写的时候就简单多了. 下面例举了一些常用的宏定义和大家分享: 1. 判断设备的操 ...