0

我刚刚阅读了Joel 在 unicode 上的博客文章,并在 unicode 网站的这个 pdf 中找到了我想用来在控制台中绘制框的字符。

直接使用 显示这些字符很简单cout,即下面的内容是它应该做的..

    cout << u8"\u256C";

但是,我正在做一些重载,如下面的片段所示,我无法弄清楚如何让框字符正确显示。

我像这样渲染我的数据......

// This is the main rendering code
ostream& operator<<(ostream& os, const DataGrid& dg)
{
    for(auto row : dg.data)
    {
        string tmp; //Make empty string
        for(auto col : row)
            tmp.append(   1, dg.gfxString[col]  );
        os << tmp << endl;
    }

    return os;
}

与我的数据模型成为朋友...

class DataGrid
{
public:
    friend ostream& operator<<(ostream& os, const DataGrid& dg);

    //EDIT: rest of class added on request
    DataGrid(Rectangle _rect = Rectangle(Point(0,0), Point(5,5))) :
    rect(_rect),
    data ( vector<vector<p_t>> (_rect.getHeight(), vector<p_t>(_rect.getWidth(), p_t::EMPTY))  ){}

    void addPoint(Point p, p_t type)
    { 
        data[p.getY()][p.getX()] = type;
    }

    void addBorder()
    {
        //Top and bottom
        fill_n(data[0].begin()+1, rect.getWidth()-2, p_t::BORDER_T);
        fill_n(data[rect.getBtm()].begin()+1, rect.getWidth()-2, p_t::BORDER_B);

        //Left and right hand border edges
        for (int nn=1; nn<rect.getHeight()-1; ++nn){
            addPoint(Point(rect.getLeft(), nn), p_t::BORDER_L);
            addPoint(Point(rect.getRight(), nn), p_t::BORDER_R);
        }

        //Corners
        addPoint(rect.getTL(), p_t::BORDER_TL);
        addPoint(rect.getTR(), p_t::BORDER_TR);
        addPoint(rect.getBL(), p_t::BORDER_BL);
        addPoint(rect.getBR(), p_t::BORDER_BR);
    }


private:
    Rectangle rect;
     vector<vector<p_t>> data; //p_t is an enum

    //string gfxString = " abcdefghijklmnop"; //This works fine
    string gfxString = u8"\u256C\u256C\u256C\u256C\u256C\u256C\u256C\u256C"; //Nope     
};

然后尝试使用以下内容渲染它,但会胡言乱语......

DataGrid p = DataGrid(Rectangle(Point(0,0), 40, 10));
p.addBorder();
cout << p;

如果有人能找到解决办法,那就太好了。谢谢阅读。

4

1 回答 1

1

我会将 gfxString 更改为 std::strings 的向量(甚至是 std::array):

// Probably in the final code, these are not all the same value
std::array<std::string, 8> gfxString = {
   { u8"\u256C"
   , u8"\u256C"
   , u8"\u256C"
   , u8"\u256C"
   , u8"\u256C"
   , u8"\u256C"
   , u8"\u256C"
   , u8"\u256C"
   }};

甚至只是一个数组const char*

于 2012-12-19T01:29:47.180 回答