Skip to content

Instantly share code, notes, and snippets.

@DimChtz
Last active November 8, 2017 10:58
Show Gist options
  • Select an option

  • Save DimChtz/ca9b9dd25412806c42bfb18270a6a02f to your computer and use it in GitHub Desktop.

Select an option

Save DimChtz/ca9b9dd25412806c42bfb18270a6a02f to your computer and use it in GitHub Desktop.
Range function for 2d loop in C++.
#include <iostream>
#include <vector>
#include <utility>
template<typename T>
std::vector<std::pair<T, T>> range2(T start1, T end1, T start2, T end2) {
std::vector<std::pair<T, T>> loopValues{};
for (T i = start1; i < end1; ++i) {
for (T j = start2; j < end2; ++j) {
loopValues.push_back(std::make_pair (i, j));
}
}
return loopValues;
}
int main() {
for (auto p : range2(1, 3, 1, 3)) {
std::cout << p.first << p.second << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment