在 C++ 中使用最小类型歧义;

发布于 2024-12-28 13:35:52 字数 829 浏览 2 评论 0原文

我的 C++ 程序中有一个数据结构,其中有一些“Vector”类型的属性(由我定义)和一些“double”类型的属性。在我的代码的另一个地方,我非常希望能够迭代这个数据结构,并对每个值执行相同的算术运算。我需要的运算符是为 Vector 定义的,因此代码实际上看起来与我需要为 Vector 类型和 double 类型执行的操作相同。

如果可能的话,我想避免迭代“向量值”,然后单独迭代“双值”。

我意识到这对 C++ 的严格类型造成了压力,但是有什么好的方法可以实现这一点吗?

编辑:

现在,我正在做一些像这样的例行操作:

e.values.vel = k0[i].vel + k1[i].vel * c_1;
e.values.acc = k0[i].acc + k1[i].acc * c_1;
....

(e和k0[i]是相同的类类型)

现在,唯一阻止我概括e类型的事实是e的一些值可能是向量,有些可能是双精度数,理想情况下,此代码将处理这种类型的不同对象,这些对象可能具有不同的数字或类型的值。我想要的看起来像:

for (int j = 0; j < e.values.size(); j ++)
{
    e.values[j] = k0[i].values[j] + k1[i].values[j]
}

理想情况下,我希望 e 是可迭代的,或者提供某种下一个函数,但要做到这一点,要么:它必须将其变量存储在只能包含一种已知类型的数组中,或者它将提供下一个只能返回一种已知类型的函数。

我正在尝试找到一种方法来抽象这个概念,以便我可以在不知道每个值的类型的情况下迭代 e 类型的某些内容。

I have a data structure in my C++ program that has some attributes of type 'Vector' (defined by me), and some of type 'double'. In another place in my code, I would very much like to be able to iterate through this data structure, and perform the same arithmetic operations on each value. The operators that I will need are defined for Vector, so the code literally looks the same for what I would need to do for a Vector type and a double type.

If at all possible, I would like to avoid iterating through the 'Vector values' and then separately iterating through the 'double values'.

I realize this is straining the strict-typing of C++, but is there any good way to accomplish this?

EDIT:

Right now, I'm doing some routine operations like this:

e.values.vel = k0[i].vel + k1[i].vel * c_1;
e.values.acc = k0[i].acc + k1[i].acc * c_1;
....

(e and k0[i] are the same class type)

Right now, the only thing keeping me from generalizing e's type is the fact that some of e's values may be vectors, and some may be doubles, and ideally this code will deal with different objects of this type which may have different numbers or kinds of values. What I want would look like:

for (int j = 0; j < e.values.size(); j ++)
{
    e.values[j] = k0[i].values[j] + k1[i].values[j]
}

Ideally, I would have e be iterable, or provide some sort of next function, but to do that, either: it would have to store it's variables in an array which can only contain one known type, or it would provide the next function which can only return one known type.

I'm trying to find a way where I can abstract this concept so that I can iterate through something of e's type without knowing the type of each value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2025-01-04 13:35:52

您可能需要考虑 Boost.Fusion。它允许您将类型转换为编译时序列,然后您可以使用编译时扩展 foreach 为每个元素调用适当的函子。

You might want to consider Boost.Fusion. It allows you to turn your type into a compile-time sequence, where you can then use a compile-time-expanding foreach to call an appropriate functor for each element.

庆幸我还是我 2025-01-04 13:35:52

您的向量类型是否允许多维?如果是这样,您可以使用一维向量作为内在类型 double 的包装器,然后只迭代向量。

class Vector {
public:
   Vector(int dimensions = 1);
   ...
   double& operator[](int index);
};

或者,您可以编写一个 Vector 抽象类,然后仅实现您想要的维度:1、2、3 维向量很常见。

class Vector {
public:
   ...
   Vector operator+(const Vector& rhs) const = 0;
   ...
};

class Vector1D : Vector {
public:
   ...
   vector operator+(const Vector& rhs) const;
}

class Vector2D : Vector {
public:
   ...
   vector operator+(const Vector& rhs) const;
}

class Vector3D : Vector {
public:
   ...
   vector operator+(const Vector& rhs) const;
}

当然,如果您有适用于特定维度向量的特殊优化或功能,您可以同时执行这两种操作。

我想我真正想说的是,你应该将双精度数解释为一维向量。

Does your vector type admit multiple dimensionalities? If so, you could use 1-D Vectors as a wrapper for the intrinsic type double, and then just iterate over vectors.

class Vector {
public:
   Vector(int dimensions = 1);
   ...
   double& operator[](int index);
};

Alternatively, you could write a Vector abstract class, then implement only the dimensionalities you want: 1-, 2-, 3-d vectors are common.

class Vector {
public:
   ...
   Vector operator+(const Vector& rhs) const = 0;
   ...
};

class Vector1D : Vector {
public:
   ...
   vector operator+(const Vector& rhs) const;
}

class Vector2D : Vector {
public:
   ...
   vector operator+(const Vector& rhs) const;
}

class Vector3D : Vector {
public:
   ...
   vector operator+(const Vector& rhs) const;
}

Of course, you can do both, if you have special optimizations or functionality that applies to vectors of specific dimensionalities.

I suppose what I'm really saying is that you should just interpret doubles as 1-dimensional vectors.

最美不过初阳 2025-01-04 13:35:52

向量使用连续内存,与数组非常相似,因此假设双精度数位于数组中,则可以以相同的方式迭代两者。您可以使用函数模板来一般性地对待它们:

template <typename T>
void DoOperationOnSequence(size_t maxIndex, const T& container) {
    for (int i = 0; i < maxIndex; ++i) {
        //Do something with container[i];
    }
}

假设您的双打处于连续的(内存中一个接一个的不间断)序列中,这应该可以正常工作。

PS:抱歉,如果这不是您想要的,但您的问题有点难以理解,所以我只是想尝试一下:)

A vector uses contiguous memory much like an array, so assuming your doubles are in an array, you can iterate over both the same way. You can use a function template to treat them generically as so:

template <typename T>
void DoOperationOnSequence(size_t maxIndex, const T& container) {
    for (int i = 0; i < maxIndex; ++i) {
        //Do something with container[i];
    }
}

This should work fine, assuming your doubles are in a contiguous (one after another un-interupted in memory) sequence.

PS: sorry if this isn't what you were looking for, your questions a little hard to understand though so I just thought I'd take a stab at it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文