- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
基本上我有一个包含 8 列的数据文件,我想将每一列的每个值放入一个数组变量中。但问题是缺少某些值。例如
100 54201.10 49392 9379101 10381.1372
101 5823829 73929 83729.77
缺失值由额外的制表符或/t 空格隔开。我如何才能读取这些值,忽略丢失的数据并将正确的值输入到可用的正确变量中?
我尝试使用:
infile >> network;
string val = isNaN(network);
if (count % 8 == 0) { ID[count / 8] = val; }
if (count % 8 == 1) { time[count / 8] = val; }
if (count % 8 == 2) { country_code[count / 8] = val; }
if (count % 8 == 3) { sms_in[count / 8] = val; }
if (count % 8 == 4) { sms_out[count / 8] = val; }
if (count % 8 == 5) { call_in[count / 8] = val; }
if (count % 8 == 6) { call_out[count / 8] = val; }
if (count % 8 == 7) { internet[count / 8] = val; }
count++;
最佳答案
在 C++ 中执行此操作的一个好方法是使用 getline 获取每一行。
#include <string>
#include <vector>
...
typedef struct {
unsigned long id;
unsigned long timestamp;
...
} Record;
std::vector<Record> records;
while (std::getline(std::cin, s)) {
...
然后使用substr填写记录集合。假设您的字段是制表符分隔的并且数字左对齐,那么您可以像这样处理默认设置。
posTab = s.find_first_of('\t');
records[i].id = posTab == 0
? defaultID
: std::atoi(s.substr(0, posTab).c_str());
索引 i 是记录索引,从 0 开始。对于 float 和 double float ,您需要用适当的标准数值解析器替换 std::atoi。
如果数据是制表符分隔的,对于(每条记录的)每个字段,使用 find_first_of(posTab + 1, '/t') 查找每个字段的开头,从您可以保存在 posPreviousTab 中的前一个位置开始用作相等性测试和第一个 substr 参数而不是零。
注意事项
对于大型数据集和在某些条件下,std::list 比 std::vector 更快。您可以编写测试来比较您的案例的两个选项。
如果您正在处理大数据,您可能需要更高的速度,使用 char[MAXSIZE] 和 C 中的等效算法并即时处理,而不是将每条记录都存储在内存中。
关于c++ - 从缺少列的文件中读取数据。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42731128/
我正在尝试在 map 上绘制一些疾病事件数据的位置。 我用它来导入数据: ByTown% addProviderTiles("CartoDB.Positron")%>% addPolygons
我有一个文件调用 find.js,我使用 node find.js 运行,我的节点是版本 10 我不知道为什么我无法使用 async await。 const axios = require("axi
我有一个项目作为引用添加到 System.Web。 但是,它似乎无法获取 HttpContext。这样做: Imports System.Web _ApplicationBase = HttpCont
在互联网上找到这段代码,出于某种原因它缺少 while 循环逻辑“while(i....)”,虽然我找到了 PigLatin* 问题的其他可行解决方案,但我真的很想了解这个正在工作。 *PigLati
我工作了一整天来运行 Xampp 并在其上安装 TYPO3。现在我登录到后端,但没有显示许多管理模块,例如模板、访问等。 - 一定是我做错了什么,但我不知道。 these are the module
你好 我有编译这个问题 \begin{equation} J = \sum_{j=1}^{C} \end{equation} 我不断收到错误 missing $ inserted 这很奇怪,因
我正在尝试使用 SQLite CLI,但无法获得 generate_series功能来工作。我可以按照文档中的建议使用递归 CTE 对其进行模拟,但我似乎无法获得该链接中的任何示例。这是我的 sess
我目前正在开发我想要的软件,而软件正在安装,它可以在后台为软件创建 native 图像。 我正在考虑使用 NGEN 并将进程优先级设置为低,因为我不希望它消耗 100% CPU。但是我发现我的计算机上
我想使用 Xcodes Instruments 进行 UI 自动化测试。但似乎缺少“自动化”。我怎样才能添加这个? 最佳答案 如果您想使用自动化仪器,请使用 Xcode 7.3。 Apple 在 Xc
我目前在 JS 开发中迈出了一小步,并编写了以下链接添加器: const button = document.getElementById('button') const listdiv = docu
此代码有什么问题: NSError *error = nil; [SFHFKeychainUtils deleteItemForUsername:@"IAPNoob01" andServiceName
出于某种原因,在安装和配置(我认为)一切之后,com.adobe.utils.AGALMiniAssembler 不见了,其他一切正常。 我认为我已尽一切努力让孵化器正常工作,但显然我错过了一步。 如
我有一个名为 new 的方法。调用 new 时,我传递了一个参数,但是当我运行应用程序时,出现没有参数或参数为空的错误。 StepReader.pm package StepReader; use s
安装 gtk 1.2(包名 gtk1)和 macports chokes 在最终的 make 中,在 libintl.h 的第 440 行。 extern locale_t libintl_newlo
我用按钮创建表格。 这是javascript代码: function layersListTable(layers) { var content =''; $.each($(layer
我在使用此 javascript 时遇到此错误,任何人都可以帮我弄清楚我做错了什么吗? $(this).prepend('Check availability »'); 它给我错误 mis
我有一个独立的工具链 NDK13b、api19、llvm 3.8 编译器、arm 32 位、带有 libcpp(llvm C++ 库) 我想避免依赖 libgcc,所以我构建了 compiler-rt
我按照一些教程使用 phonegap 的条形码扫描器插件。但是当我从现有源创建一个新的 android 项目来创建条码库时 (step 6 in this page)我收到错误:“AndroidMan
我现在尝试在 Eclipse 中打开我的布局 xml 文件。我只得到错误 No XML content. Please add a root view or layout to your docume
我的 android-sdk-windows\tools 目录中缺少层次结构查看器工具。 工具链接: http://developer.android.com/guide/developing/too
我是一名优秀的程序员,十分优秀!