std::list; list_type 为 (char * data, int lenght)

发布于 2024-07-27 20:16:41 字数 199 浏览 5 评论 0原文

我有一些

std::list<char> list_type

现在我必须将列表的内容提供为(char *data,int length)。 有没有方便的方法将列表内容呈现为指针和长度? 有这样的接口吗?

先感谢您。

I have some

std::list<char> list_type

Now I have to supply contents of the list as (char *data, int length). Is there convenient way to present list contents as pointer and length? Does <vector> has such interface?

Thank you in advance.

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

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

发布评论

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

评论(5

请持续率性 2024-08-03 20:16:42

您可以使用向量来完成此操作,因为它的数据是连续存储的:

std::vector<char> vec;

char* data = &vec[0];
int length = static_cast<int>(vec.size());

对于列表,您必须将数据复制到数组中。 幸运的是,这也相当简单:

std::list<char> list:
int length = static_cast<int>(list.size());
char* data = new char[length]; // create the output array
std::copy(list.begin(), list.end(), data); // copy the contents of the list to the output array

当然,您会留下一个动态分配的数组,您必须再次释放它。

You can do it with a vector, because its data is stored contiguously:

std::vector<char> vec;

char* data = &vec[0];
int length = static_cast<int>(vec.size());

For list, you have to copy the data to an array. Luckily, that too is fairly easy:

std::list<char> list:
int length = static_cast<int>(list.size());
char* data = new char[length]; // create the output array
std::copy(list.begin(), list.end(), data); // copy the contents of the list to the output array

Of course, you're then left with a dynamically allocated array you have to free again.

空城旧梦 2024-08-03 20:16:42

您可以使用向量来完成此操作,而不是使用列表。 向量保证是连续的内存块,因此您可以说:

char *data = &list_type[0];
std::vector<char>::size_type length = list_type.size();

You can do this with vector, not with list. A vector is guaranteed to be a contigous chunk of memory so you can say:

char *data = &list_type[0];
std::vector<char>::size_type length = list_type.size();
你不是我要的菜∠ 2024-08-03 20:16:42

我不知道 std::list,但 std::vector 知道:

std::vector<char> list_type;

...

foo(&list_type[0], list_type.size())

std::string 也可以完成这项工作,但你可能已经知道了。

I don't know about std::list, but std::vector does:

std::vector<char> list_type;

...

foo(&list_type[0], list_type.size())

std::string can do the job too, but you probably already know it.

孤檠 2024-08-03 20:16:42

您无法使用列表执行此操作,因为列表将其数据保存在列表节点中。 但是,您可以使用向量来完成此操作,保证将其数据存储在连续的内存中。 您可以使用 &v[0]&*v.begin() 来获取指向其第一个元素的指针:

void f(std::list<char>& list)
{
  std::vector<char> vec(list.begin(),list.end());
  assert(!vec.empty());
  c_api_function(&vec[0],vec.size());
  // assuming you need the result of the call to replace the list's content
  list.assign(vec.begin(),vec.end());
}

请注意,向量将自动释放其第一个 元素函数返回时的内存。
还有(至少)两件事值得注意:

  • 向量不能为空。 不允许您访问空向量的 v[0]。 (也不允许取消引用 v.begin()。)
  • 由于涉及动态分配,因此需要在 std::liststd:: 之间来回转换。矢量可能是真正的性能杀手。 考虑完全切换到 std::vector 。

You cannot do this with a list, as a list saves its data in list nodes. However, you can do this with a vector, which is guaranteed to store its data in a contiguous piece of memory. You can use either &v[0] or &*v.begin() to get a pointer to its first element:

void f(std::list<char>& list)
{
  std::vector<char> vec(list.begin(),list.end());
  assert(!vec.empty());
  c_api_function(&vec[0],vec.size());
  // assuming you need the result of the call to replace the list's content
  list.assign(vec.begin(),vec.end());
}

Note that the vector will automatically free its memory when the function returns.
There are (at least) two more noteworthy things:

  • The vector must not be empty. You are not allowed to access v[0] of an empty vector. (Neither are you allowed to dereference v.begin().)
  • Since dynamic allocation is involved, converting back and forth between std::list and std::vector can be a real performance killer. Consider switching to std::vector altogether.
奈何桥上唱咆哮 2024-08-03 20:16:42

list是一个链表数据结构。 如果不进行转换,您就无法(理论上)做到这一点。

您将能够访问 (C+ +0x Draft 23.2.6.3)C++0x 中带有 .data()向量 的后备存储。 目前,最好的选择是通过获取初始元素的地址将其视为数组。

list is a linked list data structure. There's no way you could do that (theoretically) without conversion.

You'll be able to access (C++0x Draft 23.2.6.3) the backing store of a vector with .data() in C++0x. Currently, your best bet is to treat it as an array by taking the address of the initial element.

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