本文主要是介绍(P28)map:map介绍 ,插入数据 ,查找与修改 ,删除,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1.map介绍
- 2.插入数据
- 3.查找与修改
- 4.删除
1.map介绍
- 使用map得包含map类所在的头文件
#include <map>
- map实际上是一种关联性容器,基于<key,value>来存储的,是哈希表的变种,内部数据结构是红黑树(一种特殊的二叉树)
- 定义一个map对象:
map<string, int>可以看作是一个模板类,实例化一个模板类对象mapTestmap<string, int> mapTest;//用string作为索引,存储int对象
2.插入数据
- 插入数据的4种方法
mapTest["aaa"] = 100;//用下标的方式插入mapTest.insert(map<string, int>::value_type("bbb",200));mapTest.insert(pair<string,int>("ddd", 400));mapTest.insert(make_pair<string, int>("ccc", 300));
- eg:P28\01.cpp
#include <iostream>
#include <map>
using namespace std;int main(void)
{//key类型是string,有一定的限制//value值类型是int,无限制//因为插入到map容器内部的元素默认是按照key从小到大来排序//所以key类型一定要重载<运算符,这样才可以进行比较和排序map<string,int> mapTest;//仅仅是对value来讲//内部重载了[]运算符,效率低一些,但是直观:因为经历2步:首先插入key=“aaa”的元素,初始值等于int类型的默认值,为0//第二步,再把100赋值给它//也就是说,先构造一个元素,其值是默认值,若是类的话,就使用类的构造函数来构造一个对象,放到map容器内部,然后再把//该对象赋值给它(上面的对象)mapTest["aaa"] = 100;//int& operator[](const string& index)mapTest["eee"] = 500;mapTest["eee"] = 300;//可以更新原来eee的值//构造了一个value_type对象//(map<string, int>::value_type这是一个类类型,第一个参数是key,第二个参数是valuemapTest.insert(map<string, int>::value_type("bbb", 200));mapTest.insert(map<string, int>::value_type("bbb", 2000));//不可以更新原来bbb的值//构造一个pair对象,pair是一个类类型mapTest.insert(pair<string,int>("ccc", 300));mapTest.insert(pair<string,int>("ccc", 3000));//不可以更新原来ccc的值//make_pair是一个函数,它返回的就是pair类型对象,与上面的等价mapTest.insert(make_pair("ddd",400));mapTest.insert(make_pair("ddd",4000));//不可以更新原来ddd的值//遍历map容器,也使用迭代器map<string, int>::const_iterator it;for(it=mapTest.begin(); it!=mapTest.end(); ++it) {//it->first就是string,it->second就是int//输出会自动按照key来排序cout<it->first<<" "<<it->second<<endl;}return 0;}
- 测试:
3.查找与修改
- 2种方法
//方法1,若bbb的值为0,或者不存在bbb的值,其n返回值都为0,不知道能不能找到int n = mapTest["bbb"];cout<<n<<endl;mapTest["bbb"] = 2000;//方法2:与方法1相比,可以判断是否找到 map<string, int>::iterator it;it = mapTest.find("ccc");if(it != mapTest.end()){//将找到的ccc的值修改为3000,若是const_iterator,则不能赋值,因为是常量it->second = 3000;}else{cout<<"not found"<<endl;}
- eg:P28\02.cpp
#include <iostream>
#include <map>
using namespace std;int main(void)
{map<string,int> mapTest;mapTest["aaa"] = 100;//int& operator[](const string& index)mapTest.insert(map<string, int>::value_type("bbb", 200));mapTest.insert(pair<string,int>("ccc", 300));mapTest.insert(make_pair("ddd",400));//查找key=“bbb”的值//方法1,若bbb的值为0,或者不存在bbb的值,其n返回值都为0,不知道能不能找到int n = mapTest["bbb"];cout<<n<<endl;mapTest["bbb"] = 2000;//方法2:与方法1相比,可以判断是否找到 map<string, int>::iterator it;it = mapTest.find("ccc");if(it != mapTest.end()){//将找到的ccc的值修改为3000,若是const_iterator,则不能赋值,因为是常量it->second = 3000;}else{cout<<"not found"<<endl;}//遍历map容器,也使用迭代器// map<string, int>::const_iterator it;for(it=mapTest.begin(); it!=mapTest.end(); ++it) {cout<it->first<<" "<<it->second<<endl;}return 0;}
- 测试:
4.删除
- 2种方法
//erase可以通过key,也可以通过迭代器//方法1mapTest.erase("bbb");//方法2//遍历map容器,也使用迭代器map<string, int>::const_iterator it;it = mapTest.find("ccc");if (it != mapTest.end()){mapTest.erase(it);}
eg:P28\03.cpp
#include <iostream>
#include <map>
using namespace std;int main(void)
{map<string,int> mapTest;mapTest["aaa"] = 100;//int& operator[](const string& index)mapTest.insert(map<string, int>::value_type("bbb", 200));mapTest.insert(pair<string,int>("ccc", 300));mapTest.insert(make_pair("ddd",400));//erase可以通过key,也可以通过迭代器//方法1mapTest.erase("bbb");//方法2//遍历map容器,也使用迭代器map<string, int>::const_iterator it;it = mapTest.find("ccc");if (it != mapTest.end()){mapTest.erase(it);}//也可以在遍历的时候删除,要注意++it的使用,不懂的话参看P27\02.cppfor(it=mapTest.begin(); it!=mapTest.end(); ++it) {cout<it->first<<" "<<it->second<<endl;}return 0;}
- 测试:
这篇关于(P28)map:map介绍 ,插入数据 ,查找与修改 ,删除的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!