表格的生命周期
libfort表生命周期:
- 创建一个表(使用默认构造函数、复制构造函数或移动构造函数);
- 用数据填充它(运算符<<,运算符[],write_ln...);
- 修改基本表外观;
- 将表转换为字符串表示(to_string)并打印出来。
在表生存期中分配的所有资源将在析构函数中自动释放。
案例:
fort::char_table table;
table << fort::header
<< "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
<< "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr
<< "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr
<< "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr;
std::cout << table.to_string() << std::endl;
输出:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 47.362 |
| 2 | Hamilton | 1:26.373 | 35.02 |
| 3 | Verstappen | 1:26.469 | 29.78 |
+---+------------+----------+-----------+
往表格中填充数据
在每个时刻,libfort
表都有一个当前单元格——在下一次写操作中将向其写入数据。函数set_cur_cell
和<<(fort::endr)
操作可以用来改变当前单元格:
/* 将当前单元格设置为具有坐标(row, col)的单元格 */
void set_cur_cell(size_t row, size_t col)
/* 将当前单元格设置为下一行(line)第一个单元格 */
table << fort::endr;
有很多函数可以用来将数据填充表格。所有用来写数据的函数都是成对的:(function
,function_ln
),其中function
向一组连续的单元格写入数据,function_ln
做同样的事情,并将当前单元格指针移动到下一行(line
)的第一个单元格。
<<操作符
应用于libfort表的操作符(<<
)称为插入操作符。它将字符串插入到表格单元格中。libfort内部使用std::stringstream
将参数转换为字符串。如果要在表中插入某些自定义类型的参数,则应重载std::stringstream::operator<<
。
案例:
fort::char_table table;
table << fort::header
<< "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
<< "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr
<< "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr
<< "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr;
std::cout << table.to_string() << std::endl;
输出:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
[]操作符
此运算符([]
)提供了直接更改特定单元格内容的功能。
案例:
fort::char_table table;
table << fort::header;
table[0][0] = "N"; table[0][1] = "Driver"; table[0][2] = "Time"; table[0][3] = "Avg Speed";
table[1][0] = "1"; table[1][1] = "Ricciardo"; table[1][2] = "1:25.945"; table[1][3] = "47.362";
table[2][0] = "2"; table[2][1] = "Hamilton"; table[2][2] = "1:26.373"; table[2][3] = "35.02";
table[3][0] = "3"; table[3][1] = "Verstappen"; table[3][2] = "1:26.469"; table[3][3] = "29.78";
std::cout << table.to_string() << std::endl;
输出:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
write, write_ln
write
,write_ln
可以向表单元写入任意数量的字符串参数。
案例:
fort::char_table table;
table << fort::header;
table.write_ln("N", "Driver", "Time", "Avg Speed");
table.write_ln("1", "Ricciardo", "1:25.945", "47.362");
table.write_ln("2", "Hamilton", "1:26.373", "35.02");
table.write_ln("3", "Verstappen", "1:26.469", "29.78");
std::cout << table.to_string() << std::endl;
输出:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
range_write, range_write_ln
range_write
,range_write_ln
从由一对迭代器确定的容器中写入数据。
案例:
template <typename InputIt>
bool range_write(InputIt first, InputIt last);
template <typename InputIt>
bool range_write_ln(InputIt first, InputIt last);
fort::char_table table;
table << fort::header;
std::vector<std::string> header = {"N", "Driver", "Time", "Avg Speed"};
std::list<std::string> line_1 = {"1", "Ricciardo", "1:25.945", "47.362"};
std::initializer_list<std::string> line_2 = {"2", "Hamilton", "1:26.373", "35.02"};
std::deque<std::string> line_3 = {"3", "Verstappen", "1:26.469", "29.78"};
table.range_write_ln(header.begin(), header.end());
table.range_write_ln(line_1.begin(), line_1.end());
table.range_write_ln(line_2.begin(), line_2.end());
table.range_write_ln(line_3.begin(), line_3.end());
std::cout << table.to_string() << std::endl;
输出:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
单元格和表格的操作
默认情况下,在libfort表中创建的所有单元格都将具有相同的属性。
要更改单元格属性,应使用函数set_cell_{property_name}
:
bool set_cell_min_width(unsigned value);
bool set_cell_text_align(enum fort::text_align value);
bool set_cell_top_padding(unsigned value);
bool set_cell_bottom_padding(unsigned value);
bool set_cell_left_padding(unsigned value);
bool set_cell_right_padding(unsigned value);
bool set_cell_empty_str_height(unsigned value);
bool set_cell_row_type(enum fort::row_type value);
bool set_cell_content_fg_color(enum fort::color value);
bool set_cell_bg_color(enum fort::color value);
bool set_cell_content_bg_color(enum fort::color value);
bool set_cell_text_style(enum fort::text_style value);
bool set_cell_content_text_style(enum fort::text_style value);
属性可以设置为默认值(用于将来创建的表中的所有单元格)、用于指定表的所有单元格、用于指定某一行的单元格、用于指定某一列中的单元格以及用于指定表的特定单元格。
以下示例说明了这一点:
// 为以后创建的所有表格中的所有单元格设置top padding = 2
fort::table::default_props().set_cell_top_padding(2);
// 为第2行中的所有单元格设置行类型
table.row(2).set_cell_row_type(fort::row_type::header);
// 为第1列中的所有单元格设置文本对齐方式
table.column(1).set_cell_text_align(fort::text_align::center);
// 为单元格(0,0),设置最小宽度
table[0][0].set_cell_min_width(20);
下面是一个简单的完整示例:
fort::char_table table;
// Fill table with data
table << fort::header
<< "Rank" << "Title" << "Year" << "Rating" << fort::endr
<< "1" << "The Shawshank Redemption" << "1994" << "9.5" << fort::endr
<< "2" << "12 Angry Men" << "1957" << "8.8" << fort::endr
<< "3" << "It's a Wonderful Life" << "1946" << "8.6" << fort::endr
<< fort::endr;
table[0][0].set_cell_min_width(20);
table.column(1).set_cell_text_align(fort::text_align::center);
table[3][3].set_cell_left_padding(15);
std::cout << table.to_string() << std::endl;
输出:
+--------------------+--------------------------+------+-------------------+
| Rank | Title | Year | Rating |
+--------------------+--------------------------+------+-------------------+
| 1 | The Shawshank Redemption | 1994 | 9.5 |
| 2 | 12 Angry Men | 1957 | 8.8 |
| 3 | It's a Wonderful Life | 1946 | 8.6 |
+--------------------+--------------------------+------+-------------------+
边框的样式
libfort有许多内置的边框样式。要更改libfort表的边框样式,可以使用set_default_bound_style
、set_bound_style
函数:
/* 更改将创建的所有libfort表的边框样式 */
bool set_default_border_style(struct ft_border_style *style);
/* 更改当前表格的边框样式 */
bool set_border_style(struct ft_border_style *style);
下面是一个创建表格和设置边框样式的简单示例:
fort::char_table table;
/* Set table border style */
table.set_border_style(FT_BASIC_STYLE);
// Fill table with data
table << fort::header
<< "Rank" << "Title" << "Year" << "Rating" << fort::endr
<< "1" << "The Shawshank Redemption" << "1994" << "9.5" << fort::endr
<< "2" << "12 Angry Men" << "1957" << "8.8" << fort::endr
<< "3" << "It's a Wonderful Life" << "1946" << "8.6" << fort::endr
<< fort::separator
<< "4" << "2001: A Space Odyssey" << "1968" << "8.5" << fort::endr
<< "5" << "Blade Runner" << "1982" << "8.1" << fort::endr
<< fort::endr;
table.column(0).set_cell_text_align(fort::text_align::center);
table.column(1).set_cell_text_align(fort::text_align::left);
std::cout << table.to_string() << std::endl;
输出:
+------+--------------------------+------+--------+
| Rank | Title | Year | Rating |
+------+--------------------------+------+--------+
| 1 | The Shawshank Redemption | 1994 | 9.5 |
| 2 | 12 Angry Men | 1957 | 8.8 |
| 3 | It's a Wonderful Life | 1946 | 8.6 |
+------+--------------------------+------+--------+
| 4 | 2001: A Space Odyssey | 1968 | 8.5 |
| 5 | Blade Runner | 1982 | 8.1 |
+------+--------------------------+------+--------+
style可选值
FT_BASIC_STYLE
+------+--------------------------+------+--------+
| Rank | Title | Year | Rating |
+------+--------------------------+------+--------+
| 1 | The Shawshank Redemption | 1994 | 9.5 |
| 2 | 12 Angry Men | 1957 | 8.8 |
| 3 | It's a Wonderful Life | 1946 | 8.6 |
+------+--------------------------+------+--------+
| 4 | 2001: A Space Odyssey | 1968 | 8.5 |
| 5 | Blade Runner | 1982 | 8.1 |
+------+--------------------------+------+--------+
FT_BASIC2_STYLE
+------+--------------------------+------+--------+
| Rank | Title | Year | Rating |
+------+--------------------------+------+--------+
| 1 | The Shawshank Redemption | 1994 | 9.5 |
+------+--------------------------+------+--------+
| 2 | 12 Angry Men | 1957 | 8.8 |
+------+--------------------------+------+--------+
| 3 | It's a Wonderful Life | 1946 | 8.6 |
+------+--------------------------+------+--------+
| 4 | 2001: A Space Odyssey | 1968 | 8.5 |
+------+--------------------------+------+--------+
| 5 | Blade Runner | 1982 | 8.1 |
+------+--------------------------+------+--------+
FT_SIMPLE_STYLE
Rank Title Year Rating
------ -------------------------- ------ --------
1 The Shawshank Redemption 1994 9.5
2 12 Angry Men 1957 8.8
3 It's a Wonderful Life 1946 8.6
------ -------------------------- ------ --------
4 2001: A Space Odyssey 1968 8.5
5 Blade Runner 1982 8.1
FT_PLAIN_STYLE
-------------------------------------------------
Rank Title Year Rating
-------------------------------------------------
1 The Shawshank Redemption 1994 9.5
2 12 Angry Men 1957 8.8
3 It's a Wonderful Life 1946 8.6
-------------------------------------------------
4 2001: A Space Odyssey 1968 8.5
5 Blade Runner 1982 8.1
FT_DOT_STYLE
...................................................
: Rank : Title : Year : Rating :
:......:..........................:......:........:
: 1 : The Shawshank Redemption : 1994 : 9.5 :
: 2 : 12 Angry Men : 1957 : 8.8 :
: 3 : It's a Wonderful Life : 1946 : 8.6 :
:......:..........................:......:........:
: 4 : 2001: A Space Odyssey : 1968 : 8.5 :
: 5 : Blade Runner : 1982 : 8.1 :
:......:..........................:......:........:
FT_EMPTY_STYLE
Rank Title Year Rating
1 The Shawshank Redemption 1994 9.5
2 12 Angry Men 1957 8.8
3 It's a Wonderful Life 1946 8.6
4 2001: A Space Odyssey 1968 8.5
5 Blade Runner 1982 8.1
FT_EMPTY2_STYLE
Rank Title Year Rating
1 The Shawshank Redemption 1994 9.5
2 12 Angry Men 1957 8.8
3 It's a Wonderful Life 1946 8.6
4 2001: A Space Odyssey 1968 8.5
5 Blade Runner 1982 8.1
FT_SOLID_STYLE
┌──────┬──────────────────────────┬──────┬────────┐
│ Rank │ Title │ Year │ Rating │
├──────┼──────────────────────────┼──────┼────────┤
│ 1 │ The Shawshank Redemption │ 1994 │ 9.5 │
│ 2 │ 12 Angry Men │ 1957 │ 8.8 │
│ 3 │ It's a Wonderful Life │ 1946 │ 8.6 │
├──────┼──────────────────────────┼──────┼────────┤
│ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 │
│ 5 │ Blade Runner │ 1982 │ 8.1 │
└──────┴──────────────────────────┴──────┴────────╯
FT_SOLID_ROUND_STYLE
╭──────┬──────────────────────────┬──────┬────────╮
│ Rank │ Title │ Year │ Rating │
├──────┼──────────────────────────┼──────┼────────┤
│ 1 │ The Shawshank Redemption │ 1994 │ 9.5 │
│ 2 │ 12 Angry Men │ 1957 │ 8.8 │
│ 3 │ It's a Wonderful Life │ 1946 │ 8.6 │
├──────┼──────────────────────────┼──────┼────────┤
│ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 │
│ 5 │ Blade Runner │ 1982 │ 8.1 │
╰──────┴──────────────────────────┴──────┴────────╯
FT_DOUBLE_STYLE
╔══════╦══════════════════════════╦══════╦════════╗
║ Rank ║ Title ║ Year ║ Rating ║
╠══════╬══════════════════════════╬══════╬════════╣
║ 1 ║ The Shawshank Redemption ║ 1994 ║ 9.5 ║
║ 2 ║ 12 Angry Men ║ 1957 ║ 8.8 ║
║ 3 ║ It's a Wonderful Life ║ 1946 ║ 8.6 ║
╠══════╬══════════════════════════╬══════╬════════╣
║ 4 ║ 2001: A Space Odyssey ║ 1968 ║ 8.5 ║
║ 5 ║ Blade Runner ║ 1982 ║ 8.1 ║
╚══════╩══════════════════════════╩══════╩════════╝
FT_DOUBLE2_STYLE
╔══════╤══════════════════════════╤══════╤════════╗
║ Rank │ Title │ Year │ Rating ║
╠══════╪══════════════════════════╪══════╪════════╣
║ 1 │ The Shawshank Redemption │ 1994 │ 9.5 ║
╟──────┼──────────────────────────┼──────┼────────╢
║ 2 │ 12 Angry Men │ 1957 │ 8.8 ║
╟──────┼──────────────────────────┼──────┼────────╢
║ 3 │ It's a Wonderful Life │ 1946 │ 8.6 ║
╠══════╪══════════════════════════╪══════╪════════╣
║ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 ║
╟──────┼──────────────────────────┼──────┼────────╢
║ 5 │ Blade Runner │ 1982 │ 8.1 ║
╚══════╧══════════════════════════╧══════╧════════╝
FT_BOLD_STYLE
┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━┓
┃ Rank ┃ Title ┃ Year ┃ Rating ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━┫
┃ 1 ┃ The Shawshank Redemption ┃ 1994 ┃ 9.5 ┃
┃ 2 ┃ 12 Angry Men ┃ 1957 ┃ 8.8 ┃
┃ 3 ┃ It's a Wonderful Life ┃ 1946 ┃ 8.6 ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━┫
┃ 4 ┃ 2001: A Space Odyssey ┃ 1968 ┃ 8.5 ┃
┃ 5 ┃ Blade Runner ┃ 1982 ┃ 8.1 ┃
┗━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━┻━━━━━━━━┛
FT_BOLD2_STYLE
┏━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━┓
┃ Rank │ Title │ Year │ Rating ┃
┣━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┿━━━━━━━━┫
┃ 1 │ The Shawshank Redemption │ 1994 │ 9.5 ┃
┠──────┼──────────────────────────┼──────┼────────┨
┃ 2 │ 12 Angry Men │ 1957 │ 8.8 ┃
┠──────┼──────────────────────────┼──────┼────────┨
┃ 3 │ It's a Wonderful Life │ 1946 │ 8.6 ┃
┣━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┿━━━━━━━━┫
┃ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 ┃
┠──────┼──────────────────────────┼──────┼────────┨
┃ 5 │ Blade Runner │ 1982 │ 8.1 ┃
┗━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━┛
FT_FRAME_STYLE
▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ Rank ┃ Title ┃ Year ┃ Rating ▐
▌━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━▐
▌ 1 ┃ The Shawshank Redemption ┃ 1994 ┃ 9.5 ▐
▌ 2 ┃ 12 Angry Men ┃ 1957 ┃ 8.8 ▐
▌ 3 ┃ It's a Wonderful Life ┃ 1946 ┃ 8.6 ▐
▌━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━▐
▌ 4 ┃ 2001: A Space Odyssey ┃ 1968 ┃ 8.5 ▐
▌ 5 ┃ Blade Runner ┃ 1982 ┃ 8.1 ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟
标签:教程,set,单元格,cell,bool,table,cpp,libfort,fort
From: https://www.cnblogs.com/cnyuyang/p/18372741