fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <map>
  4. #include <vector>
  5.  
  6. struct Sample {char value;};
  7.  
  8. std::vector<std::vector<Sample>> get_n_uplets(std::size_t n, const std::map<int, Sample>& samples)
  9. {
  10. if (samples.size() < n) {
  11. return {};
  12. }
  13. std::vector<std::vector<Sample>> res;
  14.  
  15. auto first = samples.begin();
  16. auto last = std::next(first, n - 1);
  17.  
  18. for (; last != samples.end(); ++first, ++last) {
  19. std::vector<Sample> inner;
  20.  
  21. for (auto it = first; it != std::next(last); ++it) {
  22. inner.push_back(it->second);
  23. }
  24. res.push_back(inner);
  25. }
  26. return res;
  27. }
  28.  
  29. void print(const std::vector<Sample>&t)
  30. {
  31. for (const auto& s : t) {
  32. std::cout << s.value << " ";
  33. }
  34. std::cout << std::endl;
  35. }
  36.  
  37. int main() {
  38. std::map<int, Sample> samples{
  39. {15, {'a'}},
  40. {21, {'b'}},
  41. {33, {'c'}},
  42. {37, {'d'}},
  43. {49, {'e'}}
  44. };
  45.  
  46. auto triplets = get_n_uplets(3, samples);
  47.  
  48. for (const auto& triplet : triplets) {
  49. print(triplet);
  50. }
  51. }
  52.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
a b c 
b c d 
c d e