【问题标题】:Duplicate Struct in C++C ++中的重复结构
【发布时间】:2016-06-28 14:55:09
【问题描述】:

我怎样才能将一个struct 复制到另一个。目前,以下代码可以编译,但在运行时不断崩溃。有没有更好的方法来做到这一点?

struct Trip
{
    int startX;
    int startY;
    int endX;
    int endY;
    int suppress
};

struct Feedback
{
    int startX;
    int startY;
    int endX;
    int endY;
    int suppress;
};

vector<Trip> tripList;
vector<Trip> TTMx[288]; 
TTMX[0] = &tripList;
vector<Feedback> Tripfeed[288];

    for(time = 0; time < 288; time++){
            for (int trp=0; trp < tripList.size(); trp++) {

                Tripfeed[time][trp].startX = tripList[trp].startX;
                Tripfeed[time][trp].startY = tripList[trp].startY;
                Tripfeed[time][trp].endX = tripList[trp].endX;
                Tripfeed[time][trp].endY = tripList[trp].endY;
                Tripfeed[time][trp].suppress = tripList[trp].suppress;
    }
    }

【问题讨论】:

  • 你有一个包含 288 个空向量的数组。要将元素添加到一个向量,请使用push_back
  • 如何使用push_back向对象startX添加数据?
  • 什么是tripList?你应该能够在你的 for 循环中使用这个语句Tripfeed[time].push_back(tripList[trp])
  • 如果我消除这个复制程序,我的主要代码运行良好。因此,我不得不假设问题必须与上述代码有关。

标签: c++ object struct copy


【解决方案1】:

Tripfeed 被声明为一个包含 288 个空向量的数组。它们是空的,因为这是向量类的默认构造函数的行为。当您尝试访问这些向量的元素时,由于访问冲突,它会崩溃,因为向量是空的。对于每个向量,您必须在访问其元素之前将其调整为所需的大小。这是正确的代码:

struct Feedback
{
    int startX;
    int startY;
    int endX;
    int endY;
    int suppress;
};

vector<Feedback> Tripfeed[288];

for(time = 0; time < 288; time++)
{

    Tripfeed[time].resize (tripList.size());

    for (int trp=0; trp < tripList.size(); trp++) 
    {
        Tripfeed[time][trp].startX = tripList[trp].startX;
        Tripfeed[time][trp].startY = tripList[trp].startY;
        Tripfeed[time][trp].endX = tripList[trp].endX;
        Tripfeed[time][trp].endY = tripList[trp].endY;
        Tripfeed[time][trp].suppress = tripList[trp].suppress;
    }
}

【讨论】:

  • 感谢一百万!我通常更喜欢 push_back,这样我就不必调整大小,但我应该意识到这一点。
  • 如果您事先知道向量的大小,您将获得更好的性能,因为只有一个内存分配。如果您有 1000 个单独推回的项目,则向量会根据需要不断增长。每次增长它都必须分配更大的内存块,将旧内存复制到新内存,然后删除旧内存。
【解决方案2】:

您的 Tripfeed 声明应该是

    vector<vector<Feedback>> Tripfeed(288, vector<Feedback>(tripList.size(), Feedback()));

【讨论】:

    【解决方案3】:

    如果你有一个不错的编译器,可以使用基于范围的 for 循环:

    #include <algorithm>
    
    // ...
    
    for (std::vector<Feedback>& f : Tripfeed)
    {
      f.resize(tripList.size()); // all vectors in Tripfeed are initially empty
      std::copy(tripList.begin(), tripList.end(), f.begin());
    }
    

    【讨论】:

      【解决方案4】:

      使用 resize() 为向量分配足够的内存。您的代码应该如下所示。但是,我建议使用 2D 向量来声明 Tripfeed(std::vector > Tripfeed)。

      for(int time = 0; time < 288; time++){
          Tripfeed[time].resize(tripList.size());
          for (int trp=0; trp < tripList.size(); trp++) {
              Tripfeed[time][trp].startX = tripList[trp].startX;
              Tripfeed[time][trp].startY = tripList[trp].startY;
              Tripfeed[time][trp].endX = tripList[trp].endX;
              Tripfeed[time][trp].endY = tripList[trp].endY;
              Tripfeed[time][trp].suppress = tripList[trp].suppress;
          }
        }
      

      【讨论】:

        猜你喜欢
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        • 2010-10-07
        • 2010-12-22
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        相关资源
        最近更新 更多