#include

时间:2021-10-08 10:20:33

//tuple多元数组,必须是静态数组,类似结构体

//配合array,vector使用

//std::tuple<数组元素类型>数组变量名(数组元素变量名);

 #include <iostream>
#include <map>
using namespace std; //tuple多元数组,必须是静态数组,类似结构体
//配合array,vector使用 void main()
{
int int1();
double double1(99.8);
char ch1('A');
char *str = "hello"; //std::tuple<数组元素类型>数组变量名(数组元素变量名);
std::tuple<int, double, char, const char *>mytuple(int1, double1, ch1, str);//创建一个多元数组,可以存放不同类型的元素 auto data0 = std::get<>(mytuple);//下标只能是常量
auto data1 = std::get<>(mytuple);
auto data2 = std::get<>(mytuple);
auto data3 = std::get<>(mytuple); std::cout << typeid(data0).name() << " " << data0 << std::endl;//在C++中,typeid用于返回指针或引用所指对象的实际类型。
std::cout << typeid(data1).name() << " " << data1 << std::endl;
std::cout << typeid(data2).name() << " " << data2 << std::endl;
std::cout << typeid(data3).name() << " " << data3 << std::endl; decltype(data0) dataA;//根据一个变量,创建一个备份 system("pause");
}

map, multimap的区别:map每一个结点是映射,multimap每一个结点是映射链表的开头

1 map

2 multimap

1 map

映射(map) 由{键,值}对组成的集合,以某种作用于键对上的谓词排列 <map>

如果已经有重复的元素,将会插入失败

 #include <iostream>
#include <map> int main()
{
std::map<const char *, int>mymap; mymap.insert(std::pair<const char *, int>("司令1", ));
mymap.insert(std::pair<const char *, int>("司令2", ));
mymap.insert(std::pair<const char *, int>("司令3", ));
mymap.insert(std::pair<const char *, int>("司令4", )); mymap.insert(std::pair<const char *, int>("司令1", ));//如果已经有重复的元素,将会插入失败 auto ib = mymap.begin();
auto ie = mymap.end(); for (; ib != ie; ib++)
{
std::cout << ib->first << " " << ib->second << std::endl;
} return ;
}

映射

 #include <iostream>
#include <map> struct student
{
char *name;
int year;
}; struct stuinfo
{
int id;
student stu;
}; int main()
{
stuinfo infoarray[] = { {,{"hello",}},{ ,{ "hello", }},{,{ "hello", }} }; std::map<int, student>m; for (int i = ; i < ; i++)
{
m[infoarray[i].id] = infoarray[i].stu;
} auto ib = m.begin();
auto ie = m.end(); for (; ib != ie; ib++)//遍历
{
std::cout << (*ib).first << " ";
std::cout << (*ib).second.name << " " << (*ib).second.year << std::endl;
} return ;
}

2 multimap

 #include <iostream>
#include <map> int main()
{
std::multimap<const char *, int>mymap; mymap.insert(std::pair<const char *, int>("司令1", ));
mymap.insert(std::pair<const char *, int>("司令2", ));
mymap.insert(std::pair<const char *, int>("司令3", ));
mymap.insert(std::pair<const char *, int>("司令4", )); mymap.insert(std::pair<const char *, int>("司令1", ));//multimap如果有重复,也会插入成功 auto ib = mymap.begin();
auto ie = mymap.end(); for (; ib != ie; ib++)
{
std::cout << ib->first << " " << ib->second << std::endl;
} return ;
}

multimap的元素可以重复,因此equal_range(const T&);可以实现查找多个相同的元素

 #include <iostream>
#include <map>
#include <string> void main()
{
std::multimap<std::string, std::string>mymap; mymap.insert(std::pair<std::string, std::string>("hello", "a"));
mymap.insert(std::pair<std::string, std::string>("world", "b"));
mymap.insert(std::pair<std::string, std::string>("hello", "c"));
mymap.insert(std::pair<std::string, std::string>("world", "d")); auto ib = mymap.begin();
auto ie = mymap.end(); for (; ib != ie; ib++)
{
std::cout << ib->first << " " << ib->second << std::endl;
}
std::cout << std::endl; auto pfind = mymap.find("hello");//查找
std::cout << pfind->first << " " << pfind->second << std::endl << std::endl;//只输出查找的第一个元素 auto it = mymap.equal_range("hello");
for (auto i = it.first; i != it.second; i++)
{
std::cout << i->first << " " << i->second << std::endl;
}
}

#include <map>的更多相关文章

  1. C&plus;&plus; std&colon;&colon;map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  2. C&plus;&plus; map的基本操作和使用

    原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...

  3. c&plus;&plus; map 使用

    . 包含头文件: #include <map> 2. 构造函数: std::map<char,int> first; first[; first[; first[; first ...

  4. 字符串作为map的key

    #include <map> #include <string> struct cmp_str{ bool operator()(char const* a, char con ...

  5. map&lt&semi;虽然一直不喜欢map&gt&semi;但突然觉得挺好用的

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...

  6. STL模板中的map的使用与例题

    最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...

  7. C&plus;&plus;11 新特性: unordered&lowbar;map 与 map 的对比

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...

  8. HDU 5167&lpar;map &plus; 暴力&rpar;

    题意:给出一个数n,问n能否是斐波那契数列中数的乘积 先刷选 斐波那契数列,然后就枚举 #include <cstdio> #include <cstring> #includ ...

  9. 字符串专题:map POJ 1002

    第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...

随机推荐

  1. wxPython学习笔记&lpar;三&rpar;

    要理解事件,我们需要知道哪些术语? 事件(event):在你的应用程序期间发生的事情,它要求有一个响应. 事件对象(event object):在wxPython中,它具体代表一个事件,其中包括了事件 ...

  2. mysql安装过程

      1.到官网下载Mysql,目前最新版都是5.0以上版本,下载之后直接解压即可   2.在终端进入bin目录(如果嫌麻烦可配置环境变量,配置之后则无需进入bin目录则可敲命令),安装数据库服务:my ...

  3. 【webpack学习笔记】a07-代码分离

    官方文档说进行代码分离有三种方法: 入口起点:使用entry配置手动分离. 防止重复:使用CommonsChunkPlugin插件去重合分离chunk 注:在webpack4中,CommonsChun ...

  4. 【数据表格】datatable&plus;SpringMVC&plus;Spring Data JPA

    初步实现 $("#userTable").dataTable({ "processing": true, "serverSide": tru ...

  5. Java大数相加(多个大数相加)-hdu1250

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 题目描述: 题目大意是:已知一个Hat's Fibonacci序列,该序列满足F(1) = 1, ...

  6. JavaScript大杂烩3 - 理解JavaScript对象的封装性

    JavaScript是面向对象的 JavaScript是一种基于对象的语言,你遇到的所有东西,包括字符串,数字,数组,函数等等,都是对象. 面向过程还是面向对象? JavaScript同时兼有的面向过 ...

  7. JavaScript学习第一天(一)

    JavaScript介绍 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...

  8. Android Camera详解

    相关的类 android.hardware.camera2 Camera SurfaceView---这个类用于向用户呈现实时相机预览. MediaRecorder---这个类用于从摄像机录制视频. ...

  9. Listbox Binding ItemsSource

    把List<CourseItem>绑定到ListBox. 前台绑定: <ListBox x:Name="ItemBox" Grid.Row="1&quo ...

  10. 无法安装64位版本的office因为在您的pc

    无法安装64位版本的office因为在您的pcWindows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Installer\Products\00 ...