我在 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++;
}
}
但我遇到了一个问题,多个名为“New Zealand”和“United States”等国家/地区的术语分别分配在多个 block 中,例如“New”、“Zealand”。 所以我不能适本地比较两个文本文件。所以我想如果我能在将它分配给 Vector 时区分它的类型是数字还是非数字,我就可以解决这个问题。 我该怎么办?
最佳答案
如果您不能更改格式以更清楚地划分国家/地区名称,您可以计算找到的项目数量并从头到尾分配它们。找到您的值(value)观后剩下的应该加入名称。
关于c++ - 如何在一个 vector 中分配多个不同类型的变量或区分其类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822794/