首页 > 其他分享 >map

map

时间:2023-03-21 12:46:25浏览次数:32  
标签:map mapStudent insert student pair include

1、map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值。

2、map内部所有的数据都是有序的

3、map的插入最常见是数组方式。

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string> mapStudent;
       mapStudent[1] =  "student_one";
       mapStudent[2] =  "student_two";
       mapStudent[3] =  "student_three";
       map<int, string>::iterator  it;
       for(it = mapStudent.begin(); it != mapStudent.end(); it++)
       {
          cout<<it->first<<"   "<<it->second<<endl;
       }
}

4、map的查找是按照关键字查找的,相当于是索引。------通过索引找到你想找的值。第一个存索引,第二个存值。

5、用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。

find(里面写的是索引----关键字),返回的是一个迭代器(类似指针)。

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       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"));
       map<int, string>::iterator iter;
       iter = mapStudent.find(1);
       if(iter != mapStudent.end())
      {
           cout<<"Find, the value is "<<iter->second<<endl;
       }
       else
       {
           cout<<"Do not Find"<<endl;
       }
}

6、map#include <map>

#include <string>
#include <iostream>
using namespace std;
int main()
{
       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"));
       map<int, string>::iterator it;
       it = mapStudent.find(1);
       mapStudent.erase(it);
       //如果要删除1,用关键字删除
       int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
      
//用迭代器,成片的删除 ,前闭后开。 mapStudent.earse(mapStudent.begin(), mapStudent.end()); }

 

标签:map,mapStudent,insert,student,pair,include
From: https://www.cnblogs.com/bhd123/p/17239577.html

相关文章

  • mybatis-spring注解MapperScan的原理
    很多开发者应该都知道,我们只使用@MapperScan这个注解就可以把我们写的Mybatis的Mapper接口加载到Spring的容器中,不需要对每个Mapper接口加@Mapper这个注解了,加快了我们开发......
  • Treemap按key和value降序排序
    Treemap是一种根据键排序的数据结构,可以通过重载它的比较器来按照值排序。要按键排序,可以使用默认的比较器,而要按值排序,可以创建一个自定义的比较器并将其传递给treemap的......
  • Mybatis 源码(六):Mapper接口代理对象创建
    Mapper接口通过sqlSession的getMapper()方法获取,接口无法创建实例对象,获取到的是其代理对象,下面来看看Mapper接口的代理是如何创建的。UserMappermapper=sqlSess......
  • hashmap,hashtabl,hashtree,linkedhashmap区别分析
    java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMapHashtableLinkedHashMap和TreeMap.Map主要用于存储健值对,根据......
  • org.hibernate.MappingException: could not instantiate id generator
    org.hibernate.MappingException:couldnotinstantiateidgeneratorHibernate的版本不对,也解决无法创建org.hibernate.id.UUIDGenerator的问题@......
  • Mybatis 源码(四):Mapper的解析工作
    1、Mapper配置方式1、package方式指定包路径:<mappers><packagename="org.snails.mapper"/></mappers>2、resource方式指定mapper.xml文件的相对路径:<map......
  • 多线程面试——CountDownLatch,CyclicBarrier,Semaphore
    0.总结1.CountDownLatch是1个线程等待其他线程,CyclicBarrier是多个线程相互等待;2.CountDownLatch是计数-1,直到减为0,CyclicBarrier是计数+1,直到达到指定值;3.CountDownLatch......
  • java常用的工具类之Collectors.toMap
    java.util.stream.Collectors一.Collectors.toMap1.构造ListList<Student>list=newArrayList<>();for(inti=1;i<4;i++){list.add(newStudent(i+"","......
  • Java HashMap为什么线程不安全
    一、学习目标1、HashMap线程不安全原因:原因:JDK1.7中,由于多线程对HashMap进行扩容,调用了HashMap#transfer(),具体原因:某个线程执行过程中,被挂起,其他线程已经完成数据迁......
  • resources目录下的mapper写sql语句没有提示
    resources目录下的mapper写sql语句没有提示首先了解一下mybatix-config.xml连接mapper文件的三种方式:<!--第一种--><mapperresource="com/bbl/dao/UserMa......