- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个存储一些数据
的对象
列表。一旦一个对象被添加到列表中,它就永远不会被删除。给定一些数据,我也想快速查找它来自哪个对象。
与其遍历整个列表并检查每个对象,不如创建一个从数据到对象的映射。
#include <vector>
#include <map>
class Data {/*Stuff here*/};
struct Object {
Data d;
Object(Data d) : d(d) {}
};
class ObjectStorage {
std::vector<Object> objects;
std::map<Data&, Object&> reverse_lookup;
public:
void add_object(Data d) {
objects.emplace_back(d);
auto &obj = objects.back();
reverse_lookup.emplace(obj.d, obj);
}
};
int main() {
ObjectStorage S;
S.add_object(Data());
}
为了节省空间,我在 map 中使用了引用资料。对象和数据已存储在列表中,我知道它们永远不会被删除。
但是,在尝试编译该程序时出现以下错误。
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1024:18: error: multiple overloads of 'operator[]' instantiate to the same signature 'std::__1::map<Data &, Object &, std::__1::less<Data &>, std::__1::allocator<std::__1::pair<Data &, Object &> > >::mapped_type &(std::__1::map<Data &, Object &,
std::__1::less<Data &>, std::__1::allocator<std::__1::pair<Data &, Object &> > >::key_type &)'
mapped_type& operator[](key_type&& __k);
^
test.cpp:13:27: note: in instantiation of template class 'std::__1::map<Data &, Object &, std::__1::less<Data &>, std::__1::allocator<std::__1::pair<Data &, Object &> > >' requested here
std::map<Data&, Object&> reverse_lookup;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1022:18: note: previous declaration is here
mapped_type& operator[](const key_type& __k);
^
1 error generated.
错误的主要部分似乎是:
error: multiple overloads of 'operator[]' instantiate to the same signature
最佳答案
错误源于在 std::map
中使用引用.见问题"Why Can't I store references in a std::map
in C++?"有关原因的更多信息。
如果不是太贵,改reverse_lookup
通过
std::map<Data, Object> reverse_lookup;
并为 bool operator<(const Data& lhs, const Data& rhs)
添加定义允许 Data
用作 key 将解决问题。
如果复制Data
和 Object
太昂贵了,可以使用指针映射代替引用。但是,请注意指向数据的存储方式。在这种情况下,如果 objects
vector 增长超过其内部容量并调整大小,所有指针都将变为无效。使用类似 std::list
的东西不会在内存中移动数据应该可以防止该问题。感谢Christophe感谢您在评论中指出这一点。
关于c++ - 'operator[]' 的多个重载实例化为相同的签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55230481/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!