[英]Retrieve pointer that can acess all the elements of arrays stored in a std::vector
Is it possible to have a pointer that points to the contiguous buffer that is used by a vector?是否可以有一个指向向量使用的连续缓冲区的指针?
For example (see below please), here std::vector<unsigned char*> vec
contains two differently sized unsigned char*
pointers.例如(请参见下文),这里
std::vector<unsigned char*> vec
包含两个不同大小的unsigned char*
指针。 I need to have a buffer pointer that points to all pushed data in this vector.我需要一个指向这个向量中所有推送数据的缓冲区指针。 I'd guess that this is possible as the standard guarantees that a vector uses a contiguous memory buffer, right?
我猜这是可能的,因为标准保证向量使用连续的内存缓冲区,对吗?
PS are the two ways of printing the elements of the vector I use in this example fine? PS 打印我在这个例子中使用的向量元素的两种方式好吗? (the two for loops)
(两个for循环)
unsigned char* data1 = new unsigned char[3];
data1[0] = 'a';
data1[1] = 'b';
data1[2] = 'c';
unsigned char* data2 = new unsigned char[1];
data2[0] = 'x';
std::vector<unsigned char*> vec;
vec.push_back(data1);
vec.push_back(data2);
for (size_t i = 0; i < vec.size(); i++) {
std::cout << vec[i];
}
std::cout << "\n";
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
std::cout << (*iter);
}
std::cout << "\n\n";
unsigned char* buffer = (unsigned char*) vec[0];
Does buffer
point to all data in vec
? buffer
是否指向vec
所有数据? ie buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?即
buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?
Does
buffer
point to all data invec
?buffer
是否指向vec
所有数据? iebuffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?即
buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?
It doesn't.它没有。 It points to the beggining of the array stored in first element of the vector.
它指向存储在向量第一个元素中的数组的开始。
Are the two ways of printing the elements of the vector I use in this example fine?
我在这个例子中使用的两种打印向量元素的方法好吗?
They are not, those arrays are not null terminated, they can't be printed as strings.它们不是,这些数组不是空终止的,它们不能作为字符串打印。
Is it possible to have a pointer that points to the contiguous buffer that is used by a vector?
是否可以有一个指向向量使用的连续缓冲区的指针?
Yes, it's possible.是的,这是可能的。
If you'd like a pointer that can correctly access all the data in the vector, including individual elements of the unsigned char array members you'd want:如果您想要一个可以正确访问向量中所有数据的指针,包括您想要的 unsigned char 数组成员的各个元素:
unsigned char **buffer = vec.data();
And the access:和访问:
for(size_t i = 0; i < 3; i++)
std::cout << buffer[0][i]; //indexing like a 2D array, albeit unbalanced
//output: abc
std::cout << buffer[1][0]; //output: x
Note that I use a cycle to access each element of data1
instead of simply treating it like a string, and this is because it is not a string, aka a null terminated char array.请注意,我使用循环来访问
data1
每个元素,而不是简单地将其data1
字符串,这是因为它不是字符串,也就是空终止字符数组。
Needless to say that you will need to know how many elements are stored in each array.不用说,您需要知道每个数组中存储了多少个元素。
Alternatively you can null terminate them:或者,您可以空终止它们:
unsigned char* data1 = new unsigned char[4];
//...
data1[3] = '\0';
And和
unsigned char* data2 = new unsigned char[2];
//...
data2[1] = '\0';
Here printing them like strings:在这里像字符串一样打印它们:
std::cout << buffer[0];
std::cout << buffer[1];
Using a null terminator has the extra benefit of allowing you to know the size of the arrays at any time using strlen((char*)buffer[0])
.使用空终止符的额外好处是允许您随时使用
strlen((char*)buffer[0])
了解数组的大小。
You want the data()
method on vector.你想要向量上的
data()
方法。 It will return a pointer to the data, assuming that the vector size is greater than zero.假设向量大小大于零,它将返回一个指向数据的指针。 If it is zero, then
data()
will return something but using it is undefined.如果它为零,那么
data()
将返回一些东西,但使用它是未定义的。
Read https://en.cppreference.com/w/cpp/container/vector/data阅读https://en.cppreference.com/w/cpp/container/vector/data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.