是否可以创建一个向量图,其中向量可以是任何类型?

发布于 2024-12-14 00:41:14 字数 108 浏览 0 评论 0 原文

键是一个字符串,值是任何类型的向量。

例如,第一个键可以映射到整数向量,第二个键可以映射到浮点数向量。这在c++中可能吗?

如果没有,我可以通过其他方式达到类似的效果吗?

The key would be a string and the value would be a vector of any type.

For example the first key could map to a vector of ints and a second key could map to a vector of floats. Is that possible in c++?

If not is there a way I could achieve a similar effect through some other means?

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

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

发布评论

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

评论(4

巷子口的你 2024-12-21 00:41:14

好吧,你可以这样做:

std::map<std::string, std::vector<boost::any> >

但这不太可能是你真正想要的。你为什么认为你需要这个?

我有一个 opengl 渲染器,目前用 java 编写,但正在移植到 c++。在此渲染器中,我尝试隔离可以专门更改缓冲区和着色器的部分。所以我有一个registerBuffer和registerShader函数。 registerBuffer 函数将着色器变量名称(字符串)映射到缓冲区。但缓冲区可以是 GLfloat 或 GLushort。所以现在我希望能够将着色器变量名称映射到 GLfloat 或 GLushort 向量。

那么,您可以使用 std::map>

Well, you could do it:

std::map<std::string, std::vector<boost::any> >

But it is very unlikely that this is what you actually want. Why do you think you need this?

I have an opengl renderer currently written in java but being ported over to c++. In this renderer I've tried to isolate the parts that can change specifically the buffers and shaders. So I have a registerBuffer and registerShader function. The registerBuffer function maps the shader variable name (a string) to the buffer. But buffers could be GLfloat or GLushort. So now I want to be able to map the shader variable name to a vector of GLfloat or GLushort.

Well, you could use a std::map<std::string, boost::variant<GLfloat, GLushort>> then.

掩于岁月 2024-12-21 00:41:14

boost::任何解决方案对我来说都像是黑客。我建议您为渲染器研究面向对象的设计。

您需要的是一个具有字符串键和缓冲区作为值的映射,对吗?然后编写一个 Buffer 类,它可以在内部保存任何类型的 OpenGL 缓冲区,并让每个实例跟踪它所保存的缓冲区的类型。此类的数据存储可以是通用的 void*,然后在需要将缓冲区发送到 OpenGL 时将其转换为正确的类型。

然后,你的数据结构问题就完全解决了,你可以做:

std::map<std::string, Buffer>

祝你好运。

The boost::any solution seems like a hack to me. I recommend that you look into an object oriented design for your renderer.

What you need is a map that has string keys and buffers as values, correct? Then write a Buffer class, that can internally hold OpenGL buffers of any type, and make each instance keep track of what is the type of the buffer it holds. The data storage for this class could be a generic void* that is then cast to the proper type when the buffer needs to be sent to OpenGL.

Then, your data structure problem is fully resolved and you can do:

std::map<std::string, Buffer>

Good luck.

月下伊人醉 2024-12-21 00:41:14

任何类型?您可以考虑使用 Boost.Any。那么你的类型将是:

std::map<std::string, std::vector<boost::any> >

Any type? You could consider using Boost.Any. Then your type would be:

std::map<std::string, std::vector<boost::any> >
绿阴红影里的.如风往事 2024-12-21 00:41:14

向量 的值必须全部为同一类型T,而map 的值必须全部为相同类型类型为 V ,对于某些 T 来说是 vector ,所以不,不是开箱即用的,但您可以绕过该类型系统与 Boost.Any 库。

A vector<T>'s values must all be of the same type T and a map<K,V>'s values must all of type V which is vector<T> for some T, so no, not out of the box, but you can get around the type system with the Boost.Any library.

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