C++:从 vector<vector<T>> 获取 T**

山姆

我正在与需要类型为 的二维数组的 C 库交互float**,其大小在运行时确定。我想使用诸如 std::vector 之类的 STL 容器来管理此内存,但vector<vector<float>>::data()给出的是vector<float>*,而不是float**. 我能做什么?

加利克

你可以创建一个新的载体保持指针由内部管理的所有内部阵列矢量您的载体的载体

void old_c_function(float** floats, std::size_t X, std::size_t Y)
{
    for(auto x = 0U; x < X; ++x)
        for(auto y = 0U; y < Y; ++y)
            std::cout << "[" << x << ", " << y << "] = " << floats[x][y] << '\n';
}

int main()
{
    std::vector<std::vector<float>> v =
    {
        {1.2, 3.4, 5.6},
        {7.8, 9.0, 1.2},
    };

    // create a new vector to hold the pointers to the arrays
    // managed by the internal vectors
    std::vector<float*> v_ptrs;
    v_ptrs.reserve(v.size());

    // add the addresses of all the arrays to the new vector
    std::for_each(std::begin(v), std::end(v),
        [&v_ptrs](auto& v){ v_ptrs.push_back(v.data()); });

    // call your legacy function using your pointer vector
    old_c_function(v_ptrs.data(), v.size(), v.front().size());
}

输出:

[0, 0] = 1.2
[0, 1] = 3.4
[0, 2] = 5.6
[1, 0] = 7.8
[1, 1] = 9
[1, 2] = 1.2

笔记:

显然,如果你改变你的向量,你将需要重建你的指针向量,因为地址可能会改变。

您可以通过一些包装函数即时重建它,如下所示:

void new_wrapper_function(std::vector<std::vector<float>>& v)
{
    // create a new vector to hold the pointers to the arrays
    // managed by the internal vectors
    std::vector<float*> v_ptrs;
    v_ptrs.reserve(v.size());

    // add the addresses of all the arrays to the new vector
    std::for_each(std::begin(v), std::end(v)
        [&v_ptrs](auto& v){ v_ptrs.push_back(v.data()); });

    // call your legacy function using your pointer vector
    old_c_function(v_ptrs.data(), v.size(), v.front().size());
}

或者(我最喜欢的)构建一个包装器class来封装这两个向量,并在主向量在其维度之一的容量增加时更新指针向量。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++-将vector <T>转换为vector <array <T >>

C ++ vector <T> v,vector <T> * v,vector <T *> vec,哪个最快(或最高效)?

C ++ vector <T>,其中T是类内部的结构

将vector <T *>移动到vector <const T *>

将std :: vector <char>转换为std :: vector <T>转换c ++

如何从 Vector[T] 推断参数 T

填充std :: vector时出现“ vector <T>太长”错误

shared_ptr <T>到shared_ptr <T const>,vector <T>到vector <T const>

在自定义Vector <T>类中实现push_back(T && c)

C ++-shared_ptr <向量<T >>与vector <shared_ptr <T >>

在现代 C++ 中将 std::array<std::array<T,N>> 转换为 std::vector<T>

C ++:从vector <byte>的任何位置获取int

在不获取所有权的情况下访问vector <unique_ptr <T >>中的对象T

模板函数中的输出vector <T>

缺少System.Numerics.Vectors.Vector <T>

为什么Vector <T> .Count是静态的?

用std :: vector <T *>控制内存

将 std::vector<T> 转换为 char *

将vector <shared_pt <T >>复制到vector <shared_ptr <const T >>(不同情况下)C ++

将矩阵`shared_ptr <vector <vector <T <>>>>的行((* sp)[i])传递给接受`shared_ptr <vector <T >>`的函数

T-> std :: vector <T>的模板专业化

如何将std :: vector <std :: reference_wrapper <T>>转换为std :: vector <T>

const std :: vector <T>和std :: vector <T> const有什么区别?

是否有一个比vector <vector <T >>更自然的方式来表示T的矩阵?

如何将vector <unique_ptr <T >>复制到独立vector <T *>

C++ - 在模板方法中将<T> 对象添加到 std::vector<abstract C> 的正确方法,无需复制 T

逐行从文件读取到 vector<T> 对二进制数据 C++ 不正确

C ++ CLI:cliext :: vector <T>作为公共类函数的返回类型?

C ++如何通过套接字发送const std :: vector <std :: uint8_t>?