STL容器篇之map与tuple

简介: STL容器篇之map与tuple

map

map叫做映射,y = x, 所谓的映射就是一种对应的关系,数组是下标对应数据的一种关系

注意:

1.map存储的数据是数对类型:pair类型(包含,first,second)

2.有序性,按照first从小到大排列

3.唯一性:first唯一

单映射

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
  //包含2个数据类型,可以一样,也可以不一样
  map<int, string> date;
  pair<int, string>  tmp = {1, "nim"}; //pair其实也相当于一个模板
  cout << tmp.first << tmp.second << endl;
  date[1] = string("NI");
  date.insert(tmp);
  date[-100] = string("ni"); //这也跟数组下标不太一样,只要满足first类型,想怎么写就怎么写、
  date.insert(tmp);
  date.insert(pair<int, string>(1, "无名对象"));
  date.insert(make_pair<int, string>(1, "第三方函数"));
  date.insert(pair<int, string>(1, "重复运算"));
  for (auto& v : date)
  {
    cout << v.first << v.second << endl;  //新版for循环访问
  }
  for (map<int, string> ::iterator it = date.begin(); it != date.end(); it++)
  {
    cout << it->first << it->second << endl; //迭代器处理
  }
  system("pause");
  return 0;
}

多重映射

不支持下标法插入,存在二义性

有序性

同样map也有第三个参数,less和great分别表示从小到大,从大到小.

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
  multimap<int, string> sdate;
  sdate.insert(pair<int, string>(1, "ni"));
  for (auto& v : sdate)
  {
    cout << v.first << v.second;
  }
  //greater 从大到小
  multimap<string, string, greater<string>> mdate;
  mdate.insert(make_pair<string, string>("ni", "niini"));
  mdate.insert(pair<string, string>("m", "mmmmmmmmmmmmmm"));
  for (auto& v : mdate)
  {
    cout << v.first << v.second;
  }
  system("pasue");
  return 0;
}

操作自定义类型

比较的方法,还是跟之前一样,写一个仿函数

打印的方法:用重载的方法,或者用接口函数去访问

#include<iostream>
#include<map>
#include<string>
using namespace std;
class MM
{
public:
  MM(int age, string name) : age(age), name(name) {}
  friend ostream& operator << (ostream& out, const MM & object) //加const 修饰
  {
    out << object.age << object.name << endl;
    return out;
  }
private:
  int age;
  string name;
};
class mm
{
public:
  mm(){}
  mm(int math, int Chinses): math(math), Chinese(Chinses) {}
  friend ostream& operator << (ostream& out,const mm& object1)  //加const修饰
  {
    out << object1.math << object1.Chinese << endl;
    return out;
  }
private:
  int math;
  int Chinese;
};
class compare
{
public: 
  bool operator ()(const MM& object1, const MM& object2) const
  {
    return object1.getAge() < object2. getAge();
  }
};
int main()
{
  map<MM, mm, compare> date;
  date[MM(1, "ni")] = mm(10, 100);
  date[MM(3, "nininini")] = mm(78, 67);
  for (auto& v : date)
  {
    cout << v.first << v.second;
  }
  system("pause");
  return 0;
}

tuple

tuple叫做元组,知道如何使用即可,低层使用的折叠参数可变模板

创建对象和初始化

1.forward_as_tuple

2.make_tuple

3.直接对象(进行初始化)

#include<iostream>
#include<tuple>
#include<string>
using namespace std;
int main()
{
  tuple<int, string, int, string> date1 = {1, "2", 2, "nini"};
  tuple<int, string> mm = { 1, "2" };
  tuple<int, string> make_date(1, "ni");
  tuple<string, string> forward_as_tuple("ni", "ninin");
  system("pause");
  return 0;
}

访问

1.使用tie

2.使用get<>(), get<参数>,

注意:这个参数要跟常量,不能用变量,常数变量也不行

#include<iostream>
#include<tuple>
#include<string>
using namespace std;
int main()
{
  tuple<int, string, int> date = { 9, "ni", 9 };
  //用get<1>函数访问
  cout << get<0>(date) << get<1>(date) << get<2>(date) << endl;
  //tie访问
  int age;
  string name;
  int age1;
  tie(age, name, age1) = date;
  cout << age << name << age1;
  cout << endl;
  //忽略方式访问
  tie(age, ignore,ignore) = date;
  cout << age;
  system("pause");
  return 0;
}

tuple的其他操作

使用tuple_cat将2个tuple容器连接起来

#include<iostream>
#include<tuple>
#include<string>
using namespace std;
int main()
{
  tuple<int, string> date1 = { 1, "nu" };
  tuple<int, string> date2 = { 2, "ninini" };
  //tuple_cat
  auto date3 = tuple_cat(date1, date2); //将2个tuple来连接起来
  int number1;
  int number2;
  string name1;
  string name2;
  tie(number1, name1, number2, name2) = date3;
  cout << number1 << name1 << number2 << name2 << endl;
  system("pause");
  return 0;
}
相关文章
|
27天前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
2月前
|
存储 C++ 索引
|
2月前
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
45 0
|
2月前
|
存储 语音技术 Python
语音识别,函数综合案例,黑马ATM,/t/t一个对不齐,用两个/t,数据容器入门,数据容器可以分为列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)
语音识别,函数综合案例,黑马ATM,/t/t一个对不齐,用两个/t,数据容器入门,数据容器可以分为列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)
|
2月前
|
存储 C++ 索引
C++基础知识(八:STL标准库 Map和multimap )
C++ 标准模板库(STL)中的 map 容器是一种非常有用的关联容器,用于存储键值对(key-value pairs)。在 map 中,每个元素都由一个键和一个值组成,其中键是唯一的,而值则可以重复。
|
2月前
|
存储 算法 C语言
【C++】详解STL的适配器容器之一:优先级队列 priority_queue
【C++】详解STL的适配器容器之一:优先级队列 priority_queue
|
2月前
|
设计模式 存储 缓存
【C++】详解STL容器之一的deque和适配器stack,queue
【C++】详解STL容器之一的deque和适配器stack,queue
|
2月前
|
存储 算法 C++
【C++】详解STL容器之一的 vector
【C++】详解STL容器之一的 vector
|
2月前
|
算法 C语言 C++
【C++】详解STL的容器之一:list
【C++】详解STL的容器之一:list
|
14天前
|
Cloud Native 持续交付 Docker
云原生之旅:Docker容器化实战指南
【8月更文挑战第29天】本文将引领你进入云原生技术的世界,以Docker容器化为切入点,深入浅出地介绍如何利用Docker进行应用的打包、部署及管理。我们将通过实际代码示例,一步步展示Docker镜像的构建过程,以及如何运行和管理这些容器。无论你是初学者还是有一定经验的开发者,都能从中获得宝贵的知识和实操经验。