home | login | register | DMCA | contacts | help | donate |   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Э Ю Я

 fantasy space fantasy fantasy is horrors heroic prose   military   child   russian detective   action   child   ironical   historical   political western adventure adventure (child) child's stories love religion antique Scientific literature biography business home pets animals art history computers linguistics mathematics religion home_garden sport technique publicism philosophy chemistry close

## Examples

Fill a vector with random numbers. In this example, the function object is simply a function pointer.

`vector V(100);`

`generate(V.begin(), V.end(), rand);`

Sort a vector of double by magnitude, i.e. ignoring the elements' signs. In this example, the function object is an object of a user-defined class.

`struct less_mag : public binary_function {`

` bool operator()(double x, double y) { return fabs(x) < fabs(y); } `

`};`

`vector V;`

`…`

`sort(V.begin(), V.end(), less_mag());`

Find the sum of elements in a vector. In this example, the function object is of a user-defined class that has local state.

`struct adder : public unary_function {`

` adder() : sum(0) {}`

` double sum;`

` void operator()(double x) { sum += x; }`

`};`

`vector V;`

`…`

`adder result = for_each(V.begin(), V.end(), adder()); `

`cout << "The sum is " << result.sum << endl;`

Remove all elements from a list that are greater than 100 and less than 1000.

`list L;`

`…`

`list::iterator new_end = remove_if(L.begin(), L.end(), compose2(logical_and(), bind2nd(greater(), 100), bind2nd(less(), 1000)));`

`L.erase(new_end, L.end());`

Description | Standard Template Library Programmer`s Guide | Concepts