【问题标题】:To use vector declared inside a function in another function在另一个函数中使用在函数内部声明的向量
【发布时间】:2017-05-30 10:40:05
【问题描述】:
#include <iostream>
#include <vector>

using namespace std;

void em(vector<int>* pv );

int main()
{
    vector<int>* pv = NULL;
    em(pv);
    printf("%d %d", (*pv)[0], (*pv)[1]); // Error!

}

void em(vector<int>* pv)
{
    vector<int> V;
    pv = &V;

    V.push_back(1);
    V.push_back(2);
    printf("%d %d\n", V[0], V[1]);
    // 1 2  OK!

}

我知道向量是动态分配。 但是,当函数结束时,内存似乎被释放了。 我想使用指针来使用在另一个函数中的函数内声明的向量。 我该怎么办?

【问题讨论】:

  • 使用std::vector&lt;int&gt; em() 并返回向量。 (N)RVO 将删除看似副本的内容。
  • 或者传递对 em 的引用。
  • 我需要从函数 A 创建一个向量并在 B 中使用该向量。所以我尝试使用指针进行通信。
  • @StackQ 您不需要使用指针,只需返回向量本身,如果您担心性能,请打开编译器优化并查看输出程序集以确保对象未被复制.顺便说一句,如果你愿意允许传递一个 ptr,你有什么反对在 main 而不是 em 的范围内创建向量?

标签: c++ memory vector


【解决方案1】:

你可以在你的函数中返回一个向量。在 C++11 中,它移动值而不是复制它们,因此按值返回不是问题。

int main()
{
    vector<int> pv = em();
    printf("%d %d", pv[0], pv[1]);
}

vector<int> em()
{
    vector<int> V;
    ...
    return V;
}

也可以参考

int main()
{
    vector<int> pv;
    em(pv);
    printf("%d %d", pv[0], pv[1]);
}

void em(vector<int> &V)
{
    V.push(1);
    V.push(2);
    ...
}

【讨论】:

  • 我想使用 ref 调用
  • 在C++11中是移动而不是复制,所以按值返回向量没有问题。
  • 对于 ref 调用,你可以将向量传递给 em 作为参考,然后对其进行操作。我编辑了答案以包含一个示例。
  • @Eric - StackQ 清楚地将“引用调用”解释为“使用指针”。
  • A类有一个向量指针作为变量,我想在B函数中使用A类的向量指针进行动态赋值。
【解决方案2】:

函数em的变量向量v是临时的,当函数em返回时,变量向量v的生命将结束,内存将被删除。您可以传递对函数 em 的引用或在 em 中新建一个指针。 如果您不想传递指针,您可以阅读以下代码以供参考。

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

void em(vector<int>*& pv );

int main()
{
    vector<int>* pv = NULL;
    em(pv);
    printf("%d %d", (*pv)[0], (*pv)[1]); 
    delete pv;
    pv = NULL;

}

void em(vector<int>*& pv)
{
    vector<int>* V = new vector<int>;
    pv = V;

    V->push_back(1);
    V->push_back(2);
    printf("%d %d\n", V->at(0), V->at(1));
    // 1 2  OK!

}

【讨论】:

    【解决方案3】:

    在堆栈上创建的变量只与函数一样长。当 em 返回时,向量后面的内存被释放。

    为了使函数持续时间更长,您需要返回一个完整的对象,或者从堆中分配向量。

    vector<int> em()
    {
        vector<int> V;
        pv = &V;
    
        V.push_back(1);
        V.push_back(2);
        printf("%d %d\n", V[0], V[1]);
        // 1 2  OK!
       return V;
    }
    

    void em(vector<int>* pv)
    {
       pv = new vector<int>();
       vector<int> & V =*pv;
       V.push_back(1);
       V.push_back(2);
       printf("%d %d\n", V[0], V[1]);
    // 1 2  OK!
     // main needs to delete pv
    }
    

    【讨论】:

      【解决方案4】:
      #include <iostream>
      #include <vector>
      
      using namespace std;
      
      vector<int>* em( );
      void P(vector<int>*);
      
      int main()
      {
          vector<int>* pv = NULL;
          pv = em();
      
          printf("Second : %d %d\n", (*pv)[0], (*pv)[1]); 
      
          P(pv);
      
      }
      
      vector<int>* em()
      {
          //vector<int> V;
          vector<int>* pV = new vector < int > ;
      
      
          (*pV).push_back(1);
          (*pV).push_back(2);
          printf("First: %d %d\n", (*pV)[0], (*pV)[1]);
          // 1 2  OK!
          return pV;
      
      }
      
      void P(vector<int>* pv)
      {
          printf("Third : %d %d \n", (*pv)[0], (*pv)[1]);
      }
      

      这行得通。

      --------结果-----

      第一个:1 2

      秒:1 2

      第三:1 2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-31
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-28
        相关资源
        最近更新 更多