• <legend id='J97tT'><style id='J97tT'><dir id='J97tT'><q id='J97tT'></q></dir></style></legend>
    <i id='J97tT'><tr id='J97tT'><dt id='J97tT'><q id='J97tT'><span id='J97tT'><b id='J97tT'><form id='J97tT'><ins id='J97tT'></ins><ul id='J97tT'></ul><sub id='J97tT'></sub></form><legend id='J97tT'></legend><bdo id='J97tT'><pre id='J97tT'><center id='J97tT'></center></pre></bdo></b><th id='J97tT'></th></span></q></dt></tr></i><div id='J97tT'><tfoot id='J97tT'></tfoot><dl id='J97tT'><fieldset id='J97tT'></fieldset></dl></div>
    <tfoot id='J97tT'></tfoot>
    • <bdo id='J97tT'></bdo><ul id='J97tT'></ul>

        <small id='J97tT'></small><noframes id='J97tT'>

      1. C++向量&lt;vector&lt;double&gt;&gt;加倍**

        时间:2023-09-15

        1. <tfoot id='Kn3we'></tfoot>
          <i id='Kn3we'><tr id='Kn3we'><dt id='Kn3we'><q id='Kn3we'><span id='Kn3we'><b id='Kn3we'><form id='Kn3we'><ins id='Kn3we'></ins><ul id='Kn3we'></ul><sub id='Kn3we'></sub></form><legend id='Kn3we'></legend><bdo id='Kn3we'><pre id='Kn3we'><center id='Kn3we'></center></pre></bdo></b><th id='Kn3we'></th></span></q></dt></tr></i><div id='Kn3we'><tfoot id='Kn3we'></tfoot><dl id='Kn3we'><fieldset id='Kn3we'></fieldset></dl></div>
                <bdo id='Kn3we'></bdo><ul id='Kn3we'></ul>
                <legend id='Kn3we'><style id='Kn3we'><dir id='Kn3we'><q id='Kn3we'></q></dir></style></legend>

                  <tbody id='Kn3we'></tbody>

                  <small id='Kn3we'></small><noframes id='Kn3we'>

                  本文介绍了C++向量&lt;vector&lt;double&gt;&gt;加倍**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试传递 vector 类型的变量.> 到函数 F(double ** mat, int m, int n).F 函数来自另一个库,所以我无法更改它.有人可以给我一些提示吗?谢谢.

                  I'm trying to pass a variable of type vector<vector<double> > to a function F(double ** mat, int m, int n). The F function comes from another lib so I have no option of changing it. Can someone give me some hints on this? Thanks.

                  推荐答案

                  vector>double** 是完全不同的类型.但是可以在另一个存储一些双指针的向量的帮助下提供这个函数:

                  vector<vector<double>> and double** are quite different types. But it is possible to feed this function with the help of another vector that stores some double pointers:

                  #include <vector>
                  
                  void your_function(double** mat, int m, int n) {}
                  
                  int main() {
                      std::vector<std::vector<double>> thing = ...;
                      std::vector<double*> ptrs;
                      for (auto& vec : thing) {
                          //   ^ very important to avoid `vec` being
                          // a temporary copy of a `thing` element.
                          ptrs.push_back(vec.data());
                      }
                      your_function(ptrs.data(), thing.size(), thing[0].size());
                  }
                  

                  这样做的原因之一是因为 std::vector 保证所有元素都连续存储在内存中.

                  One of the reasons this works is because std::vector guarantees that all the elements are stored consecutivly in memory.

                  如果可能,请考虑更改函数的签名.通常,矩阵在内存中线性排列.这意味着,访问矩阵元素可以使用一些类型为 double* 的基指针 p 来完成左上角系数和一些基于行和列的计算线性索引,例如 p[row*row_step+col*col_step] 其中 row_stepcol_step 是依赖于布局的偏移量.标准库并没有真正为这些类型的数据结构提供任何帮助.但是你可以尝试使用 Boost 的 multi_array 或 GS​​L 的 multi_span 来帮助解决这个问题.

                  If possible, consider changing the signature of your function. Usually, matrices are layed out linearly in memory. This means, accessing a matrix element can be done with some base pointer p of type double* for the top left coefficient and some computed linear index based on row and columns like p[row*row_step+col*col_step] where row_step and col_step are layout-dependent offsets. The standard library doesn't really offer any help with these sorts of data structures. But you could try using Boost's multi_array or GSL's multi_span to help with this.

                  这篇关于C++向量&lt;vector&lt;double&gt;&gt;加倍**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:编译器不推导出模板参数(映射 std::vector -> st 下一篇:默认情况下,为什么 C++ 不检测何时使用 [] 运算

                  相关文章

                  最新文章

                1. <i id='amC6R'><tr id='amC6R'><dt id='amC6R'><q id='amC6R'><span id='amC6R'><b id='amC6R'><form id='amC6R'><ins id='amC6R'></ins><ul id='amC6R'></ul><sub id='amC6R'></sub></form><legend id='amC6R'></legend><bdo id='amC6R'><pre id='amC6R'><center id='amC6R'></center></pre></bdo></b><th id='amC6R'></th></span></q></dt></tr></i><div id='amC6R'><tfoot id='amC6R'></tfoot><dl id='amC6R'><fieldset id='amC6R'></fieldset></dl></div>

                    <bdo id='amC6R'></bdo><ul id='amC6R'></ul>

                      <small id='amC6R'></small><noframes id='amC6R'>

                      <legend id='amC6R'><style id='amC6R'><dir id='amC6R'><q id='amC6R'></q></dir></style></legend>
                      <tfoot id='amC6R'></tfoot>