将 char* 转换为 std::vector
是否有一种快速(CPU)方法将 char
数组转换为 std::vector
?
我的函数如下所示:
void someFunction(char* data, int size)
{
// Now here I need to make the std::vector<char>
// using the data and size.
}
Is there a fast (CPU) way to cast a char
array into a std::vector<char>
?
My function looks like this:
void someFunction(char* data, int size)
{
// Now here I need to make the std::vector<char>
// using the data and size.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在这里“转换”任何内容,但您可以轻松地从 C 字符串构造一个向量:
但这将创建一个副本。
You can't "cast" anything here, but you can easily construct a vector from the C string:
This will create a copy though.
STL 容器的一般规则是它们会复制其内容。使用 C++11,可以使用特殊方法将元素移动到容器中(例如,
emplace_back()
std::vector
的成员函数),但在您的示例中,元素是char
对象,因此您仍然要复制每个size
char
对象。将 std::vector 视为指向数组的指针以及数组长度的包装器。 “将
char *
转换为std::vector
”最接近的等效项是将向量的指针和长度替换为给定的指针和长度,但是length 已指定(两种可能性是指向结束元素之后的指针或size_t
值)。std::vector
没有标准成员函数可用于将其内部数据与给定的指针和长度交换。不过,这是有充分理由的。
std::vector
为其包含的每个元素实现所有权语义。它的底层数组是用some分配器(第二个模板参数)分配的,默认是std::allocator
。如果允许您交换内部成员,那么您需要确保使用同一组堆分配例程。此外,您的 STL 实现需要修复存储向量“长度”的方法,而不是不指定此细节。在 OOP 的世界中,指定过多的细节通常是不受欢迎的,因为它会导致更高的耦合。但是,假设您的 STL 实现存在这样的成员函数。在您的示例中,您根本不知道如何分配
data
,因此您可能会无意中为std::vector
提供一个指向未按预期分配的堆内存的指针分配器。例如,data
可以使用malloc
进行分配,而向量可以使用delete
来释放内存。使用不匹配的分配和释放例程会导致未定义的行为。您可能要求someFunction()
仅接受使用特定分配例程分配的数据,但这再次指定了不必要的详细信息。希望我已经证明,交换内部数据成员的
std::vector
成员函数不是一个好主意。如果您确实需要来自data
和size
的std::vector
,您应该使用以下命令构造一个:The general rule with STL containers is that they make copies of their contents. With C++11, there are special ways of moving elements into a container (for example, the
emplace_back()
member function ofstd::vector
), but in your example, the elements arechar
objects, so you are still going to copy each of thesize
char
objects.Think of a
std::vector
as a wrapper of a pointer to an array together with the length of the array. The closest equivalent of "casting achar *
to astd::vector<char>
" is to swap out the vector's pointer and length with a given pointer and length however the length is specified (two possibilities are a pointer to one past the end element or asize_t
value). There is no standard member function ofstd::vector
that you can use to swap its internal data with a given pointer and length.This is for good reason, though.
std::vector
implements ownership semantics for every element that it contains. Its underlying array is allocated with some allocator (the second template parameter), which isstd::allocator
by default. If you were allowed to swap out the internal members, then you would need to ensure that the same set of heap allocation routines were used. Also, your STL implementation would need to fix the method of storing "length" of the vector rather than leaving this detail unspecified. In the world of OOP, specifying more details than necessary is generally frowned upon because it can lead to higher coupling.But, assume that such a member function exists for your STL implementation. In your example, you simply do not know how
data
was allocated, so you could inadvertently give astd::vector
a pointer to heap memory that was not allocated with the expected allocator. For example,data
could have been allocated withmalloc
whereas the vector could be freeing the memory withdelete
. Using mismatched allocation and deallocation routines leads to Undefined Behavior. You might require thatsomeFunction()
only accept data allocated with a particular allocation routine, but this is specifying more details than necessary again.Hopefully I have made my case that a
std::vector
member function that swaps out the internal data members is not a good idea. If you really need astd::vector<char>
fromdata
andsize
, you should construct one with: