/*
map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,
所以在map内部所有的数据都是有序的。
*/
#include <iostream>
#include <map>
using namespace std;
typedef map<int,string> mapStudent;
typedef map<int,string>::iterator mapStudengIter;
typedef pair<int,string> Param;
int main()
{
cout<<"========================"<<endl;
mapStudent mapstudent;
mapStudengIter iter,finditer;
mapstudent[4] = "student_four";
mapstudent.insert(Param(1,"Tongjm"));
mapstudent.insert(Param(2,"Zhaoxp"));
mapstudent.insert(map<int, string>::value_type (3,"student_three"));
mapstudent[10] = "student_ten";
mapstudent[10] = "student_ele";
/*
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,
用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,
insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
*/
for(iter = mapstudent.begin();iter!=mapstudent.end();iter++ )
{
cout<< iter->first<<"--->"<<iter->second<<endl;
}
cout<<mapstudent.size()<<endl;
cout<<mapstudent[10]<<endl;
/*
用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,
如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明
*/
finditer = mapstudent.find(2);
if(finditer!=mapstudent.end())
{
cout<< finditer->first<<"--->"<<finditer->second<<endl;
}
/*
数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
*/
if((!mapstudent.empty())== true)
cout<<"非空"<<endl;
int n = mapstudent.erase(9);
for(iter = mapstudent.begin();iter!=mapstudent.end();iter++ )
{
cout<< iter->first<<"--->"<<iter->second<<endl;
}
//功能等同于 mapstudent.clear()
mapstudent.erase(mapstudent.begin(),mapstudent.end());
//mapstudent.clear();
if(mapstudent.empty()== true)
cout<<"空"<<endl;
system("pause");
}
分享到:
相关推荐
主要介绍了c++中map的基本用法和嵌套用法,以实例形式分析了map容器的基本使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
C++ Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序中建立一个map 可以起到事半功倍的效果,总结了一些map基本简单实用的操作!
c++中map基本用法和操作的小例子,关于存储和查找的例子。
C++ Map常见用法说明1
stl中map用法和他的大体结构c++ map详解
map 算法的实现和操作,对处理大量的衍射数据,一一对应的关系
C++ map用法总结(整理)。 一篇文章带你快速了解!
详尽的介绍了Map容器的基本用法,方便初学者掌握Map容器!
STL中map用法详解 非常详细,有说明和可以编译的例子
* 2、支持快速查找,查找的复杂度基本是Log(N) * 3、快速插入,快速删除,快速修改记 * /************************************************************************/ #include #include #include <map> using ...
C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。
其中包含了标准C++的基本语句、类、vector、map等操作的用法。这些都是本人积累的结果,大约有1000行左右,各种用法之间没有关联性,使用者可以在此基础上进行扩充,增加自己的积累。
c++中标准STL容器MAP的用法总结,对C++学习者有用.
C++的STL string vector list stack queue set map 等常用的容器使用
map容器的用法,清晰,看了就懂了,需要的可以下载
C++中STL中的map用法详解.doc
实现了传入string格式的js到jerryscript中执行,实现了多线程的互斥锁,保证多线程访问的安全,并且js方法中使用多个c++map格式的参数为入参,返回值为bool.方法定义如下: static bool runJsEngine(const map, string> ...
C++中STL中的map用法详解,适合初学者彻底高清map用法,开发者备用查询map使用,已经map与其他容器的比较,map的使用特点和技巧