`

C++中map的常用方法

阅读更多
/************************************************************************/
/*
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,
第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,
在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),
这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
1. map的构造函数  
map<int, string> maphai;
map<char,int> maphai;
map<string,char> mapstring;
map<string,int> mapstring;
map<int ,char>mapint;
map<char,string>mapchar;  
*/
/************************************************************************/

#include <map>
#include <string>
#include <iostream>
using namespace std;

int main(){	

	// 第一种:用insert函数插入pair数据
	map<int, string> mapStudent;	
	mapStudent.insert(pair<int, string>(1, "student_one"));	
	mapStudent.insert(pair<int, string>(2, "student_two"));	
	mapStudent.insert(pair<int, string>(3, "student_three"));
	mapStudent.insert(pair<int,string>(3,"student_four"));
	map<int, string>::iterator iter;
	for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
		cout<<iter->first<<""<<iter->second<<endl;
	}

	// 用insert函数插入value_type数据,下面举例说明
	map<int, string> mapStudent1;	
	mapStudent1.insert(map<int, string>::value_type (1, "student_one"));	
	mapStudent1.insert(map<int, string>::value_type (2, "student_two"));	
	mapStudent1.insert(map<int, string>::value_type (3, "student_three"));
	mapStudent1.insert(map<int,string>::value_type (3,"student_four"));
	map<int, string>::iterator iter1;	
	for(iter1 = mapStudent.begin(); iter1 != mapStudent.end(); iter1++){		
		cout<<iter1->first<<""<<iter1->second<<endl;	
	}

	// 用数组方式插入数据,下面举例说明
	map<int, string> mapStudent2;	
	mapStudent2[1] = "student_one";
	mapStudent2[2] = "student_two";
	mapStudent2[3] = "student_three";
	mapStudent2[3] = "student_four";
	map<int, string>::iterator iter2;	
	for(iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++){
		cout<<iter2->first<<""<<iter2->second<<endl;
	}

	// 演示插入成功与否问题
	map<int, string> mapStudent3;
	pair<map<int, string>::iterator, bool> Insert_Pair;
	Insert_Pair = mapStudent3.insert(pair<int, string>(1, "student_one"));
	if(Insert_Pair.second == true){		
		cout<<"Insert Successfully"<<endl;
	}else{
		cout<<"Insert Failure"<<endl;
	}
	Insert_Pair = mapStudent3.insert(pair<int, string>(1, "student_two"));
	if(Insert_Pair.second == true){		
		cout<<"Insert Successfully"<<endl;
	}else{
		cout<<"Insert Failure"<<endl;
	}
	map<int, string>::iterator iter3;
	
	for(iter3 = mapStudent3.begin(); iter3!=mapStudent3.end(); iter3++)
	{
		cout<<iter3->first<<""<<iter3->second<<endl;		
	}

	// 数组插入在数据覆盖上的效果
	map<int, string> mapStudent4;
	
	mapStudent4[1] = "student_one";
	
	mapStudent4[1] = "student_two";
	
	mapStudent4[2] = "student_three";
	
	map<int, string>::iterator iter4;
	
	for(iter4 = mapStudent4.begin(); iter4 != mapStudent4.end(); iter4++){
		cout<<iter4->first<<""<<iter4->second<<endl;
	}


    // 输出map的长度
	int nSize = mapStudent.size();
	cout << nSize<<endl;

	//应用反相迭代器,下面举例说明,要体会效果,请自个动手运行程序
	map<int, string> mapStudent5;
	mapStudent5.insert(pair<int, string>(1, "student_one"));	
	mapStudent5.insert(pair<int, string>(2, "student_two"));	
	mapStudent5.insert(pair<int, string>(3, "student_three"));	
	map<int, string>::reverse_iterator iter5;
	for(iter5 = mapStudent5.rbegin(); iter5 != mapStudent5.rend(); iter5++){
		cout<<iter5->first<<""<<iter5->second<<endl;
	}

	// 
	map<int, string> mapStudent6;
	
	mapStudent6.insert(pair<int, string>(1, "student_one"));
	
	mapStudent6.insert(pair<int, string>(2, "student_two"));
	
	mapStudent6.insert(pair<int, string>(3, "student_three"));
	
	int nSize1 = mapStudent6.size();
	//此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++) 
	//by rainfish
	for(int nIndex = 0; nIndex < nSize1; nIndex++){
		cout<<mapStudent6[nIndex]<<endl;
	}
	
	/************************************************************************/
	/* 用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,
	它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器*/
	/************************************************************************/
	map<int, string> mapStudent7;	
	mapStudent7.insert(pair<int, string>(1, "student_one"));
	mapStudent7.insert(pair<int, string>(2, "student_two"));
	mapStudent7.insert(pair<int, string>(3, "student_three"));
	map<int, string>::iterator iter7;
	iter7 = mapStudent7.find(1);
	if(iter7 != mapStudent7.end()){
		cout<<"Find, the value is "<<iter7->second<<endl;
	}else{
		cout<<"Do not Find"<<endl;
	}

	// 判断map 是否为空
	if(mapStudent7.empty()){
		cout << "map is empty" <<endl;
	} else {
		cout <<"map is not empty" << endl;
	}
	// 清除map
	mapStudent7.clear();
	if(mapStudent7.empty()){
		cout << "map is empty" <<endl;
	} else {
		cout <<"map is not empty" << endl;
	}


	map<int, string> mapStudent8;	
	mapStudent8.insert(pair<int, string>(1, "student_one"));
	mapStudent8.insert(pair<int, string>(2, "student_two"));
	mapStudent8.insert(pair<int, string>(3, "student_three"));
	map<int, string>::iterator iter8;
	// 通过迭代器删除
	iter8 = mapStudent8.find(1);
	mapStudent8.erase(iter8);
	
	for(iter8 = mapStudent8.begin(); iter8 != mapStudent8.end(); iter8++){
		cout<<iter8->first<<""<<iter8->second<<endl;
	}

	//如果删除了会返回1,否则返回0
	int retValue = mapStudent8.erase(1);
	cout << retValue <<endl;
	retValue = mapStudent8.erase(2);
	cout << retValue <<endl;

	return 1;
}

 

分享到:
评论

相关推荐

    map遍历的四种方法

    在Java编程语言中,`Map`是一种常用的数据结构,用于存储键值对。为了能够有效地处理和操作这些键值对数据,熟练掌握遍历`Map`的方法至关重要。本文将详细介绍四种不同的遍历`Map`的方式,并通过具体的代码示例来...

    C++ STL --map and algorithm

    C++ STL 中的 map 和 algorithm 是两种非常重要的组件,map 是一种关联式容器,能帮助我们建立一一对应的关系,而 algorithm 则提供了一些常用的算法来处理数据。 Map 简介 map 是一种典型的关联式容器,它的特点...

    C++ Map的实例使用说明

    通过实例说明C++ map关联容器的使用,介绍了map中常用的成员函数的使用

    C++中vector可以作为map的键值实例代码

    该方法可以广泛应用于实际开发中,例如在项目中根据状态找到一个对应的结果时,可以使用map容器来存储状态和结果之间的映射关系,并且使用vector作为键值类型来存储复杂的状态信息。 本文为大家介绍了C++中vector...

    C++ api中文参考手册

    2. **STL泛型编程**:STL是C++中极其重要的一部分,它包括容器(如vector、list、set、map等)、迭代器、算法和函数对象。STL通过泛型编程实现了数据结构和算法的分离,极大地提高了代码的复用性和效率。手册会深入...

    C++编程惯用法——高级程序员常用方法和技巧

    在C++编程中,掌握高级程序员的常用方法和技巧是提升代码质量和效率的关键。本文将深入探讨一些核心的C++编程惯用法,帮助你更好地理解和应用这一强大的编程语言。 一、模板(Templates) 模板是C++中的一个强大...

    GridMap.zip

    在GridMap.zip的压缩包中,"GridMap"可能是源代码文件,包含实现上述功能的C++代码。通过学习和理解这个文件,开发者可以掌握如何在Qt环境下构建一个交互式的路径规划系统,包括地图的创建、路径搜索算法的实现以及...

    C++标准模板库map的常用操作

    a.map中实际数据的数据:map.size() b.map中最大数据的数量:map.max_size() c.判断容器是否为空:map.empty() 修改: a.插入数据:map.insert() b.清空map元素:map.clear() c.删除指定元素:map.erase(it) ...

    visual c++简单控制google map

    在IT行业中,Visual C++是一种常用的编程环境,用于开发Windows平台的应用程序。它结合了Microsoft的C++编译器和一套强大的开发工具,为开发者提供了高效且直观的开发体验。当我们谈论“visual c++简单控制google ...

    C++编程惯用法——高级程序员常用方法和技巧.rar

    在C++编程中,熟练掌握高级程序员常用的技巧和方法对于提升代码质量和效率至关重要。这份"高级程序员常用方法和技巧"的资料集包含了诸多关键知识点,旨在帮助开发者在实践中提升技能。以下是一些主要的C++高级编程...

    STL 中的常用的Vector Map Set Sort用法

    在C++编程中,STL(Standard Template Library,标准模板库)是一组高效、泛型的容器、迭代器、算法和函数对象,极大地简化了C++程序员的工作。本篇文章将详细探讨STL中的四个常用组件:`vector`、`map`、`set`以及...

    c++常用库函数速查手册(中文)

    本速查手册是针对C++中常用的库函数进行编译的中文版资源,旨在帮助开发者快速查询和回忆起那些不常使用的函数。 手册主要涵盖了C++标准库中的内容,包括但不限于: 1. **输入/输出流**:`std::cin`、`std::cout`...

    模板常用操作 和 map常用操作

    在IT行业中,模板和映射(map)是C++编程中的重要工具,广泛应用于软件开发。下面我们将深入探讨这两个主题,以及与它们相关的操作。 **模板(Template)**: 模板是C++的一个特性,允许我们编写泛型代码,即代码...

    C++map介绍及详细使用示例(源代码)

    ### C++ STL 中 `std::map` 和 `std::array` 的详细介绍及示例 #### 一、`std::map` 详解 `std::map` 是 C++ 标准模板库 (STL) 中的一个关联容器,它可以用来存储键值对。尽管描述中提到“包含可以重复的键值对”...

    C/C++中文帮助文档.chm

    STL包括容器(如vector、list、set、map等)、算法(如排序、查找)、迭代器、函数对象(functors)等模块,它们极大地提高了C++的效率和可读性。此外,文档还涉及了输入/输出流(I/O Streams),如iostream库,用于...

    实用C++的CHM集合

    1. **The C++ Standard Library.chm**:这个文件专注于C++标准库,它是C++语言的核心部分,提供了大量的容器、算法和工具,如vector、list、map、set、algorithm等。标准库还包含了异常处理、输入/输出流(iostream...

    每天学点C++(C++实例教程:教程+源码)map容器.zip

    在C++中,`map`容器是STL(标准模板库)的一部分,它提供了一种关联数组的数据结构,使得我们可以通过键(key)来查找对应的值(value)。本实例教程将深入讲解`map`容器的使用方法和源码实践。 1. **map容器的基本...

Global site tag (gtag.js) - Google Analytics