比较两个文件中的两个中间人的最佳方法.C++
Best way to Compare two Intergers from two files. C++
我有两个文本文件,在单独的行上包含 7 位整数,我想要一个程序来比较一个文件中的整数和另一个文件。
使用的示例日期(大约 300+ 个单独的整数(
1867575
1867565
1867565
1867433
这是我到目前为止的代码,可以打开保存到桌面的两个文件。
#include <iostream> //I have no idea what these do...
#include <fstream> //Will be tidying this up once it all works
#include <cmath>
#include <cstdlib>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <stdio.h>
using namespace std;
int main(){
ifstream arfile; // Declares the first text file Applicants records - AR
ifstream qvfile; // Declares the second text file Qualifaction records - QV
// Will be comparing intergers from AR list to Qv list
arfile.open("C:\Users\sagrh18\Desktop\ar.txt"); // Opens the AR file
if(!arfile.is_open()){
printf ("AR file hasn't openedn");
getchar();
exit(EXIT_FAILURE); // Checks the file has been opened
}else
{
qvfile.open("C:\Users\sagrh18\Desktop\qv.txt"); // Opens the Input file Qv for comparrsion.
if(!qvfile.is_open()){
printf ("QV file hasn't openedn");
getchar();
exit(EXIT_FAILURE); // Checks the file has been opened
}
printf("I have opened the QA and AR filen");
//Need a loop to comapare Ar lines to Qv lines
//If there is a match do nothing
//If there not a match then print off the number
}
printf ("Program has finsihed press Enter n");
getchar();
return 0;
}
我知道步骤是什么,我只是不确定如何最好地实现它们,使用两个数组是最好的吗?逐行阅读的最简单方法是什么?自从我编写任何东西以来已经有几年了,所以任何建议都会很棒。
给定有效的ifstream arfile
和ifstream qvfile
,您可以使用istream_iterator
来填充vectors
:
const vector<int> arvec { istream_iterator<int>(arfile), istream_iterator<int>() };
vector<int> qvvec { istream_iterator<int>(qvfile), istream_iterator<int>() };
读取两个文件的内容后,您现在需要比较文件,最快的方法是对qvvec
进行排序,然后使用binary_search
:
sort(begin(qvvec), end(qvvec));
for(const auto& i : arvec) {
if(!binary_search(cbegin(qvvec), cend(qvvec), i)) {
cout << i << endl;
}
}
相关文章:
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 比较两个节点坐标的最佳方法是什么?
- 比较C++中两个整数的最有效和最干净的方法是什么?
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 使用 std::vector::swap 方法在C++中交换两个不同的向量是否安全?
- 检查两个向量是否并行的最有效方法
- 用相同的参数声明两个构造函数的最偶像化的方法是什么?
- 仅通过C++中数组初始化的不同方法,即可在同一输入上获得两个不同的答案
- 区分两个零arg构造函数的惯用方法
- 在C++中将返回unique_ptr和shared_ptr的两个工厂方法组合为一个
- 有没有办法仅在两个模板参数相同时才覆盖匹配的模板方法?
- std::list 的两个 insert() 方法签名之间的实现差异
- 两个继承类如何调用彼此的方法?
- 为什么我们不能同时实现两个方法'getAB() &&'和'getAB()'?
- 编写两个方法的最佳方法,这些方法在C++中返回同一项的值和引用
- 访问对象 B 中对象 C 的方法,这两个方法均由对象 A 创建
- GoogleMock:期待两个方法调用中的任何一个
- Qt在两个不同的线程中运行同一对象的两个方法
- 两个方法声明都"pass by reference"吗?