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;
}
相关文章
|
1天前
|
存储 安全 Java
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
|
5天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
11 1
|
11天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
13天前
|
C++ 容器
STL—map容器
STL—map容器
|
17天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
17天前
|
存储 算法 Java
盘点Java集合(容器)概览,Collection和Map在开发中谁用的最多?
盘点Java集合(容器)概览,Collection和Map在开发中谁用的最多?
30 0
|
25天前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
14 0
|
25天前
|
容器
06-python数据容器-tuple(元组)
06-python数据容器-tuple(元组)
|
1月前
|
编译器 容器
map映照容器
map映照容器 map映照容器
|
4月前
|
Python
Python元组tuple“删除”元素的两种函数代码设计
实际上,Python的tuple元组内的元素是不能被修改的,因此也是无法被删除的,但是,为了移除Python元组tuple内的某些元素,以获得一个新的元组,还是有其办法存在的。比如,我们可以使用for循环添加的方法,来创建一个不包含那些需要被移除的元素的新元组。Python中元组添加元素的内置方法为__add__()方法,实际上,该方法也是
51 4