C++读取txt中的矩阵数据并存入vector中
来源:互联网 发布:表演者言 知乎 编辑:程序博客网 时间:2024/05/16 04:39
C++读取txt中的矩阵数据并存入vector中
每种矩阵数据都有他的独特性质,比如有的全是整数,有的全是小数,也有的既有小数还有整数,还有的是负数,这些数字难以用指针对字符串操作来读取,但是用正则表达式就很好处理,下面我就以正则表达式处理文本数据中的整数来示例。
假设有如下数据:
0 9 13 3 0 0 400 0 22 0 12 0 00 2 0 6 13 0 00 0 0 0 0 0 30 0 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 3 00 0 0 0 53 2 220 0 4 0 6 0 00 0 2 0 0 25 09 0 2 19 0 0 042 0 0 0 30 0 30 0 6 9 5 0 00 0 0 0 0 0 00 0 2 0 0 0 00 0 0 0 0 0 0
数据之间以空格或者Tab来隔开。
源代码:
#include <iostream>#include <fstream>#include <regex>#include <string>#include <vector>using namespace std;int main() { vector<int> temp_line; vector<vector<int>> Vec_Dti; string line; ifstream in("xxx.txt"); //读入文件 regex pat_regex("[[:digit:]]+"); //匹配原则,这里代表一个或多个数字 while(getline(in, line)) { //按行读取 for (sregex_iterator it(line.begin(), line.end(), pat_regex), end_it; it != end_it; ++it) { //表达式匹配,匹配一行中所有满足条件的字符 cout << it->str() << " "; //输出匹配成功的数据 temp_line.push_back(stoi(it->str())); //将数据转化为int型并存入一维vector中 } cout << endl; Vec_Dti.push_back(temp_line); //保存所有数据 temp_line.clear(); } cout << endl << endl; for(auto i : Vec_Dti) { //输出存入vector后的数据 for(auto j : i) { cout << j << " "; } cout << endl; } return 0;}
输出结果:
第一个块为匹配数据,第二个快为存入vector后的数据。
阅读全文
0 0
- C++读取txt中的矩阵数据并存入vector中
- ios读取txt文件并存入数组
- 用java将数据库表中内容读取出来并存入txt中
- C++读取txt文本中的矩阵数据
- 从Excel读取数据并存入数据库
- 前台数据通过AJAX传到JAVA后台并存入到txt文件中
- Java解析txt文件中json数据到List<entity>,并存入数据库
- C# 读取EXCEL中的数据并存入SQL Server 2005数据库
- C语言读取txt文档中的数据
- python中,从mysql读取数据,并存入redis里面(1)
- python中,从mysql读取数据,并存入redis里面(2)
- python中,从mysql读取数据,并存入redis里面(3)
- java读取word中的表格并存入到mysql数据库中实例
- C语言读取多行字符串,并存入字符数组中
- 读取txt中的数据到数组中
- android 读取excel表格数据,并存入数据库
- c语言从文件中读入格式化数据并存入sqlite3
- java 读取txt中矩阵
- 观察者模式
- JavaScript之this的工作原理
- 函数指针
- 【爬虫】爬取猫眼电影top100
- set的使用题集
- C++读取txt中的矩阵数据并存入vector中
- Windows的cmd命令查询指定端口占用的进程并关闭
- 牛客网(直通BAT面试算法班) 第三章,字符串,专题系列 Day4
- Ubuntu16.04+opencv3.3.0的安装配置说明
- quartz作业调度框架
- 10-1 JS案例:表单校验
- 基于矢量切片的电子地图配图(二)示例数据来源
- maven-antrun-plugin插件
- 装逼必备:大型分布式网站术语分析