答案 0 :(得分:2)
您可以在订购密钥时迭代地图:
std::vector<std::array<Sample, 3u>> get_triplets(const std::map<int, Sample>& samples)
{
if (samples.size() < 3) {
return {};
}
std::vector<std::array<Sample, 3u>> res;
auto it = samples.begin();
auto it1 = std::next(it);
auto it2 = std::next(it1);
for (; it2 != samples.end(); ++it, ++it1, ++it2) {
res.push_back({{it->second, it1->second, it2->second}});
}
return res;
}
编辑:拥有n-uplets,与之前的三元组版本相比有一些小变化:
std::vector<std::vector<Sample>> get_n_uplets(std::size_t n, const std::map<int, Sample>& samples)
{
if (samples.size() < n) {
return {};
}
std::vector<std::vector<Sample>> res;
auto first = samples.begin();
auto last = std::next(first, n - 1);
for (; last != samples.end(); ++first, ++last) {
std::vector<Sample> inner;
for (auto it = first; it != std::next(last); ++it) {
inner.push_back(it->second);
}
res.push_back(inner);
}
return res;
}