Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++ C++;结果是;无效运算符<&引用;_C++_Sorting_Operators_Operator Overloading_Overloading - Fatal编程技术网

C++ C++;结果是;无效运算符<&引用;

C++ C++;结果是;无效运算符<&引用;,c++,sorting,operators,operator-overloading,overloading,C++,Sorting,Operators,Operator Overloading,Overloading,当前正在尝试对对象的向量进行排序,每个对象包含一个字符串,使用C++ 字符串可以包含字母或数字(由于设计限制,这是必要的,因为比较器可以更改) 目前,对象的类是重载的,因此在比较两个对象时,会比较它们包含的字符串。这在某种程度上是有效的——但是,当我使用排序操作(如STL排序)对对象进行排序时,它将按“1”、“12”、“4”的顺序对三个字符串进行排序,如“1”、“4”、“12”。4大于12,但由于它从最左边的数字开始比较,因此会出现这种“不正确”排序 我最初的反应是改变我重载比较操作的方式。我会

当前正在尝试对对象的向量进行排序,每个对象包含一个字符串,使用C++

字符串可以包含字母或数字(由于设计限制,这是必要的,因为比较器可以更改)

目前,对象的类是重载的,因此在比较两个对象时,会比较它们包含的字符串。这在某种程度上是有效的——但是,当我使用排序操作(如STL排序)对对象进行排序时,它将按“1”、“12”、“4”的顺序对三个字符串进行排序,如“1”、“4”、“12”。4大于12,但由于它从最左边的数字开始比较,因此会出现这种“不正确”排序

我最初的反应是改变我重载比较操作的方式。我会首先检查我正在比较的字符串的长度——如果字符串的内容较大或较小,这将是一个信号

// overloaded comparision operators
friend bool operator<(const nodeRecord & record1, const nodeRecord & record2){
    // we need to deal with strings of different lengths...
    if(record1.comparator.length() < record2.comparator.length())
        return true;
    else
        return (record1.comparator < record2.comparator);
}
//重载的比较运算符
friend bool Operator连接成本=连接成本;
//将比较器设置为我们选择的比较项
如果(!compareByCost){
this->comparator=this->fromNode;//在本例中我们使用from节点,因为我们向外构建树
}
否则{
细流ss;
ss连接成本;
this->comparator=ss.str();//在本例中,我们使用连接成本来对新连接进行排序
}
//将此设置为非空(活动)记录
此->nullRecord=false;
}

为什么不使用一个比较器,使该函数更智能一些?让它在开头检查数字字符,如果是,则执行一对
strtol()
atoi()
并比较结果


否则,请根据非数字要求比较字符串长度和字符。

找到引发错误的以下代码段,然后思考重载操作的工作方式

template<class _Ty1, class _Ty2> inline
    bool _Debug_lt(_Ty1& _Left, _Ty2& _Right,
        _Dbfile_t _File, _Dbline_t _Line)
    {   // test if _Left < _Right and operator< is strict weak ordering
    if (!(_Left < _Right))
        return (false);
    else if (_Right < _Left)
        _DEBUG_ERROR2("invalid operator<", _File, _Line);
    return (true);
    }
模板内联
bool(左),右(右),,
_Dbfile\u t\u File,\u Dbline\u t\u Line)
{//测试_Left<_rightand operator<是否为严格弱序
如果(!(左<右))
返回(假);
else if(右<左)

_调试错误2(“无效运算符您的运算符实际上无效

如果希望运算符
!(y

让我们定义
x=“b”
y=“aa”

  • x
    因为
    的长度“b”
    小于
    的长度“aa”
  • y
    因为
    “aa”
    不如
    “b”

还要注意的是,如果数字的前缀是
0
s,那么数字的定义就很奇怪了

哦,比较字符串要比比较数字慢得多

My take?停止使用比较信息更改节点。实际的比较模式与节点本身无关

然后您只需编写两种比较方法,一种按成本进行比较,另一种按来源进行比较


<> P>并回到原来的问题,如何编写一个比较器,考虑<代码> [ a ]、“b”、“aa”> /代码>排序?< /p> 你几乎做到了,但是“长度”的比较是不完整的。只有在长度不同的情况下,你才需要回到实际的词汇比较,因此你忘记了右手边参数的长度低于左手边参数的情况

因此,正确的形式是,假设两个字符串:

bool compare(std::string const& lhs, std::string const& rhs) {
  if (lhs.length() < rhs.length()) { return true; }
  if (rhs.length() < lhs.length()) { return false; } // don't forget this
  return lhs < rhs;
}
bool比较(std::string const&lhs,std::string const&rhs){
if(lhs.length()
什么是comparator?发布代码。你能展示一下comparator的定义吗?@Mike和@Mario——comparator是在初始化nodeRecord对象的过程中初始化的。你可以在上面看到这一点。我希望能够像对数字排序一样对字符串进行排序。例如,stings“a”、“aa”和“b”“应按“a”、“b”、“aa”排序。我所发布的方法是我所知道的唯一可以实现此目的的方法。不幸的是,这无法解决我的问题。同样的错误也会发生=(@BSchlinker:我的回答假设只使用一个
操作符!您的解决方案确实帮助我发现并理解了问题所在,并提醒了我另一个我忘记的案例。
// overloaded comparision operators
friend bool operator<(const nodeRecord & record1, const nodeRecord & record2){
    // we need to deal with strings of different lengths...
    if(record1.comparator.length() > record2.comparator.length()
        && (record1.comparator.length() !=0 && record2.comparator.length() != 0))
        return false;
    else if(record1.comparator.length() < record2.comparator.length()
        && (record1.comparator.length() !=0 && record2.comparator.length() != 0))
        return true;
    else
        return (record1.comparator < record2.comparator);
}
bool compare(std::string const& lhs, std::string const& rhs) {
  if (lhs.length() < rhs.length()) { return true; }
  if (rhs.length() < lhs.length()) { return false; } // don't forget this
  return lhs < rhs;
}