map函数,java和C++的常见用法
来源:互联网 发布:接线示意图制作软件 编辑:程序博客网 时间:2024/05/27 02:30
map函数是一个很常用到的映射函数,他在算法分析的时候有着举足轻重的作用,他可以结果超大空间的问题,比如说需要开一个很大的数组来表示一组关系,比如说要记录下一个学生的学号和姓名,总共有10^9之多的学生,如果使用一维数组是承受不了的,这个时候只能使用map函数,来记录这么庞大的数组。
下边先来看下c++的map函数的用法:
#include<map>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n = 3;
/*map<int, string> a;
for (int i = 0; i < n; i++){//可以直接使用[]运算符来表示这一个关系,这个关系已经被重载过了的,不用担心会用错
a[i] ="123";
}
a[20]="456";
map<int, string>::iterator it;
for (it = a.begin(); it != a.end(); it++){
cout << it->first << ":" << it->second << endl;
}
0 123
1 123
2 123
3 123
20 456
*/
/////////////////////////////////////////////////////////////////////////////////////
/*map<char, int> b;
for (int i = 0; i < n; i++){
b[i+'0'] = i;
}
b[20+'0']=20;
map<char,int>::iterator it2;
for (it2 = b.begin(); it2 != b.end(); it2++){
cout << it2->first << ":" << it2->second << endl;
}
0 0
1 1
2 2
3 3
20 20
*/
/////////////////////////////////////////////////////////////////////////////////////
/*map<char, string>c;
for (int i = 0; i < n; i++){
c[i + '0'] = "wang";
}
c[20+'0']="wang";
map<char,string>::iterator it3;
for (it3 = c.begin(); it3 != c.end(); it3++){
cout << it3->first << ":" << it3->second << endl;
}
0 wang
1 wang
2 wang
3 wang
20 wang
*/
/////////////////////////////////////////////////////////////////////////////////////
return 0;
}
下边来看java的map函数:
package Main;
import java.util.*;
import org.omg.PortableInterceptor.Interceptor;
import java.io.*;
class Main{
public static void cout(Object s) {
if(s instanceof Map){
//方法1,只输出value一值,key并没有,要得到key,需要用到迭代器,如方法二
// for(Object o:((Map) s).keySet()) {
// cout(((Map) s).get(o));
// }cout("");
//方法2
Iterator it=((Map) s).entrySet().iterator();
while(it.hasNext()) {
Map.Entry entry=(Map.Entry)(it.next());
cout(entry.getKey().toString()+":"+entry.getValue().toString());
}
return;
}
System.out.println(s);
}
public static void main(String args[]) {
Map map=new HashMap();
map.put(2, "123");
map.put(1, 4);
map.put('3', "456");//put函数是自动帮你排好序的,对应于key从小到大自动排序;
cout(map.get(2));cout("");
cout("----------------------");
cout(map);
cout("++++++++++++++++++++++");
map.clear();
cout(map);
cout("\\\\\\\\\\\\\\\\\\\\\\");
HashMap map1=new HashMap();
map1.put("1", "A");
HashMap map2 = new HashMap();
map2.put("2", "B");
map2.put("3", "C");
map1.putAll(map2);
System.out.println(map1);
//直接使用这样的输出就会是{1=A, 2=B, 3=C}
//两个map具有重复的key
HashMap map3=new HashMap();
map3.put("1", "A");
HashMap map4 = new HashMap();
map4.put("1", "B");
map4.put("3", "C");
map3.putAll(map4); //如果发现有相同的key就会覆盖前边的value;
//当然这里的put都会有自动从小到大排序;
System.out.println(map3);
cout("-----------------------------");
Map<String, String > map5=new TreeMap<String,String>(
new Comparator<String>() {
public int compare(String a,String b) {
return b.compareTo(a);//降序排序;
}
});
for(int i=0;i<5;i++) {
map5.put("2", "555");
map5.put("5", "7777777");
map5.put("4", "1");
}//这里的key是重复的,所以后来的都会被忽略掉的。
cout(map5);cout("");
}
}
123
----------------------
1:4
2:123
3:456
++++++++++++++++++++++
\\\\\\\\\\\
{1=A, 2=B, 3=C}
{1=B, 3=C}
-----------------------------
5:7777777
4:1
2:555
- map函数,java和C++的常见用法
- java中Map常见用法
- JAVA中MAP的四种类型区别和常见的简单用法
- C++MAP和vector的用法
- c++map的基本操作和用法
- Map的常见用法总结
- std::map的一些常见用法和个别注意事项
- c++map的用法
- c++map的用法
- c++map的用法
- C++map的用法
- c++map的用法
- c++map的用法
- c++Map的用法
- java 集合list和map的用法
- java深度探究---Map常见用法,遍历
- Python中的map()函数和reduce()函数的用法
- Python中的map()函数和reduce()函数的用法
- FileItem类
- Struts2框架整理(HelloWorld环境搭建)
- LIST_找出链表的共同后缀
- VitrualBox 做共享ASM磁盘,测试通不过,报rac1:Unable to determine the sharedness of /dev/sdb on nodes:
- js鼠标滚动事件
- map函数,java和C++的常见用法
- 会谈报表
- java代码调用webservice接口
- 知道这20个正则表达式,能让你少写1,000行代码
- 面谈报告(1)
- 5. Longest Palindromic Substring
- LIST_求链表中倒数第K个结点的值
- 问卷
- 博弈论入门(1)——游戏与必胜策略