用map构建映射关系可以用来离散化,这里记录一下用法;

插入:将要插入的两个值组成pair,用insert插入map中;

查找:定义iterator,赋成find返回的迭代器,如果没有找到则=mp.end(),否则可以用->指向要取出的值,此时得到的就是定义类型了;

其实就是一些语法,见代码即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<int,int>mp;
int main()
{
    mp.insert(pair<int,int>(30,1));
    mp.insert(pair<int,int>(20,2));
    map<int, int>::iterator it_find;
    it_find = mp.find(20);
    if (it_find != mp.end())printf("fail");
    else printf("%d",it_find->second);
    return 0;
}

 

相关文章:

  • 2022-01-13
  • 2021-11-18
  • 2021-11-04
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2021-11-18
  • 2021-08-01
  • 2021-06-02
相关资源
相似解决方案