//
// Created by HongDaYu on 17 十月 2024.
//#ifndef HDYSDK_UTIL_H
#define HDYSDK_UTIL_H#include <cstdint>
#include <string>
#include <list>
#include <iomanip>
#include <memory>class dataGrid {
private:std::list<const char*> _table;int32_t col = 0;size_t width = 15;char split_char = '-';
public:explicit dataGrid(size_t w,int32_t col,char s='-'):width(w),split_char(s),col(col){}dataGrid &operator<<(const char*);const std::list<const char *> &getList(){return _table;}std::shared_ptr<char> table();
};#endif//HDYSDK_UTIL_H#include "util.h"
#include <list>
#include <iostream>
#include <cstring>
#include <strstream>dataGrid &dataGrid::operator<<(const char *row) {this->_table.push_back(row);return *this;
}std::shared_ptr<char> dataGrid::table() {size_t len = 0;size_t counts = 1;int32_t index = 0;for(auto it : _table){len+=strlen(it);counts++;}std::shared_ptr<char> grid(new char[len+width*counts*2]);std::ostrstream buffer(grid.get(),static_cast<int>(len+width*counts*2));for(auto it : _table){buffer << std::setw(static_cast<int>(width)) << std::left << it;if(((index+1)%col) == 0){if((index+1) == col)buffer << std::endl << std::setfill(split_char) << std::setw(static_cast<int>(width) * col) << "" << std::setfill(' ') << std::endl;elsebuffer << std::endl;}index++;}return grid;
}int main() {dataGrid buffer(15,2);buffer << "hello" << "world" << "very" << "good";std::cout << buffer.table();return 0;
}
一般只有开发人员在debug的时候,在调用一个库有点累赘