fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. auto pairs(const vector<int>& in, int groupLength = 4) {
  7. vector<pair<int, int>> result;
  8. int groups = in.size() / groupLength;
  9. for (int group = 0; group < groups; ++group) {
  10. int i = group * groupLength;
  11. int j = i + groupLength - 1;
  12. while (i < j) {
  13. result.emplace_back(in[i++], in[j--]);
  14. }
  15. }
  16. return result;
  17. }
  18.  
  19. int main() {
  20. vector<int> test = {1,2,3,4,5,6,7,8};
  21. for (auto p : pairs(test)) {
  22. cout << p.first << ' ' << p.second << endl;
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
1 4
2 3
5 8
6 7