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;
}
相关文章
|
7月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
191 2
|
7月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
369 73
|
8月前
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
194 2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
195 5
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
199 2
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
185 0
|
存储 索引 Python
元组(Tuple)在Python编程中的应用与实例
元组(Tuple)在Python编程中的应用与实例
508 2
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
230 1

热门文章

最新文章

下一篇
oss教程