gpt4 book ai didi

重载 I/O 运算符时的 C++ unicode 框字符绘制

转载 作者:行者123 更新时间:2023-11-28 07:50:20 25 4
gpt4 key购买 nike

我刚刚读了Joel's blog post on unicode , 并在控制台中找到我想用来绘制框的字符 in this pdf from the unicode website .

直接使用 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;
}

让它成为我的数据模型的 friend ...

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;

如果有人能找到修复程序,那就太好了。感谢阅读。

最佳答案

我会将 gfxString 更改为 std::strings 的 vector (甚至是 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* 的数组。

关于重载 I/O 运算符时的 C++ unicode 框字符绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com