Created
April 24, 2017 17:12
-
-
Save victorholt/9f1cb77347676df3b27a331736f79ea5 to your computer and use it in GitHub Desktop.
C++ ForEach Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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