- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
目前正在尝试在 C++ 中对对象 vector 进行排序,每个对象包含一个字符串
字符串可以包含字母或数字(由于设计限制,这是必要的,因为比较器可以更改)。
此时重载对象的类,这样当比较两个对象时,比较的是它们包含的字符串。这在一定程度上起作用——但是,当我使用排序操作(例如 STL 排序)对对象进行排序时,它将按顺序对三个字符串(例如“1”、“4”、“12”)进行排序“1”、“12”、“4”。 4 大于 12,但因为它从最左边的数字开始比较,所以会出现这种“不正确”的排序。
我最初的 react 是改变我重载比较操作的方式。我会首先检查我正在比较的字符串的长度——如果字符串的内容更大或更小,这将是一个明显的迹象。
// 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);
}
此操作会在运行时产生“表达式:无效运算符<”消息。
关于我在哪里犯错有什么想法吗?看来我应该能够准确地指示操作我希望排序操作如何发生——即使它是无效的,因为我目前正在使用一个 vector 来包含对象。
nodeRecord 对象初始化期间的比较器:
nodeRecord(int fromNode, int toNode, int connectionCost, bool compareByCost = false){
// take the provided stock information and insert it into the object
stringstream fromNodeSS;
fromNodeSS << fromNode;
this->fromNode = fromNodeSS.str();
stringstream toNodeSS;
toNodeSS << toNode;
this->toNode = toNodeSS.str();
this->connectionCost = connectionCost;
// set the comparator to our chosen comparision term
if (!compareByCost){
this->comparator = this->fromNode; // we use from node in this case, since we build the tree outwards
}
else{
stringstream ss;
ss << this->connectionCost;
this->comparator = ss.str(); // we use the connection cost in this case, to allow us to sort new connections
}
// set this as a non-null (active) record
this->nullRecord = false;
}
最佳答案
您的运营商实际上无效。
运营商<
如果您希望它可用于排序,则必须具有许多数学属性。一个是 AntiSymmetry 属性:
x < y => !(y < x)
让我们定义x = "b"
和 y = "aa"
.
x < y
因为 "b"
的长度不如"aa"
的长度y < x
因为"aa"
不如"b"
嗯?
另请注意,您对数字的定义很奇怪,因为它们的前缀为 0
哦,比较字符串比比较数字慢得多。
我的看法?停止更改具有比较信息的节点。实际比较模式与节点本身无关。
然后你就写两个比较方法,一个按成本比较,一个按原产地比较。
回到最初的问题,如何编写一个考虑 ["a", "b", "aa"]
的比较器排序了吗?
您快到了,但是“长度”比较不完整。只有在长度不同的情况下,您才需要回退到实际的词法比较,因此您忘记了右侧参数的长度低于左侧参数的情况。
因此,假设有两个字符串,正确的形式是:
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;
}
关于c++ - 在 C++ 中重载比较运算符导致 "invalid operator<",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5786296/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!