.


:




:

































 

 

 

 





 

, , . , , -, , -.

 

std::transform , ( !) . . , std::plus.

 

:

 

#include <vector>

#include <iostream>

#include <iterator>

#include <algorithm>

 

//

template < typename InputIt >

void printCollection (InputIt _first, InputIt _last, const char * _prefix)

{

std::cout << _prefix;

while (_first!= _last)

{

std::cout << * _first << ' ';

++ _first;

}

std::cout << std::endl;

}

 

int main ()

{

//

std::vector< double > vX, vY;

for (int i = 0; i < 10; i++)

{

vX.push_back(i);

vY.push_back(i * 2);

}

 

//

std::vector< double > vR;

 

//

// vR[ i ] = vX[ i ] + vY[ i ]

std::transform(

vX.begin(), vX.end(), vY.begin(),

std::back_inserter(vR),

std::plus< double >()

);

 

//

printCollection(vX.begin(), vX.end(), "X: ");

printCollection(vY.begin(), vY.end(), "Y: ");

printCollection(vR.begin(), vR.end(), "R: ");

}

 

:

 

 

std::plus . ++ :

 

+ std::plus
- () std::minus
- () std::negates
* std::multiplies
/ std::divides
% std::modulus
== std::equal_to
!= std::not_equal_to
< std::less
> std::greater
<= std::less_equal
>= std::greater_equal
&& std::logical_and
|| std::logical_or
! std::logical_not
& std::bit_and
| std::bit_or
^ std::bit_xor
~ std::bit_not

 

 

std::bind

 

- , , ( 2, 3). , . .

 

, (binder), . ++11 , ++11 std::bind ( <functional>). , , .

 

, , 2:

 

#include <functional>

#include <iostream>

#include <vector>

#include <algorithm>

 

//

bool dividesOn (int x, int N)

{

return (x % N) == 0;

}

 

int main ()

{

//

std::vector< int > v;

for (int i = 0; i < 10; i++)

v.push_back(i);

 

// , 2

std::cout

<< std::count_if(

v.begin(), v.end(), //

//

std::bind(

//

std::logical_not< bool >(),

// : 2

std::bind(& dividesOn, std::placeholders::_1, 2)

)

)

<< std::endl;

}

 

. , ( ), , , :

 

std::bind(& dividesOn, std::placeholders::_1, 2)

 

std::placeholders::_1 () , . - , . , std::placeholders::_1, std::placeholders::_2 , .

 

, :

 

struct binder_temp_functor

{





:


: 2017-01-21; !; : 342 |


:

:

,
==> ...

1682 - | 1626 -


© 2015-2024 lektsii.org - -

: 0.021 .