在矢量中特定位置的unique_ptr矢量

A vector of unique_ptr at specific positions in vector

本文关键字:unique ptr 矢量 位置 定位      更新时间:2023-10-16

我有一个名为Grid的类,它由Cells组成。每个单元格都可以有自己的格式(概念类似于MS Excel(。

网格中的格式保存在拥有所有格式的向量std::vector<std::unique_ptr<CellFormat>> m_CellFormatTable中,因此每当我需要读取 Cells 格式时,我都会从向量中读取它,每当有变化时,它都会报告回向量。抱歉,我对 C++11 标准很陌生,所以我的想法可能是错误的。

由于网格是一个矩阵,当单元格的格式发生变化时,每个单元格都属于矩阵的不同部分,因此它应该反映在矩阵的正确部分,即在向量中正确定位(CellFormatTable(。因此,在此阶段我不能使用向量的push_back方法。

类:

struct CellFormat
{
    wxFont m_Font;
    wxColor m_BackgroundColor, m_TextColor;
    int m_HorizontalAlignment, m_VerticalAlignment;
    CellFormat(Grid* ws) {
        m_BackgroundColor = ws->GetDefaultCellBackgroundColour();
        m_TextColor=ws->GetDefaultCellTextColour();
        int horizontal = 0, vertical = 0;
        ws->GetDefaultCellAlignment(&horizontal, &vertical);
    }
    CellFormat(const CellFormat& other) {
        m_Font = other.m_Font;
        m_BackgroundColor = other.m_BackgroundColor;
        m_TextColor = other.m_TextColor;
        m_HorizontalAlignment = other.m_HorizontalAlignment;
        m_VerticalAlignment = other.m_VerticalAlignment;
    }
    CellFormat& operator=(const CellFormat& other) {
        if (this == &other) return *this;
        m_Font = other.m_Font;
        m_BackgroundColor = other.m_BackgroundColor;
        m_TextColor = other.m_TextColor;
        m_HorizontalAlignment = other.m_HorizontalAlignment;
        m_VerticalAlignment = other.m_VerticalAlignment;
        return *this;
    }
};

在网格中。

class Grid{
    std::vector<std::unique_ptr<CellFormat>> m_CellFormatTable;
    //
    CellFormat* GetCellFormat(int row, int column);
    void SetCellFormat(int row, int column, CellFormat format);
    void ApplyCellFormat(int row, int column, const CellFormat* format);
    CellFormat* CreateCellFormat(int row, int column);
    //rest is omitted
}

在网格中.cpp

Grid(some arguments){
    m_CellFormatTable.resize(nrows*ncols);
    //rest is omitted
}
CellFormat* Grid::GetCellFormat(int row, int column)
{
    int ncols= GetNumberCols();
    return m_CellFormatTable[row*ncols+ column].get();
}
void Grid::SetCellFormat(int row, int column, CellFormat other)
{
    CellFormat* format = GetCellFormat(row, column);
    if (format == 0) format = CreateCellFormat(row, column);
    *format = other;
}
void Grid::ApplyCellFormat(int row, int column, const CellFormat * format)
{
    if (format == 0) {
        int ncols= GetNumberCols();
        //Set everything to default values
        //Omitted
        m_CellFormatTable[row*ncols+ column].reset();
    }
    else {
        wxColor bgcolor = format->m_BackgroundColor;
        if (bgcolor.IsOk()) SetCellBackgroundColour(row, column, bgcolor);
        SetCellTextColour(row, column, format->m_TextColor);
        SetCellFont(row, column, format->m_Font);
        SetCellAlignment(row, column, format->m_HorizontalAlignment, format->m_VerticalAlignment);
    }
}
CellFormat* Grid::CreateCellFormat(int row, int column)
{
    int ncols= GetNumberCols();
    CellFormat* format = new CellFormat(this);
    m_CellFormatTable.emplace(m_CellFormatTable.begin() + row*ncols+ column, std::move(format));
    return format;
}

每当我格式化单元格时,比如说它的背景颜色发生了变化,我都会使用以下尝试:

CellFormat* format = ws->GetCellFormat(row, col);
if (format == 0) format = ws->CreateCellFormat(row, col);
if (ChangeFillColor) {
    ws->SetCellBackgroundColour(row, col, m_LastChosenFillColor);
    format->m_BackgroundColor = m_LastChosenFillColor;
}

代码在format->m_BackgroundColorApplyCellFormat函数失败,因为应该是 Cell 背景颜色的颜色无效。这告诉我,大多数而且很可能CreateCellFormat没有将CellFormat放在正确的位置。我尝试使用insert而不是emplace但编译器(VS 2015(抱怨我所有的尝试。

任何想法都值得赞赏。

你有几个问题。
一种是你添加一个CellFormat*但你的向量存储unique_ptr;所以你需要std::make_unique新格式。

问题:您确定需要指针向量而不是对象吗?

另一种是,您假定向量具有所有单元格的所有数据,如果尚未设置,则0。这是不对的。矢量只有您"推动"或"放置"的元素数量。
假设您已经"推送"了单元格 (0,0( 的格式。现在您要设置 (5,2( 的格式,即(假设您有 10 列(向量中的第 52 个元素,但您只有一个。所以vector[51]是未定义的(vector.at(51)会引发错误(。
首先添加所有单元格格式,一些值 = 0 表示尚未设置。或者重新考虑您的策略。

顺便说一下,您可以使用wxGridCellAttr,它提供了您自己编码的内容。

从您使用unique_ptr(而不是对象(的vector的事实来看,我推断并非矩阵的所有元素实际上都被占用了。在这种情况下,最好使用对象(而不是unique_ptr s(的std::map(如果矩阵非常大,则std::unordered_map(。

template<typename T>
struct grid
{
    using index = std::pair<unsigned, unsigned>;
    // insert element if not already present
    // returns if insertion occurred
    template<typename...Args>
    bool insert(index const&i, Args&&...args)
    {
        return data.emplace(std::forward<Args>(args)...).second;
    }
    // remove element (if it exists)
    void remove(index const&i)
    {
        data.erase(i);
    }
    // get pointer to element, may be nullptr
    T* get(index const&i)
    {
        auto it = data.find(i);
        return it==data.end() ?
            nullptr : std::addressof(*it);
    }
  private:
    std::map<index,T> data;
};

我看到您的代码在代码的这一部分失败的原因:

CellFormat* format = ws->GetCellFormat(row, col);
if (format == 0) format = ws->CreateCellFormat(row, col);
if (ChangeFillColor) {
    ws->SetCellBackgroundColour(row, col, m_LastChosenFillColor);
    format->m_BackgroundColor = m_LastChosenFillColor;
}

是由于类的定义方式:

class Grid{
    std::vector<std::unique_ptr<CellFormat>> m_CellFormatTable;
    //
    CellFormat* GetCellFormat(int row, int column);
    void SetCellFormat(int row, int column, CellFormat format);
    void ApplyCellFormat(int row, int column, const CellFormat* format);
    CellFormat* CreateCellFormat(int row, int column);
    //rest is omitted
}

默认情况下,您的类将其成员和函数设置为private:

将您的类更改为以下内容:

class Grid {
public:
    typedef std::vector<std::unique_ptr<CellFormat>> Format;
private:
    Format m_CellFormatTable;
public:
    CellFormat* getCellFormat( int row, int column );
    void        setCellFormat( int row, int column, const CellFormat& format );
    void        applyCellFormat( int row, int column, const CellFormat& format );
   // Add This Function If Needed
   Format getCellFormatTable() const { return m_CellFormatTable; }
};

因此,类的成员函数声明为 public: 然后,外部和非友元对象现在可以访问此类的成员函数,并能够通过 get 方法返回数据结构。

感谢大家的有用评论和帖子。最后,它按预期工作。

正如 Ripi2 所建议的那样,向量中的CellFormat对象没有初始化,因此在构造函数中我初始化了它们。这里也没有介绍,我在代码中的某个地方有一个未初始化的对象向量,所以也更正了那部分。

虽然遍历网格的所有行和列并创建默认格式不是最好的主意,但对我来说,未来的工作将是 Walter 的建议,即使用集合。

Grid(some arguments){
    for (int i = 0; i < nrows*ncols; i++) {
        m_CellFormatTable.emplace_back(new CellFormat(this));
    }
   //Rest is omitted
}

还更正了以下代码:

CellFormat* Grid::CreateCellFormat(int row, int column)
{
    int ncols = GetNumberCols();
    CellFormat* format = new CellFormat(this);
    std::unique_ptr<CellFormat> ptr(format);
    m_CellFormatTable.emplace(m_CellFormatTable.begin() + row*ncols + column,std::move(ptr));
    return format;
}

跟踪格式的一种方法是:

CellFormat* format = ws->GetCellFormat(i, j);
if (ChangeFillColor) {
    ws->SetCellBackgroundColour(i, j, m_LastChosenFillColor);
    format->m_BackgroundColor = m_LastChosenFillColor;
}