检索可以访问存储在 std::vector 中的数组的所有元素的指针
Retrieve pointer that can acess all the elements of arrays stored in a std::vector
是否可以有一个指向向量使用的连续缓冲区的指针?
例如(请见下文),这里 std::vector<unsigned char*> vec
包含两个不同大小的 unsigned char*
指针。我需要一个指向该向量中所有推送数据的缓冲区指针。我猜这是可能的,因为标准保证向量使用连续的内存缓冲区,对吧?
P.S。我在这个例子中使用的打印矢量元素的两种方法是否合适? (两个 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];
buffer
是否指向vec
中的所有数据? 即 buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?
Does buffer
point to all data in vec
? i.e. buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?
没有。它指向存储在向量的第一个元素中的数组的开头。
Are the two ways of printing the elements of the vector I use in this example fine?
它们不是,那些数组不是空终止的,它们不能打印为字符串。
Is it possible to have a pointer that points to the contiguous buffer that is used by a vector?
是的,有可能。
如果您想要一个可以正确访问向量中所有数据的指针,包括您想要的 unsigned char 数组成员的各个元素:
unsigned char **buffer = vec.data();
访问权限:
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
请注意,我使用循环来访问 data1
的每个元素,而不是简单地将其视为字符串,这是因为它不是字符串,也就是空终止字符数组。
不用说,您将需要知道每个数组中存储了多少个元素。
或者你可以空终止它们:
unsigned char* data1 = new unsigned char[4];
//...
data1[3] = '[=12=]';
和
unsigned char* data2 = new unsigned char[2];
//...
data2[1] = '[=13=]';
这里像字符串一样打印它们:
std::cout << buffer[0];
std::cout << buffer[1];
使用空终止符有一个额外的好处,即允许您使用 strlen((char*)buffer[0])
.
随时知道数组的大小
您需要向量上的 data()
方法。它将 return 指向数据的指针,假设向量大小大于零。如果它是零,那么 data()
将 return 某些东西 但使用它是未定义的。
是否可以有一个指向向量使用的连续缓冲区的指针?
例如(请见下文),这里 std::vector<unsigned char*> vec
包含两个不同大小的 unsigned char*
指针。我需要一个指向该向量中所有推送数据的缓冲区指针。我猜这是可能的,因为标准保证向量使用连续的内存缓冲区,对吧?
P.S。我在这个例子中使用的打印矢量元素的两种方法是否合适? (两个 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];
buffer
是否指向vec
中的所有数据? 即 buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?
Does
buffer
point to all data invec
? i.e.buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x
?
没有。它指向存储在向量的第一个元素中的数组的开头。
Are the two ways of printing the elements of the vector I use in this example fine?
它们不是,那些数组不是空终止的,它们不能打印为字符串。
Is it possible to have a pointer that points to the contiguous buffer that is used by a vector?
是的,有可能。
如果您想要一个可以正确访问向量中所有数据的指针,包括您想要的 unsigned char 数组成员的各个元素:
unsigned char **buffer = vec.data();
访问权限:
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
请注意,我使用循环来访问 data1
的每个元素,而不是简单地将其视为字符串,这是因为它不是字符串,也就是空终止字符数组。
不用说,您将需要知道每个数组中存储了多少个元素。
或者你可以空终止它们:
unsigned char* data1 = new unsigned char[4];
//...
data1[3] = '[=12=]';
和
unsigned char* data2 = new unsigned char[2];
//...
data2[1] = '[=13=]';
这里像字符串一样打印它们:
std::cout << buffer[0];
std::cout << buffer[1];
使用空终止符有一个额外的好处,即允许您使用 strlen((char*)buffer[0])
.
您需要向量上的 data()
方法。它将 return 指向数据的指针,假设向量大小大于零。如果它是零,那么 data()
将 return 某些东西 但使用它是未定义的。