如何使用索引访问C ++结构属性值? [英] How to access C++ struct property value using index?

查看:31
本文介绍了如何使用索引访问C ++结构属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct student {
    string name;
    int age;
};

int main() {
    student a1;

    cout << a1[0] << endl;  //Access the first variable of the struct
    cout << a2[1] << endl;  //Access the first variable of the struct
}

如何使用索引而不是使用"a1.name"来从C ++结构中访问和检索值?

How could I access and retrieve value from the C++ struct using index instead of using "a1.name" ??

推荐答案

您不能.至少不是直接以您想要的方式进行操作,而没有部分重新定义结构是什么.我将答案分为两部分,第一部分说明了至少接近您想要的方式的可能方法,第二部分说明了您应实际执行的操作:

You can't. At least not in the direct manner you want to do it and without partially redefining what a structure is. I will split my answer into two parts the first one explaining possible ways to get at least close to what you want and the second one explaining what you actually should do:

下床弄脏

有两种方法(我目前可以提出)可以给您一些思考:

There are two ways (that I can currently come up with) that might give you something to think about:

  • 使用包装器类-尽管C ++确实增加了结构的灵活性,但它并没有改变其作为简单异构数据容器的目的.但是,它确实允许运算符重载,包括 [] 运算符.因此,如果您创建一个包含该结构作为其成员的类(也就是说,它包装了该结构),则可以使用 [] 公开该结构的数据.这与您想要做的尽可能接近.但是,它的确违背了使用结构的全部目的,因为您可以只使用普通的非强壮的类成员就可以做到这一点,但实际上我不久前才看到它,当时我正在研究一个包装了以前C-的C ++库.本身的基本版本,以便提供更现代的功能,而无需完全重写C代码.
  • 使用带偏移量的指针-使用索引通常表明底层容器在涉及其包含的数据块时具有一致性.问题在于结构不一定要遵循此要求,因为它可以包含(就像您的情况一样)多种类型的数据.如果您可以牺牲结构的异构性并坚持使用单一数据类型(例如一个或多个双精度),则可以安全地使用(直到必须始终记住该结构具有的成员数)指针以及增加/减少的偏移量以访问其成员.就像在创建指向某物的标准引用(即指针)时使用的任何类型的数据一样,该引用指向该数据正在使用的内存起始地址.使用指针来遍历数组是一种常见的做法,它的工作原理与之完全相同-创建对结构的引用以及添加+ 1,+ 2,...(与struct的许多成员一样).但是,这会使事情变得过于复杂,并且容易出错.如前所述,它还需要在结构内部使用相同类型的数据.
  • Use a wrapper class - while C++ does increase the flexibility of structure it doesn't change their purpose of a simple heterogeneous data container. It does however allow operator overloading including the [] operator. So if you create a class that contains the structure as its member (that is it wraps around the structure), you can expose the structure's data using []. This comes as close to what you want to do as possible. It does however defeat the whole purpose of using a struct since you can do that with just plain non-sturct class members but I have actually seen it not so long time ago when I was going through a C++ library that was wrapping a previous C-based version of itself in order to provide more modern features without the need of completely rewriting the C code.
  • Use pointer with an offset - using indexing generally suggest that the underlying container has a consistency when it comes to the blocks of data it contains. The problem is that a structure doesn't necessarily obey this since it can contain (just like in your case) multiple types of data. If you can sacrifice the heterogeneity of your structure and stick with a single data type (for example one or more doubles), you can safely use (up to the point that you have to always remember the number of members the structure has) a pointer and an increasing/decreasing offset to access its members. Just like with any sort of data when you create a standard reference (aka pointer) to something, that reference points at the address of the beginning of the memory this data is using. It is a common practice to use pointers to iterate through arrays and it works exactly like that - create a reference to your structure and the add +1, +2, ... (as many members that struct has). This makes things overly complicated though and is prone to error. As mentioned it also requires using the same type of data inside your structure.

替代方案...

根据您提供的信息,我认为您正在寻找一种完全不同的数据类型-字典,地图或包含某种自定义通用数据容器的列表,该容器可以保存任何类型的数据,但也可以存储这些数据数据的类型,以便允许将其重铸为原始状态.许多库都提供了这样的容器,例如Qt具有 QVariant (核心模块的一部分), boost 具有 boost :: variant std :: tuple (甚至更好的命名为tuple),依此类推.因为我有更多的经验,所以我可以更详细地谈论Qt.它提供了允许编制索引的 QVariantList (用于 QList< QVariant> typedef ).当然,所有这些都需要您1)放弃结构,2)使用一些更高级的容器,这些容器可能会也可能不会在您正在从事的工作上带来巨大的弊端,包括许可问题,内存开销,更大的二进制文件,处理大量额外的库文件等.

From what you have given as information I think you are looking for a completely different type of data - a dictionary, map or a list that contains some sort of custom generic data container that can hold any type of data but also stores that data's type in order to allow recasting it to its original state. Many libraries provide such containers for example Qt has the QVariant (part of the core module), boost has the boost::variant, std::tuple (or even better - named tuples) provided with your standard C++ (since C++11) and so on. I can speak about Qt in greater detail since I have more experience with it. It offers the QVariantList (a typedef for QList<QVariant>) which allows indexing. Of course all this requires you to 1)abandon your structure-thing and 2)use some more advanced containers that may or may not introduce huge drawbacks on whatever you are working on including licensing issues, memory overhead, larger binaries, handling a lot of extra library files etc.

这篇关于如何使用索引访问C ++结构属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆