Created
October 2, 2012 15:07
-
-
Save t1anchen/3819921 to your computer and use it in GitHub Desktop.
C++0x Lambda Demo
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 <string> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
class Address { | |
public: | |
template<typename Func> | |
std::vector<std::string> findMatchingAddresses(Func func) { | |
std::vector<std::string> results; | |
for(auto itr = _addresses.begin(), end = _addresses.end(); | |
itr != end; | |
++itr) { | |
if(func(*itr)) { | |
results.push_back(*itr); | |
} | |
} | |
return results; | |
} | |
void addAddress(std::string newAddress) { | |
_addresses.push_back(newAddress); | |
} | |
private: | |
std::vector<std::string> _addresses; | |
}; | |
int main() { | |
auto func = []() { std::cout << "Hello World!" << std::endl; }; | |
func(); | |
Address g_address_book; | |
g_address_book.addAddress("gnu.org"); | |
g_address_book.addAddress("ibm.com"); | |
g_address_book.addAddress("wikipedia.org"); | |
std::vector<std::string> ret = g_address_book.findMatchingAddresses( | |
[](const std::string& addr) {return addr.find(".org") != std::string::npos;}); | |
std::copy(ret.begin(), ret.end(), std::ostream_iterator<std::string>(std::cout, " ")); | |
std::cout << std::endl; | |
auto func2 = [](std::string x){ return "I find it! It's " + x; }; | |
std::transform(ret.begin(), ret.end(), std::ostream_iterator<std::string>(std::cout, "\n"), func2); | |
return 0; | |
} |
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
CXX = i686-pc-mingw32-g++ | |
CXXFLAGS = -Wall | |
CXXDEBUGFLAG = -g | |
CXXOPTFLAG = -O3 | |
CXX0XFLAG = -std=c++0x | |
CXXLINKFLAG = -static | |
BINS = cxx0x-address | |
all: $(BINS) | |
cxx0x-address: cxx0x-address.o | |
$(CXX) $(CXXLINKFLAG) -o$@ $^ | |
.cc.o: | |
$(CXX) $(CXXFLAGS) $(CXXOPTFLAG) $(CXX0XFLAG) -c $^ | |
clean: | |
rm -vf *.o | |
dist-clean: | |
rm -vf $(BINS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment