tailieunhanh - HandBooks Professional Java-C-Scrip-SQL part 85
Tham khảo tài liệu 'handbooks professional java-c-scrip-sql part 85', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Transform using std bind1st and std plus std transform std bind1st std plus int 4 Transform using a lambda expression std transform _1 - 4 The difference is astounding When adding 4 using traditional means it s hard for the untrained eye to see what s going on. Reading the code we see that we are binding the first argument of a default-constructed instance of std plus to 4. The lambda expression spells it outsubtract 4 from the element. If you think that the version using bindlst and plus isn t that bad try it with longer expressions. supports all of the arithmetic operators in C so there s rarely a need to include functional just for the sake of arithmetic function objects. The following example demonstrates the use of some of those arithmetic operators. Each element in the vector vec is modified using the additive and multiplicative operators. include iostream include vector include algorithm include boost lambda int main using namespace boost lambda std vector int vec 3 vec 0 1 vec 1 2 vec 2 3 std for_each vec. end _1 10 std for_each _1- 10 std for_each _1 3 std for_each vec. end _1 2 std for_each vec. end _1 3 Terse readable and maintainablethat s the kind of code you get with . Skip std plus std minus std multiplies std divides and std modulus your code is always better with . Writing Readable Predicates Many of the algorithms in the Standard Library come in a version that accepts a unary or binary predicate. These predicates are free functions of function objects but of course a lambda expression also fits the bill. For predicates that are used often it makes perfect sense to define function objects but frequently they are used once or twice and then never looked at again. In such cases a lambda expression is a superior choice both because the code becomes easier to understand all .
đang nạp các trang xem trước