【问题标题】:Setting-up a 2D array using std::Vector使用 std::Vector 设置二维数组
【发布时间】:2020-10-02 13:02:23
【问题描述】:

我已经从this question 改进了我的数组,以便在一个类中创建,我还使用std::vector 对其进行了优化,因此它不是分配在堆上而是在堆栈上,因此它更快并且我也不必手动管理内存。

但我无法创建一个可以为元素设置值的函数。我收到了类似expression must have pointer-to-object type 的错误(当我尝试将我的元素声明为int cellan array may not have element of this type (当我尝试将其声明为int cell[][] 时),当我尝试声明它时出现分段错误像这样int *cell[]

这是我的代码

.hpp

#pragma once
#include <iostream>
#include <vector>
class myPlan 
{ 
    int slots; 
    int rows = 3;
    int cell[][];
  
public: 

    myPlan(int slots);  
    
    void setCellValue(int row, int slot, int value)
 };

.cpp

#include "myPlan.hpp"

myPlan::myPlan(int slots) 
{ 
   this->slots = slots;

   std::vector<std::vector<int> > cell(STATUS_N);
   for (int i = 0; i < STATUS_N; i++) 
   { 
        // declare  the i-th row to size of column 
        cell[i] = std::vector<int>(slots); 
   }

  //Print the array
    for (int i = 0; i < STATUS_N; i++) { 
        for (int j = 0; j < cell[i].size(); j++) 
         {
            cell[i][j] = 0;
            std::cout << cell[i][j] << " "; 
         }
        std::cout << "\n"; 
    } 
} 


void myPlan::setCellValue(int row, int slot, int value)
{
    cell[row][slot] = value;
}

ma​​in.cpp

#include "myPlan.hpp"

int main() 
{ 
    myPlan plan(N_PLAN); 
    
    plan.setCellValue(0,2,42);
}

非常感谢您的任何帮助 附:我希望这个比this array更优化,如果有更好的优化版本,我会很高兴了解它。

【问题讨论】:

  • " 所以它不是分配在堆上而是分配在堆栈上,因此速度更快。"抱歉,std::vector 确实将其元素存储在堆上。也许您正在寻找std::array
  • 向量从堆中为其元素分配内存。向量本身可能在堆栈上。这与指针的情况完全相同,指针本身可能在堆栈上,但它指向的内存在堆上。
  • 请一字不差地包含错误信息,请勿总结或解释它
  • int cell[][]; -- 这应该做什么?这不是有效的 C++。

标签: c++ arrays optimization vector


【解决方案1】:

所以这就是我认为的根本问题,它与数组或向量无关。您只是错误地处理了您的类变量。这是你的课

class myPlan 
{ 
    int slots; 
    int rows = 3;
    int cell[][];

int cell[][]; 不是合法的 C++,但我们会让它过去的。关键是您的班级中有某种二维数组,称为cell

现在这是你的构造函数

myPlan::myPlan(int slots) 
{ 
   this->slots = slots;

   std::vector<std::vector<int> > cell(STATUS_N);
   for (int i = 0; i < STATUS_N; i++) 
   { 
        // declare  the i-th row to size of column 
        cell[i] = std::vector<int>(slots); 
   }

现在您在这里声明了一个名为cell 的二维向量。但是(这就是重点)这与您班级中的cell 相同,它是一个完全独立的变量,恰好具有相同的名称。就像函数内声明的任何变量一样,它在函数退出后将不再存在。

这是应该怎么做的。

class myPlan 
{ 
    int slots; 
    int rows = 3;
    std::vector<std::vector<int> > cell;
    ...

myPlan::myPlan(int slots) 
{ 
   this->slots = slots;

   cell.resize(STATUS_N);
   for (int i = 0; i < STATUS_N; i++) 
   { 
        // declare  the i-th row to size of column 
        cell[i] = std::vector<int>(slots); 
   }

看到区别了吗?我没有声明新的cell 变量,我只是调整了类中声明的变量的大小。

【讨论】:

    【解决方案2】:

    据我了解您的问题,您需要一个数组数组(多个插槽),其大小在编译时是已知的。而且你更喜欢它在堆栈上。

    我鼓励您使用 std::array 并忘记 C 风格的数组([] - 这些家伙)。或者更好地了解它们之间的区别并自行决定。

    有一个示例如何使用它。该类必须被模板化以在编译时提供数组大小的参数。它非常简单,但如果您不需要它,您可以删除类定义之前的模板行,并将RowNumberSlotSize 替换为您在编译时已知的定义或常量。

    此解决方案仅在编译时已知大小的情况下才有效。如果您想提供行数作为myPlan 构造函数参数,那么您需要动态分配内存并使用std::vector 或类似的东西。

    #include <iostream>
    #include <array>
    
    template<std::size_t RowNumber, std::size_t SlotSize>
    class myPlan { 
        std::array<std::array<int, SlotSize>, RowNumber> cells;
      
    public: 
        myPlan() : 
        cells{} // initialize array with zeros
        {  
            //Print the array
            for (int i = 0; i < cells.size(); i++) { 
                for (int j = 0; j < cells[i].size(); j++) 
                {
                    std::cout << cells[i][j] << " "; 
                }
                std::cout << "\n"; 
            }       
        }
        
        void setCellValue(int row, int slot, int value) {
            cells[row][slot] = value;
        }
    };
    
    #define N_PLAN 4
    #define STATUS_N 3
    
    int main() 
    { 
        myPlan<N_PLAN, STATUS_N> plan;    
        plan.setCellValue(0,2,42);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2012-04-05
      相关资源
      最近更新 更多