C++0x³¢ÏÊ ÓÃlambda±í´ïʽģÄâRubyµÄ¹ý³Ì¶ÔÏó
¹ØÓÚC++0x
Ô¤¼ÆÓÚÃ÷Äêµ×ÍƳöµÄбê×¼C++0x£¬Ëä˵ÒѽӽüÊչٽ׶Σ¬È´ÈÔÓÐÈô¸ÉÓдý½ÓÄɵÄÐÂÌá°¸±»Ìá³ö¡£ÓÈÆäÖµµÃ¹Ø×¢µÄÊÇ£¬Óëlambda±í´ïʽÒÔ¼°¾Ö²¿º¯ÊýÏà¹ØµÄÌá°¸“Unified Function Syntax£¨Í³Ò»µÄº¯ÊýÓï·¨£©”£¬¾¡¹Ü²»Êܱê׼ίԱ»á´ý¼û£¨Á½Äê¼äÀú¾ËÄ´ÎÑÓ³Ù±í¾ö£¬Á½´Î²»Óèͨ¹ý£©£¬ÈÔÈ»“ÍçÇ¿µØ”ÍƳöÁËÆäµÚ7¸ö°æ±¾N2989£¬Ìá°¸×÷Õßïƶø²»ÉáµÄ¾«ÉñʵÔÚÁîÈËÇÕÅå¡£
¹ØÓÚlambda±í´ïʽ
Êܺ¯ÊýÐͱà³Ì·ç¸ñµÄÓ°Ï죬һЩOOPΪÖ÷Ìâ˼ÏëµÄ¾²Ì¬ÓïÑÔ·×·×ÍƳöÁËlambdaÕâÒ»ÒÔ¶ÌСµÄÄäÃûº¯ÊýΪÌصãµÄÓïÑÔÉèÊ©£¬µäÐ͵ÄÈç΢ÈíµÄVB9ºÍC#3.0¡£Óë´ËÏà¶ÔÓ¦µÄÊÇ£¬Öڶද̬½Å±¾ÓïÑÔ£¨python, ruby, javascript£©ÔòÔç¾Í¾ß±¸ÁËÏàÓ¦µÄÓïÑÔÌØÐÔ£¬RubyµÄ¹ý³Ì¶ÔÏó±ãÊÇÆäÖÐÒ»Àý¡£ÕâÒ»´Î½èC++0xбê×¼µÄ¶«·ç£¬C++ÓïÑÔ²»¸ÊÈ˺óͬÑùÒýÈëÁËÕâÒ»¼¤¶¯ÈËÐĵÄÐÂÌØÐÔ£¬Îªº¯ÊýÐͱà³Ì·ç¸ñÔÚC++ÓïÑÔÄڵĽøÒ»²½Íƹã´òÏÂÁ˼áʵµÄ»ù´¡¡£
ÒÔÏÂÓÃC++0xÖеÄlambda±í´ïʽÀ´Ä£ÄâRubyµÄ¹ý³Ì¶ÔÏó¡£
Ruby´úÂë
class Array
def inject(n)
each { |value| n = yield(n, value) }
n
end
def sum
inject(0) { |n, value| n + value }
end
def product
inject(1) { |n, value| n * value }
end
def find
for i in 0...size
value = self[i]
return value if yield(value)
end
return nil
end
end
[ 1, 2, 3, 4, 5 ].sum # 15
[ 1, 2, 3, 4, 5 ].product # 120
[ 1, 2, 3, 4, 5 ].find {|v| v*v > 10 } # 4
ÓйØRubyµÄ¹ý³Ì¶ÔÏóÒÔ¼°Õâ¶ÎRuby´úÂëµÄ˵Ã÷£¬Çë²Î¿¼RubyÓïÑÔÖеķº»Øµ÷¼°ÆäÔÚC++ÓïÑÔÖеÄÄ£ÄâʵÏÖÒ»ÎÄ¡£
C++´úÂë
#include <array>
#include <functional>
//#include <numeric>
#include <algorithm>
#include <boost/assign.hpp>
#include <iostream>
using namespace std;
template<typename T>
struct Array : public vector<T>
{
template<typename _Iter>
Array(_Iter _First, _Iter _Last) : vector(_First, _Last) {}
T inject(T n, function<T(T,T)> f) const {
for_each(begin(), end(), [&](T value){n = f(n, value);});
return n;
Ïà¹ØÎĵµ£º
Ò». ÔÚcÖзÖΪÕ⼸¸ö´æ´¢Çø
1.Õ» - ÓɱàÒëÆ÷×Ô¶¯·ÖÅäÊÍ·Å
2.¶Ñ - Ò»°ãÓɳÌÐòÔ±·ÖÅäÊÍ·Å£¬Èô³ÌÐòÔ±²»ÊÍ·Å£¬³ÌÐò½áÊøʱ¿ÉÄÜÓÉOS»ØÊÕ
3.È«¾ÖÇø£¨¾²Ì¬Çø£©£¬È«¾Ö±äÁ¿ºÍ¾²Ì¬±äÁ¿µÄ´æ´¢ÊÇ·ÅÔÚÒ»¿éµÄ£¬³õʼ»¯µÄÈ«¾Ö±äÁ¿ºÍ¾²Ì¬±äÁ¿ÔÚÒ»¿éÇøÓò£¬Î´³õʼ»¯µÄÈ«¾Ö±äÁ¿ºÍδ³õʼ»¯µÄ¾²Ì¬±äÁ¿ÔÚÏàÁÚµÄÁíÒ»¿éÇøÓò¡£- ³ÌÐò½áÊøÊÍ·Å
4.ÁíÍ ......
ÊÂÇéÓ¦¸Ã±»×öµÃ¾¡¿ÉÄܵļòµ¥£¬µ«²»ÊÇÈÎÒâµØ¼òµ¥»¯¡£
¨D¨D°®Òò˹̹
ËäÈ»×öºÃµÄ³ÌÐòÄÜ°´ÏîÄ¿ÐèÇóÕýÈ·ÔËÐе½×îºóÒ»²½£¬µ«ÔÚǶÈëʽϵͳ¿ª·¢Öв¢²»×ÜÊÇÄܳɹ¦µÄ¡£ÓÉÓڵͳɱ¾µÄÐèÒªÓ²¼þÉè¼ÆÕß¼¸ºõ²»¿ÉÄÜÉè¼Æ³ö×ã¹»µÄÄÚ´æºÍ´¦ÀíÆ÷ÐÔÄÜÀ´Ê¹µÃ³ÌÐòÄܱ»ÔËÐС£µ±È»£¬ÔÚÈí¼þ¿ª·¢¹ý³ÌÖÐʹµÃ³ÌÐòÄܹ»ÕýÈ·ÔËÐÐÊǸüÖØÒªµÄ¡£ÎªÁËÕâµã£¬Í¨³£ ......
¡¡¡¡1.¸ÅÊö
¡¡¡¡Ðí¶à³õѧÕ߶ÔC/C++ÓïÑÔÖеÄvoid¼°voidÖ¸ÕëÀàÐͲ»ÉõÀí½â£¬Òò´ËÔÚʹÓÃÉϳöÏÖÁËһЩ´íÎó¡£±¾ÎĽ«¶Ôvoid¹Ø¼ü×ÖµÄÉî¿Ìº¬Òå½øÐнâ˵£¬²¢ÏêÊövoid¼°voidÖ¸ÕëÀàÐ͵ÄʹÓ÷½·¨Óë¼¼ÇÉ¡£
¡¡¡¡2.voidµÄº¬Òå
¡¡¡¡voidµÄ×ÖÃæÒâ˼ÊÇ“ÎÞÀàÐÍ”£¬void *ÔòΪ“ÎÞÀàÐÍÖ¸Õ딣¬void *¿ÉÒÔÖ¸ÏòÈκÎÀàÐ͵ÄÊ ......
1) ʲôÊÇÔ¤±àÒ룬ºÎʱÐèÒªÔ¤±àÒ룺×ÜÊÇʹÓò»¾³£¸Ä¶¯µÄ´óÐÍ´úÂëÌå¡£
³ÌÐòÓɶà¸öÄ£¿é×é³É£¬ËùÓÐÄ£¿é¶¼Ê¹ÓÃÒ»×é±ê×¼µÄ°üº¬ÎļþºÍÏàͬµÄ±àÒëÑ¡Ïî¡£ÔÚÕâÖÖÇé¿öÏ£¬¿ÉÒÔ½«ËùÓаüº¬ÎļþÔ¤±àÒëΪһ¸öÔ¤±àÒëÍ·¡£
2) char * const p;
¡¡¡¡char const * p
¡¡¡¡const char *p ......