经常用C++读入、写出(打印)矩阵数组,觉得要想办法自动解决一下这个问题。于是开始模仿Python的Numpy,矩阵暂时用vector<vector<double>>代替:
*目前是V0.1
实现功能主要如下:
- ReadTxt.h / ReadTxt.cpp
- genFromtxt(支持分隔符和注释,制表符视为空格,返回
vector<vector<double>>)
- genFromtxt(支持分隔符和注释,制表符视为空格,返回
- WriteTxt.h / Writetxt.cpp
- print(显示在屏幕上,fixed,可控制位数)
- saveTxt(将
vector<vector<double>>保存为文件,fixed,可控制位数)
更详细一点的说明见头文件函数定义注释
😄ReadTxt.h
/*
@author: Kan Haoyu
@date: 2021/3/8
@function: Basic text Input
*/
#pragma once
#ifndef READTXT_H
#define READTXT_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
// read a txt into a vector<vector<double>> ;
// line splited by split_char;
// string after comment_char will be discarded;
// line without contents(pure comment) will also be discarded
vector<vector<double>> genFromtxt(const string& file_path, const char split_char, const char comment_char);
// return a string where substring after comment_char will be discarded
string decommentString(const string& str, const char comment_char);
// split string with split_char into a vector<string>
vector<string> splitString(const string& str, const char split_char);
// convert vector<string> into vector<double>
vector<double> asDouble(const vector<string>& vec_str);
// replace all tab('\t') with blankspace(' ') in a string and return the result
string replaceTabWithBlankspace(const string& str);
#endif
😄ReadTxt.cpp
#include "ReadTxt.h"
vector<vector<double>> genFromtxt(const string& file_path, const char split_char,

本文介绍了一套用于C++的简单工具,该工具能够帮助开发者轻松地从文本文件中读取矩阵数据,并能将矩阵数据保存到文本文件中。支持自定义分隔符、注释符以及固定精度的输出。
最低0.47元/天 解锁文章
1574

被折叠的 条评论
为什么被折叠?



