- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 operator< 重载的实现对于我围绕 long 值数组的包装类无法正常工作。
当在 std::map 中使用此包装器类作为键时,如果我混合不同长度的数组,调用 at/find 函数将不起作用。
const oid oid1[] = { 1,3,6,1,4,1,42570,1,3,1 };
oid_wrapper wrapper1(oid1, 10);
const oid oid2[] = { 1,3,6,1,4,1,42570,1,3,1,9,1 };
oid_wrapper wrapper1(oid2, 12);
当先添加wrapper1再添加wrapper2到map时,总是会用at/find找到wrapper1,但是对于wrapper2它会抛出一个out_of_range exception with at和iterator.end with find。
当先添加 wrapper2 再添加 wrapper1 到 map 时,它总是会用 at/find 找到 wrapper2,但是对于 wrapper1 它会抛出一个 out_of_range exception with at and iterator.end with find。
oid类型来自Net-SNMP,实际上是unsigned long int。
那么我的实现缺失或完全错误的是什么?
#include <ostream>
#include <sstream>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
class oid_wrapper {
public:
oid_wrapper(const oid* _oid_value, int _oid_length) : oid_value(0), oid_length (_oid_length) {
oid_value = snmp_duplicate_objid(_oid_value, oid_length);
}
const oid* get_oid() const { return oid_value; }
const int& get_length() const { return oid_length; }
/**
* copy operator overloader
*/
oid_wrapper& operator=(const oid_wrapper& wrapper) {
oid_value = snmp_duplicate_objid(wrapper.get_oid(), wrapper.get_length());
oid_length = wrapper.get_length();
return *this;
}
/**
* equal operator overloader
*/
bool operator==(const oid_wrapper& rhs) const {
const oid* rhs_oid = rhs.get_oid();
const int rhs_length = rhs.get_length();
if (netsnmp_oid_equals(oid_value, oid_length, rhs_oid, rhs_length) == 0) {
return true;
} else {
return false;
}
}
/**
* less-than operator overloader
*/
bool operator<(const oid_wrapper& rhs) const {
const oid* rhs_oid = rhs.get_oid();
const int rhs_length = rhs.get_length();
if (oid_length < rhs_length) { return true; }
for (int i = 0; i < oid_length; i++) {
if (oid_value[i] < rhs_oid[i]) { return true; }
}
return false;
}
const long getLastNumberInOid() const { return oid_value[oid_length - 1]; }
bool operator!=(const oid_wrapper& rhs) { return !operator==(rhs); }
bool operator>(const oid_wrapper& rhs) { return !operator<(rhs); }
bool operator<=(const oid_wrapper& rhs) { return !operator>(rhs); }
bool operator>=(const oid_wrapper& rhs) { return !operator<(rhs); }
private:
const oid* oid_value;
int oid_length;
};
这是我设计的测试类,用于检查它是否有效:
#include <iostream>
#include <ostream>
#include <string>
#include <map>
#include <stdexcept>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include "oid_wrapper.h"
using namespace std;
int main(int argc, char **argv) {
cout << "Testing first set of OID" << endl;
const oid oid1[] = { 1,3,6,1,4,1,40850,1,3 };
const oid_wrapper parent1 (oid1, 9);
const oid_wrapper parent1_child1 (parent1, 40);
const oid_wrapper parent1_child2 (parent1, 41);
const oid_wrapper parent1_child3 (parent1, 42);
const oid_wrapper parent1_child4 (parent1, 43);
map<oid_wrapper, string> test;
test.insert(make_pair(parent1_child1, "parent1_child1"));
test.insert(make_pair(parent1_child2, "parent1_child2"));
test.insert(make_pair(parent1_child3, "parent1_child3"));
test.insert(make_pair(parent1_child4, "parent1_child4"));
if (test.size() != 4) { cout << "FAIL" << endl; }
cout << "Number of OID in list: " << test.size() << endl;
const oid_wrapper parent11 (oid1, 9);
const oid_wrapper parent1_child11 (parent11, 40);
try {
string teststring = test.at(parent1_child11);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
const oid_wrapper parent1_child12 (parent11, 41);
try {
string teststring = test.at(parent1_child12);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
const oid_wrapper parent1_child13 (parent11, 42);
try {
string teststring = test.at(parent1_child13);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
const oid_wrapper parent1_child14 (parent11, 43);
try {
string teststring = test.at(parent1_child14);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
cout << "Testing second set of OID" << endl;
const oid oid2[] = { 1,3,6,1,4,1,40850,2,1,9,1 };
const oid_wrapper parent2 (oid2, 11);
const oid_wrapper parent2_child1 (parent2, 1);
const oid_wrapper parent2_child2 (parent2, 2);
const oid_wrapper parent2_child3 (parent2, 3);
const oid_wrapper parent2_child4 (parent2, 4);
test.insert(make_pair(parent2_child1, "parent2_child1"));
test.insert(make_pair(parent2_child2, "parent2_child2"));
test.insert(make_pair(parent2_child3, "parent2_child3"));
test.insert(make_pair(parent2_child4, "parent2_child4"));
if (test.size() != 8) { cout << "FAIL" << endl; }
cout << "Number of OID in list: " << test.size() << endl;
const oid_wrapper parent21 (oid2, 11);
const oid_wrapper parent1_child21 (parent21, 1);
try {
string teststring = test.at(parent1_child21);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
const oid_wrapper parent1_child22 (parent21, 2);
try {
string teststring = test.at(parent1_child22);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
const oid_wrapper parent1_child23 (parent21, 3);
try {
string teststring = test.at(parent1_child23);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
const oid_wrapper parent1_child24 (parent21, 4);
try {
string teststring = test.at(parent1_child24);
cout << "Success" << endl;
} catch (out_of_range& e) {
cout << "Failure" << endl;
}
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,1,9,1 };
const oid_wrapper wrapper(newoid, 11, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run1 = 0;
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,1,9,1 };
const oid_wrapper wrapper(newoid, 11, i);
try {
string teststring = test.at(wrapper);
run1++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run1 == 100) { cout << "Run 1 Success" << endl; }
else { cout << "Run 1: " << run1 << endl; }
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,1,3 };
const oid_wrapper wrapper(newoid, 9, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run2 = 0;
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,1,3 };
const oid_wrapper wrapper(newoid, 9, i);
try {
string teststring = test.at(wrapper);
run2++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run2 == 100) { cout << "Run 2 Success" << endl; }
else { cout << "Run 2: " << run2 << endl; }
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 10; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2 };
const oid_wrapper wrapper(newoid, 8, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run3 = 0;
for (int i = 0; i < 10; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2 };
const oid_wrapper wrapper(newoid, 8, i);
try {
string teststring = test.at(wrapper);
run3++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run3 == 10) { cout << "Run 3 Success" << endl; }
else { cout << "Run 3: " << run3 << endl; }
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,2,5,1 };
const oid_wrapper wrapper(newoid, 11, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run4 = 0;
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,2,5,1 };
const oid_wrapper wrapper(newoid, 11, i);
try {
string teststring = test.at(wrapper);
run4++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run4 == 100) { cout << "Run 4 Success" << endl; }
else { cout << "Run 4: " << run4 << endl; }
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,3,5,2 };
const oid_wrapper wrapper(newoid, 11, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run5 = 0;
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,3,5,2 };
const oid_wrapper wrapper(newoid, 11, i);
try {
string teststring = test.at(wrapper);
run5++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run5 == 100) { cout << "Run 5 Success" << endl; }
else { cout << "Run 5: " << run5 << endl; }
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,2,5,2 };
const oid_wrapper wrapper(newoid, 10, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run6 = 0;
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,2,5,2 };
const oid_wrapper wrapper(newoid, 10, i);
try {
string teststring = test.at(wrapper);
run6++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run6 == 100) { cout << "Run 6 Success" << endl; }
else { cout << "Run 6: " << run6 << endl; }
//#################################################
//Adding 100 OIDs
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,5,2,3,5 };
const oid_wrapper wrapper(newoid, 12, i);
test.insert(make_pair(wrapper, "wrapper"));
}
//Query for the 100 added OIDs
int run7 = 0;
for (int i = 0; i < 100; i++) {
const oid newoid[] = { 1,3,6,1,4,1,40850,2,5,2,3,5 };
const oid_wrapper wrapper(newoid, 12, i);
try {
string teststring = test.at(wrapper);
run7++;
} catch (out_of_range& e) {
//FAIL
}
}
if (run7 == 100) { cout << "Run 7 Success" << endl; }
else { cout << "Run 7: " << run7 << endl; }
}
最佳答案
问题可能是您的 operator<
不是反对称的。想象一下这段代码:
const oid oid1[] = {5};
oid_wrapper wrapper1(oid1, 1);
const oid oid2[] = {1, 4};
oid_wrapper wrapper2(oid2, 2);
bool b1 = wrapper1 < wrapper2;
bool b2 = wrapper2 < wrapper1;
b1
是true
, 因为 wrapper1
的长度小于 wrapper2
的长度.
b2
也是true
, 因为 wrapper2
中的第一个 oid小于wrapper1
中的.
你必须确保operator<
是总排序。它应该返回 false
如果oid_length > rhs_length
.
此外,您不能实现 operator>
作为! <
,因为这不适用于相等的值。运营商最好的选择是实现 operator==
和 operator<
并将其余部分表示为(伪代码):
operator!=(a, b) {! (a == b)}
operator<=(a, b) {a < b || a == b}
operator>(a, b) {! (a < b)}
operator>=(a, b) {! (a < b)}
关于c++ - 为长值数组重载 operator<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13452181/
关闭。这个问题不符合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; } 在这样的类中:
我是一名优秀的程序员,十分优秀!