如何在 C 函数中使用 std::vector

发布于 2024-11-19 22:12:40 字数 199 浏览 3 评论 0原文

AC 函数期望缓冲区数组在运行时位于范围内。例如

char values[x][y]

C 函数将填充缓冲区
我想使用动态数组,这样我就不必对尺寸进行硬编码
在这种情况下如何使用 std::vector ?

需要明确的是,我正在使用 C++。 C 函数包含在我无法修改的库中。

A C function expects an array of buffers to be in scope at runtime. e.g.

char values[x][y]

The C function will populate the buffers
I would like to use a dynamic array so I don't have to hard code the dimensions
How do I use a std::vector in this situation?

Just to be clear, I am using C++. The C function is contained in a library that I cannot modify.

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

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

发布评论

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

评论(3

默嘫て 2024-11-26 22:12:40

如果您只想将封装在 std::vector 中的动态数组传递给 c 例程,您可以将指针传递给底层数组的头部,如下所示

std::vector<char> myvector;
// size-up myvector as needed
foo(&myvector[0]); // pass a pointer to start of myvector to function foo

: code>c++ 标准确保 std::vector 中的底层数组始终是连续的。

希望这有帮助。

编辑:虽然声明 char values[x][y] 创建一个“数组的数组”,但 values 的内存实际上只是一个连续的块,本质上是 char Linear_values[x * y]

如果您调整 std::vector 的大小以包含 x * y 元素的计数,它应该为您提供相同的底层动态分配的数组空间。

c 函数将以行优先顺序访问数组,因此第一行元素将首先出现,然后是第二个完整行,依此类推...

If you just want to pass the dynamic array encapsulated in a std::vector to a c routine you can pass a pointer to the head of the underlying array as:

std::vector<char> myvector;
// size-up myvector as needed
foo(&myvector[0]); // pass a pointer to start of myvector to function foo

The c++ standard ensures that the underlying array in std::vector is always contiguous.

Hope this helps.

EDIT: While the declaration char values[x][y] creates an "array of arrays" the memory for values will actually just be a contiguous block, essentially char linear_values[x * y].

If you size your std::vector to include a count of x * y elements it should give you the same underlying dynamically allocated array space.

The c function will access the array in row-major order, so the first row of elements will come first, followed by the second full row etc...

谁的年少不轻狂 2024-11-26 22:12:40

C 没有标准的数据结构库。

如果您确实想要向量的所有功能,并且不是对于某些关键的功能,您可能可以在网上找到某人的直接 C 向量的宠物实现并使用它。

如果它重要,请编写自己的。这并不太难,而且非常有用。

如果您只想要一个动态增长的数组,则可以使用 realloc 函数轻松模拟向量的行为,该函数扩展了堆分配数组的维度。从一个小数组开始,当到达末尾时根据需要进行增长。大块增长的效率更高,但如果您对数据的外观有所了解,则可以以不同的方式增长它。一种常见的方法是每次用完时将数组大小加倍。

您可以在以下位置获取 realloc 的详细信息:

http://www.cplusplus.com/reference /clibrary/cstdlib/realloc/

或者,在 *nix 系统上:

man realloc

C doesn't have standard data structures libraries.

If you really want all the functionality of a vector, and it's not for something critical, you can probably find someone's pet implementation of a straight C vector online and just use that.

If it is critical, write your own. It's not too hard, and can be quite useful.

If you just want a dynamically growing array, it's easy to emulate that behavior of a vector using the realloc function, which extends the dimensions of a heap-allocated array. Start with a small array, and grow as needed when you reach the end. It's more efficient to grow in big chunks, but if you have some idea of what your data looks like you could grow it in a different way. A common method is doubling the array size every time you run out.

You can get the details of realloc at:

http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/

or, on a *nix system:

man realloc

七禾 2024-11-26 22:12:40

你不能。

根据定义,C 对 std::vector 所需的任何组件一无所知,包括但不限于:

  • C 没有命名空间,因此它无法理解 < code>std 命名空间。

  • C 没有模板,因此无法理解 std::vector 类型。

本质上,您需要看起来像 C 函数的东西,但实际上,它是一个 C++ 函数。

实现这一目标的最简单方法可能是使用 C++ 编写看起来像 C 函数的内容,并通过 C++ 编译器而不是 C 编译器运行整个混乱的过程。

You can't.

By definition, C knows nothing of any of the required components of a std::vector, including, but not limited to:

  • C does not have namespaces, so it can't understand the std namespace.

  • C does not have templates, so it can't understand the std::vector<T> type.

Essentially, you need what looks like a C function, but that is, for all intents and purposes, a C++ function.

The simplest way to achieve this is probably to write what looks like a C function, using C++, and running the whole mess through a C++ compiler rather than a C compiler.

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