小程式:從列表中移除
來源資料, 只看第一個
1 2 3
2 2 3
3 2 3
4 2 3
要刪除的內容
1
3
結果會得到
2
4
使用方法
process test.txt rmlist.txt result.txt
debug原始載入資料
process test.txt rmlist.txt result.txt 1
debug載入資料第一欄
process test.txt rmlist.txt result.txt 2
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
int DEBUG = 0;
string outpath = "result.txt";
int main(int argc, char** argv) {
cout << argc << endl;
if (argc <= 2) return -1;
cout << "long list:" << argv[1] << endl;
cout << "remove list:" << argv[2] << endl;
if (argc > 3) outpath = argv[3];
cout << "output file:" << outpath << endl;
if (argc > 4) DEBUG = atoi(argv[4]);
ifstream rdf(argv[1], ifstream::in);
ifstream rmf(argv[2], ifstream::in);
ofstream outf(outpath.c_str(), ios_base::out|ios_base::trunc);
string line;
vector<string> list;
while(getline(rdf, line)) {
if (DEBUG == 1) cout << line << endl;
// only read the first column
istringstream iss(line);
string col1;
iss >> col1;
list.push_back(col1);
if (DEBUG == 2) cout << col1 << endl;
}
cout << "list loaded " << list.size() << endl;
vector<string> rmlist;
while(getline(rmf, line)) {
rmlist.push_back(line);
if (DEBUG) cout << line << endl;
}
cout << "rmlist loaded " << rmlist.size() << endl;
for (int i=0; i <rmlist.size() ; ++i) {
vector<string>::iterator itr;
int cnt = 0;
do {
itr = find(list.begin(), list.end(), rmlist[i]);
if (itr != list.end()) {
list.erase(itr);
cnt ++;
} else {
break;
}
} while(true);
if (cnt == 0) {
cout << " item: " << rmlist[i] << " not found in the list" << endl;
} else {
if (DEBUG) cout << " item: " << rmlist[i] << " removed " << cnt << endl;
}
}
cout << "list output " << list.size() << endl;
for (int i=0 ; i<list.size() ; ++i) {
outf << list[i] << endl;
}
outf.flush();
outf.close();
return 0;
}
留言