Skip to content

Instantly share code, notes, and snippets.

@victorholt
Created April 24, 2017 17:12
Show Gist options
  • Save victorholt/9f1cb77347676df3b27a331736f79ea5 to your computer and use it in GitHub Desktop.
Save victorholt/9f1cb77347676df3b27a331736f79ea5 to your computer and use it in GitHub Desktop.
C++ ForEach Example
#include <iostream>
#include <string>
#include <functional>
#include <vector>
template<class ArrayType, class ValueType>
void forEach(const ArrayType& arr, std::function<void(const ValueType&)> callback) {
for (auto entry : arr) {
callback(entry);
}
}
int main()
{
std::vector<std::string> strArr;
strArr.push_back("a");
strArr.push_back("b");
strArr.push_back("c");
strArr.push_back("d");
strArr.push_back("e");
forEach<std::vector<std::string>, std::string>(strArr, [](const std::string& str) {
std::cout << str << std::endl;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment