C++中自定义结构体或类作为关联容器的键

简介: C++中自定义结构体或类作为关联容器的键

C++中自定义结构体或类作为关联容器的键

目录

1. 概述

STL中像set和map这样的容器是通过红黑树来实现的,插入到容器中的对象是顺序存放的,采用这样的方式是非常便于查找的,查找效率能够达到O(log n)。所以如果有查找数据的需求,可以采用set或者map。

但是我们自定义的结构体或者类,无法对其比较大小,在放入到容器中的时候,就无法正常编译通过,这是set/map容器的规范决定的。要将自定义的结构体或者类存入到set/map容器,就需要定义一个排序的规则,使其可以比较大小。最简单的办法就是在结构体或者类中加入一个重载小于号的成员函数,这样在存数据进入set/map中时,就可以根据其规则排序。

2. 实例

在这里就写了一个简单的例子,将自定义的一个二维点存入set/map,并查找其中存入的数据:

#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
const double EPSILON = 0.000001;
// 2D Point
struct Vector2d
{
public:
  Vector2d()
  {
  }
  Vector2d(double dx, double dy)
  {
    x = dx;
    y = dy;
  }
  // 矢量赋值
  void set(double dx, double dy)
  {
    x = dx;
    y = dy;
  }
  // 矢量相加
  Vector2d operator + (const Vector2d& v) const
  {
    return Vector2d(x + v.x, y + v.y);
  }
  // 矢量相减
  Vector2d operator - (const Vector2d& v) const
  {
    return Vector2d(x - v.x, y - v.y);
  }
  //矢量数乘
  Vector2d Scalar(double c) const
  {
    return Vector2d(c*x, c*y);
  }
  // 矢量点积
  double Dot(const Vector2d& v) const
  {
    return x * v.x + y * v.y;
  }
  //向量的模
  double Mod() const
  {
    return sqrt(x * x + y * y);
  }
  bool Equel(const Vector2d& v) const
  {
    if (abs(x - v.x) < EPSILON && abs(y - v.y) < EPSILON)
    {
      return true;
    }
    return false;
  }
  bool operator == (const Vector2d& v) const
  {
    if (abs(x - v.x) < EPSILON && abs(y - v.y) < EPSILON)
    {
      return true;
    }
    return false;
  }
  bool operator < (const Vector2d& v) const
  {   
    if (abs(x - v.x) < EPSILON)
    {
      return y < v.y ? true : false;
    }
    return x<v.x ? true : false;
  }
  double x, y;
};
int main()
{ 
  {
    set<Vector2d> pointSet; 
    pointSet.insert(Vector2d(0, 11));
    pointSet.insert(Vector2d(27, 63));
    pointSet.insert(Vector2d(27, 15));
    pointSet.insert(Vector2d(0, 0));
    pointSet.insert(Vector2d(67, 84));
    pointSet.insert(Vector2d(52, 63));
    for (const auto &it : pointSet)
    {
      cout << it.x << '\t' << it.y << endl;
    }
    auto iter = pointSet.find(Vector2d(27, 63));
    if (iter == pointSet.end())
    {
      cout << "未找到点" << endl;
    }
    else
    {
      cout << "可以找到点" << endl;
    }
  } 
  {
    map<Vector2d, string> pointSet;
    pointSet.insert(make_pair(Vector2d(52, 63), "插入时的第1个点"));
    pointSet.insert(make_pair(Vector2d(27, 63), "插入时的第2个点"));
    pointSet.insert(make_pair(Vector2d(0, 11), "插入时的第3个点"));    
    pointSet.insert(make_pair(Vector2d(67, 84), "插入时的第4个点"));
    pointSet.insert(make_pair(Vector2d(27, 15), "插入时的第5个点"));
    pointSet.insert(make_pair(Vector2d(0, 0), "插入时的第6个点"));
  
    for (const auto &it : pointSet)
    {
      cout << it.first.x << ',' << it.first.y << '\t' << it.second << endl;
    }
    auto iter = pointSet.find(Vector2d(27, 63));
    if (iter == pointSet.end())
    {
      cout << "未找到点" << endl;
    }
    else
    {
      cout << "可以找到点" << endl;
    }
  }
}

其中的关键就是在点的结构体中重载了<符号的比较函数,规定首先比较y的大小,其次在比较x的大小:

bool operator < (const Vector2d& v) const
{   
    if (abs(x - v.x) < EPSILON)
    {
        return y < v.y ? true : false;
    }
    return x<v.x ? true : false;
}

最终的运行结果如下:

分类: C++

标签: STL , C++


相关文章
|
5天前
|
存储 编译器 对象存储
【C++打怪之路Lv5】-- 类和对象(下)
【C++打怪之路Lv5】-- 类和对象(下)
15 4
|
5天前
|
编译器 C语言 C++
【C++打怪之路Lv4】-- 类和对象(中)
【C++打怪之路Lv4】-- 类和对象(中)
14 4
|
4天前
|
存储 安全 C++
【C++打怪之路Lv8】-- string类
【C++打怪之路Lv8】-- string类
10 1
|
6天前
|
存储 编译器 C语言
【C++打怪之路Lv3】-- 类和对象(上)
【C++打怪之路Lv3】-- 类和对象(上)
13 0
|
10天前
|
存储 编译器 C语言
深入计算机语言之C++:类与对象(上)
深入计算机语言之C++:类与对象(上)
|
14天前
|
存储 编译器 C++
【C++类和对象(下)】——我与C++的不解之缘(五)
【C++类和对象(下)】——我与C++的不解之缘(五)
|
14天前
|
编译器 C++
【C++类和对象(中)】—— 我与C++的不解之缘(四)
【C++类和对象(中)】—— 我与C++的不解之缘(四)
|
17天前
|
编译器 C语言 C++
C++入门3——类与对象2-2(类的6个默认成员函数)
C++入门3——类与对象2-2(类的6个默认成员函数)
22 3
|
17天前
|
存储 编译器 C语言
C++入门2——类与对象1(类的定义和this指针)
C++入门2——类与对象1(类的定义和this指针)
20 2
|
16天前
|
C++
C++番外篇——对于继承中子类与父类对象同时定义其析构顺序的探究
C++番外篇——对于继承中子类与父类对象同时定义其析构顺序的探究
49 1