Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何实施运营商<;在STL数据结构中使用任意结构?_C++ - Fatal编程技术网

C++ 如何实施运营商<;在STL数据结构中使用任意结构?

C++ 如何实施运营商<;在STL数据结构中使用任意结构?,c++,C++,有时我想在map和set中使用包含多个值的键。我不太在乎速度。是否有一种简单或通用的方法来编写运算符data2==other.data2) { if(this->data3==other.data3) { 返回false; } 其他的 { 返回此->数据3数据2数据1

有时我想在map和set中使用包含多个值的键。我不太在乎速度。是否有一种简单或通用的方法来编写运算符<以比较具有多个值的结构?我自己提出了以下几点,但很乏味,尤其是当值的数量增加时。谢谢

struct Properties
{
    Properties() {}

    Properties
        ( const string& data1
        , const string& data2
        , const string& data3
        )
        : data1(data1)
        , data2(data2)
        , data3(data3)
    {}

    string data1;
    string data2;
    string data3;

    bool operator < (const Properties& other) const
    {
        if (this->data1 == other.data1)
        {
            if (this->data2 == other.data2)
            {
                if (this->data3 == other.data3)
                {
                    return false;
                }
                else
                {
                    return this->data3 < other.data3;
                }
            }
            else
            {
                return this->data2 < other.data2;
            }
        }
        else
        {
            return this->data1 < other.data1;
        }
    }
};
struct属性
{
属性(){}
性质
(常量字符串和数据1)
、常量字符串和数据2
、常量字符串和数据3
)
:data1(data1)
,data2(data2)
,data3(data3)
{}
字符串数据1;
字符串数据2;
字符串数据3;
布尔运算符<(常量属性和其他)常量
{
if(this->data1==other.data1)
{
if(this->data2==other.data2)
{
if(this->data3==other.data3)
{
返回false;
}
其他的
{
返回此->数据3数据2数据1
您可以尝试将它们转换为元组,并进行比较。这将替换您的
属性
类。

您可以尝试将它们转换为元组,并对它们进行比较。这将取代您的
属性
类。

您可以使用
std::tie
实现以下目的:

#include <tuple>

bool operator<(Properties S& rhs) const
{
  return std::tie(data1, data2, data3) < std::tie(rhs.data1, rhs.data2, rhs.data3);
}
#包括

bool操作符您可以使用
std::tie
进行此操作:

#include <tuple>

bool operator<(Properties S& rhs) const
{
  return std::tie(data1, data2, data3) < std::tie(rhs.data1, rhs.data2, rhs.data3);
}
#包括

bool操作符它确实变得相当乏味

当然,如果将数据存储在一个数组中[假设所有数据的类型相同],则可以使用循环:

const int numstrings = 3;
string data[3];

...

bool operator < (const Properties& other) const
{
   for(int i = 0; i < 3; i++)
   {
      if (data[i] != other.data[i])
      {
          return data[i] < other.data[i]; 
      }
   }
 }
const int numstrings=3;
字符串数据[3];
...
布尔运算符<(常量属性和其他)常量
{
对于(int i=0;i<3;i++)
{
if(数据[i]!=其他.data[i])
{
返回数据[i]
当然,您也可以稍微缩短现有代码:

bool operator < (const Properties& other) const
{
    if (this->data1 != other.data1)
    {
        return this->data1 < other.data1;
    }
    if (this->data2 != other.data2)
    {
        return this->data2 < other.data2;
    }
    return this->data3 < other.data3;
}
bool运算符<(常量属性和其他)常量
{
if(this->data1!=other.data1)
{
返回此->数据1data2!=other.data2)
{
返回此->数据2数据3
它确实变得相当乏味

当然,如果将数据存储在一个数组中[假设所有数据的类型相同],则可以使用循环:

const int numstrings = 3;
string data[3];

...

bool operator < (const Properties& other) const
{
   for(int i = 0; i < 3; i++)
   {
      if (data[i] != other.data[i])
      {
          return data[i] < other.data[i]; 
      }
   }
 }
const int numstrings=3;
字符串数据[3];
...
布尔运算符<(常量属性和其他)常量
{
对于(int i=0;i<3;i++)
{
if(数据[i]!=其他.data[i])
{
返回数据[i]
当然,您也可以稍微缩短现有代码:

bool operator < (const Properties& other) const
{
    if (this->data1 != other.data1)
    {
        return this->data1 < other.data1;
    }
    if (this->data2 != other.data2)
    {
        return this->data2 < other.data2;
    }
    return this->data3 < other.data3;
}
bool运算符<(常量属性和其他)常量
{
if(this->data1!=other.data1)
{
返回此->数据1data2!=other.data2)
{
返回此->数据2数据3
您可以这样做:

bool operator < (const Properties& other) const
{
    if (this->data1 < other.data1)
        return true;
    if (other.data1 < this->data1)
        return false;

    if (this->data2 < other.data2)
        return true;
    if (other.data2 < this->data2)
        return false;

    return this->data3 < other.data3;
}
bool运算符<(常量属性和其他)常量
{
如果(本->数据1data1)
返回false;
如果(此->数据2data2)
返回false;
返回此->数据3
您可以这样做:

bool operator < (const Properties& other) const
{
    if (this->data1 < other.data1)
        return true;
    if (other.data1 < this->data1)
        return false;

    if (this->data2 < other.data2)
        return true;
    if (other.data2 < this->data2)
        return false;

    return this->data3 < other.data3;
}
bool运算符<(常量属性和其他)常量
{
如果(本->数据1data1)
返回false;
如果(此->数据2data2)
返回false;
返回此->数据3
执行此操作的传统方法如下

bool operator<(Properties const& lhs, Properties const& rhs) const
{
    return (lhs.data1 < rhs.data1)
           || (!(rhs.data1 < lhs.data1) && lhs.data2 < rhs.data2)
           || (!(rhs.data1 < lhs.data1) && !(rhs.data2 < lhs.data2) && lhs.data3 < rhs.data3;
}

bool操作符执行此操作的传统方法如下

bool operator<(Properties const& lhs, Properties const& rhs) const
{
    return (lhs.data1 < rhs.data1)
           || (!(rhs.data1 < lhs.data1) && lhs.data2 < rhs.data2)
           || (!(rhs.data1 < lhs.data1) && !(rhs.data2 < lhs.data2) && lhs.data3 < rhs.data3;
}

bool操作符的缺点是不工作。例如,
{3,3,1}
{2,2,2}
要小,这可能不是OP想要的。但它的缺点是不能工作。例如,
{3,3,1}
将比
{2,2,2}
要小,这可能不是OP想要的。