我有两个不同的数据集,如下面的worldGiving.txt和OrganDonor.txt
in worldGiving.txt
Australia 60% 76% 2 37% 12 67% 10
Ireland 60% 79% 1 34% 15 66% 13
Canada 58% 64% 10 42% 7 67% 10
New Zealand 57% 66% 8 38% 11 68% 9
United States 57% 57% 13 42% 7 71% 3
Netherlands 53% 73% 3 34% 15 51% 49
Indonesia 52% 71% 5 41% 10 43% 77
United Kingdom 51% 72% 4 26% 35 56% 33
Paraguay 50% 48% 23 42% 7 61% 22
Denmark 49% 70% 7 23% 45 54% 43
Liberia 49% 12% 109 53% 2 81% 1
Iran 48% 51% 21 24% 41 70% 6
Turkmenistan 48% 30% 52 58% 1 56% 33
Qatar 47% 53% 17 17% 67 71% 3
Sri Lanka 47% 42% 30 43% 6 55% 38
Trinidad and Tobago 45% 44% 26 30% 23 62% 19
Finland 45% 50% 22 27% 30 57% 29
Philippines 45% 32% 47 44% 5 58% 26
Hong Kong 44% 64% 10 13% 87 56% 33
Oman 44% 39% 32 22% 48 72% 2
Cyprus 44% 56% 14 27% 30 48% 57
...
in organDonor.txt
Croatia 36,5
Spain 35,1
Belgium 32,9
Malta 30
Puerto Rico 26,3
USA 25,6
France 24,9
Estonia 24,3
Portugal 24
Norway 23,5
Slovenia 23
Austria 22,5
Italy 22,4
Finland 19,9
Czech Rep. 19,8
Lativa 19
UK 18,3
Ireland 17
Uruguay 16,5
Poland 16,1
Argentina 15,7
Australia 15,6
Netherlands 15,1
Sweden 15
Canada 14,7
Hungary 14,3
Lithuania 13,7
Denmark 13,4
Slovak Rep. 13,1
...
以下是我的代码。
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
string line;
vector<vector<string>> extractingData;
vector<vector<string>> extractingData2;
string pathExtract;
string pathStorage;
string pathMerged;
fstream extractingFile;
fstream extractingFile2;
void mergeData(vector<vector<string>> extractingData, vector<vector<string>> extractingData2, string pathStorage);
vector<vector<string>> ReadData(fstream& extractingFile, vector<vector<string>> extractingData, string pathExtract)
{
extractingFile.open(pathExtract);
int i = 0;
int index;
if (extractingFile.is_open())
{
while (getline(extractingFile, line))
{
istringstream iss(line);
extractingData.resize(i + 1);
int j = 0;
while (!iss.eof()){
extractingData[i].resize(j+1);
iss >> extractingData[i][j];
j++;
}
i++;
}
return extractingData;
}
}
int main() {
pathExtract = "worldGiving.txt";
pathStorage = "OrganDonors.txt";
pathMerged = "merged.txt";
extractingData=ReadData(extractingFile, extractingData, pathStorage);
extractingData2=ReadData(extractingFile2, extractingData2, pathExtract);
mergeData(extractingData, extractingData2, pathMerged);
}
void mergeData(vector<vector<string>> extractingData, vector<vector<string>> extractingData2, string pathStorage){
int i = 0;
int j = 0;
while (i<extractingData2.size() && i<extractingData.size())
{
cout << extractingData[i][0];
while (extractingData[i][0] != extractingData2[j][0] && j != extractingData2.size()-1)
{
j++;
}
if (extractingData[i][0] == extractingData2[j][0])
{
for (std::vector<string>::iterator it = extractingData2[j].begin()+1; it != extractingData2[j].end(); ++it)
{
/*auto back = std::move(extractingData2[j].back);
extractingData[i].push_back(std::move(back));*/
auto back = *it;
extractingData[i].push_back(back);
}
}
i++;
j = 0;
}
i = 0;
ofstream outFile(pathStorage);
while (i<extractingData2.size() && i<extractingData.size())
{
for (std::vector<string>::iterator it = extractingData[i].begin(); it != extractingData[i].end(); ++it) {
outFile << *it <<' ';
}
outFile << endl;
i++;
}
}
但是我遇到了一个问题,即不止一个术语命名的国家如&#34;新西兰&#34;和#34;美国&#34;单独分配在多个区块中,例如&#34; New&#34;,&#34; Zealand&#34;。 所以我不能恰当地比较两个文本文件。所以我想如果我能分辨出它的数字与否,当它被分配给Vector时,我可以解决这个问题。 我该怎么办?
答案 0 :(得分:1)
如果您无法更改格式以更清楚地分隔国家/地区名称,则可以计算您找到的项目数量,并从最后开始分配它们。找到你的价值后剩下的东西应加入名称。