矢量>到c++中的数组

时间:2021-11-13 21:22:04

I've read plenty of SO threads on vector to array conversion, but how would a vector<vector<?>> be converted to a singly dimensioned array? I recently discovered the vector's data function; could that be used somehow?

我读过很多关于向量到数组转换的线程,但是向量如何 >可以转换成单维数组吗?我最近发现了向量的数据函数;这能用吗? >

1 个解决方案

#1


4  

You're on the right track with the .data() member function, but that would give you an array of objects of type std::vector<T>, not an array of objects of type T. To truly flatten a nested vector you will need to do it yourself. Something like this would probably do the trick.

您使用.data()成员函数在正确的轨道上,但是这会给您一个std类型的对象数组::vector ,而不是一个类型的对象数组。像这样的东西可能会起作用。

// 1. Compute the total size required.
int total_size = 0;
for (auto& vec : vectors) total_size += vec.size();

// 2. Create a vector to hold the data.
std::vector<T> flattened;
flattened.reserve(total_size);

// 3. Fill it
for (auto& vec : vectors)
    for (auto& elem : vec)
        flattened.push_back(elem);

// 4. Obtain the array
auto ptr = flattened.data();

For older compilers, you can iterate through the vectors like so

对于较老的编译器,可以像这样迭代这些向量

for (std::vector<std::vector<T> >::iterator iter = vectors.begin();
     iter != vectors.end(); ++iter) {
    for (std::vector<T>::iterator iter2 = iter->begin();
         iter2 != iter->end(); ++iter2) {
        flattened.push_back(*iter2);
    }
}

Or just use plain old indices and the .size() member function.

或者只使用普通的旧索引和.size()成员函数。

Internally, std::vector holds on to a pointer to its elements, and so the outermost data() must conceptually be treated like an array of pointers, not a 2D array. Therefore we have to manually walk through and flatten it.

在内部,std::vector持有指向其元素的指针,因此最外层的数据()在概念上必须被视为指针数组,而不是二维数组。因此,我们必须手动走过去,把它压平。

#1


4  

You're on the right track with the .data() member function, but that would give you an array of objects of type std::vector<T>, not an array of objects of type T. To truly flatten a nested vector you will need to do it yourself. Something like this would probably do the trick.

您使用.data()成员函数在正确的轨道上,但是这会给您一个std类型的对象数组::vector ,而不是一个类型的对象数组。像这样的东西可能会起作用。

// 1. Compute the total size required.
int total_size = 0;
for (auto& vec : vectors) total_size += vec.size();

// 2. Create a vector to hold the data.
std::vector<T> flattened;
flattened.reserve(total_size);

// 3. Fill it
for (auto& vec : vectors)
    for (auto& elem : vec)
        flattened.push_back(elem);

// 4. Obtain the array
auto ptr = flattened.data();

For older compilers, you can iterate through the vectors like so

对于较老的编译器,可以像这样迭代这些向量

for (std::vector<std::vector<T> >::iterator iter = vectors.begin();
     iter != vectors.end(); ++iter) {
    for (std::vector<T>::iterator iter2 = iter->begin();
         iter2 != iter->end(); ++iter2) {
        flattened.push_back(*iter2);
    }
}

Or just use plain old indices and the .size() member function.

或者只使用普通的旧索引和.size()成员函数。

Internally, std::vector holds on to a pointer to its elements, and so the outermost data() must conceptually be treated like an array of pointers, not a 2D array. Therefore we have to manually walk through and flatten it.

在内部,std::vector持有指向其元素的指针,因此最外层的数据()在概念上必须被视为指针数组,而不是二维数组。因此,我们必须手动走过去,把它压平。