我正在开发一个程序,该程序必须读取包含(id,user1,user2)的文件,并返回user1和user2之间的最新连接。例如:
Input:
id user1 user2
1 a a1
2 a a1
3 a a2
It should return:
a: (a1:2), (a2,3)
我编写了一个函数,该函数返回每个user1的所有条目,但我找不到删除重复项并仅保留最新的方法。 这是我的代码:
static void summ(std::vector<std::string> ×, std::vector<std::string> &srcs, std::vector<std::string> &dsts)
{
std::unordered_map<std::string, std::vector<std::string>> dstMap;
for (std::size_t s = 1; s < srcs.size(); ++s)
dstMap[srcs[s]].emplace_back(dsts[s] + " "+ times[s]);
for (auto it{ dstMap.begin() }; it != dstMap.end(); ++it)
{
std::string line{ it->first + ": " };
for (std::size_t i = 0; i < it->second.size(); ++i)
line += it->second[i] + " ";
cout << line << endl;
}
}
我相信我只需要添加for循环来比较值,但是没有用,此函数提供的输出是: a:a1 1 a1 2 a2 3
如果有人可以提供帮助,我将不胜感激。 谢谢,
您似乎在最里面的循环中添加了所有目的地:
相反,删除循环,只需添加最后一个目的地:
Assuming, you just want
line
to store the last destination for a given source.