/************************************************************************/
/*
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;
}
分享到:
相关推荐
通过实例说明C++ map关联容器的使用,介绍了map中常用的成员函数的使用
C++的STL string vector list stack queue set map 等常用的容器使用
a.map中实际数据的数据:map.size() b.map中最大数据的数量:map.max_size() c.判断容器是否为空:map.empty() 修改: a.插入数据:map.insert() b.清空map元素:map.clear() c.删除指定元素:map.erase(it) ...
STL 中的常用的Vector Map Set Sort用法
map − unordered_map (C++11) priority_queue − span (C++20) 其他容器: 顺序 − 关联 无序关联 − 适配器 迭代器库 范围库 (C++20) 算法库 数值库 常用数学函数 特殊数学函数 (C++17) 数值算法 伪随机...
C++最常用函数库及部分封装库:包括vector,hash_set, hash_map, 文件读写,时间,字符串处理,压缩,正则表达式,redis,文件系统,日志,参数解析,base64,json处理等等,简单易用
一个比较好的C++的标准模板库.主要包含C++的标准模板库中的容器和算法的类库。主要包含STL中比较常用的容器是vector,set和map,比较常用的算法有Sort等。
10.2.9Map和multimap容器 118 10.2.10容器共性机制研究 123 10.2.11其他 124 10.3算法 125 10.3.1算法基础 125 10.3.2STL算法中函数对象和谓词 138 10.3.3常用的遍历算法 148 10.3.4常用的查找算法 152 10.3.5常用的...
C++标准库中包含了大量的头文件,用于提供各种功能和特性的声明。以下是一些常用 基本输入输出:iostream、cstdio、cstdarg 字符串处理:string、cstring 容器:vector、list、deque、set、map、unordered_set、...
更令人兴奋的是利用C++的封装功能使开发者摆脱Windows中各种句柄的困扰,只需要面对C++中的对象,这样一来使开发更接近开发语言而远离系统。(但我个人认为了解系统原理对开发很有帮助) 正因为MFC是建立在C++的...
c++中常用模板库头文件及其源代码,例如,,<stl_map.h>等
总是记不住C++容器的用法,各种操作函数,所以整理了这份文档,包含map、vector、set、string、格式化输入输出。一共30多页,都是一些常用的操作。
这个是一个用C++生成pgm地图的demo,在ros里的map_server可以正常加载该地图没有问题。如有任何问疑问请联系: 2942087962@qq.com
对C++中STL的常用数据结构进行介绍,如VECTOR,LINK,SET,MAP等,包括类型说明及使用方法介绍。
【STL源代码】中包含了许多常用的数据结构(如vector、list、map等)和算法(如排序、查找、遍历等)。通过阅读代码可以仔细研究这些数据结构和算法的实现,了解它们的内部工作原理和使用方式。
redis数据库支持string、键值对、map、多层map等数据的增删改查,还支持C++多层嵌套结构体的json序列化并写入redis数据库,非常方便 编译环境:eclipse 使用平台X86,乌班图下,当然,C++代码是通用的,也可以在...
C++中的容器类包括“顺序存储结构”和“关联存储结构”,前者包括vector,list,deque等;后者包括set,map,multiset,multimap等。 常用函数介绍等. 若需要存储的元素数在编译器间就可以确定,可以使用数组来...
本例程提供了C++的STL常用数据结构及其算法的使用范例,比如vector、string、list、forward_list、deque、queue、stack、map、set、multimap、multiset、tuple、bitset的使用范例,以及algorithm常….zip
STL中的常用容器包括:顺序性容器(vector、deque、list)、关联容器(map、set)、容器适配器(queue、stac)。 1、顺序性容器 (1)vectorvector是一种动态数组,在内存中具有连续的存储空间,支持快速随机访问。...