.


:




:

































 

 

 

 


, , , 3




, :

1) ,

2) ,

3)

, : find() count(), - : replace(). sort() 3.

find() , . , , . find() 4:

vector<int> v(10); //

// (1 3 4 6 4 5 3 4)

typedef vector<int>::iterator VI;

VI p = find (v.begin(), v.end(), 4);

if (p!= v.end())

cout << "element v["<< p-v.begin() << "] found" << endl; // 2

else

cout << "element not found\n";

, p-v.begin() , p.

, :

VI p_next = find (p+1,v.end()); // 4

count() :

int n = count (v.begin(), v.end(), 4); // n==3

.

replace

:

replace (v.begin(), v.end(), 4, 10); // (1 3 10 6 10 5 3 10)

// 4 10

, , , .

:

sort(p+1,v.end());

,

sort(find (v.begin(), v.end(), 4), v.end());

, .

 

2.

 

, find() replace().

1) , (, int) , (, string). , , , , .

find() , , , sort() , , ..

2) . find() , , .

3)

.

, , . - , (. .8.7 ).

, , , , . : . , , , .

 

2.1.

for_each().

 

, , for_each(). - , ( ) , start end.

for_each() :

for_each(start, end, fun);

start , end - , fun . , fun - , .

:

void print_v(int &elem) { //

cout << elem << " ";

}

vector<int> v(10);

// v

for_each (v.begin(), v.end(), print_v);

fun() , , . :

void add_2(int &elem) { // 2

elem += 2;

}

for_each (v.begin(), v.end(), add_2);

, . , , . .

class Adder {

int val;

public:

Adder (int i) { val = i; }

void operator() (int &elem) { elem += val; }

};

int. val, (, for_each()) , :

for_each (v.begin(), v.end(), Adder(10));

for_each (v.begin(), v.end(), Adder(20));

for_each Adder, , .

- . , .

, for_each ( ), , . :

template <class In, class Op>

Op for_each (In first, In second, Op fun) {

while (first!= second) fun(*first++);

return f;

}

fun , .

 

3. .

 

- , bool.

, bool , , bool.

, . (, ) , true , true, (, ) , true.

find() count(). find() count(), , , . , , . - find_if() count_if().

find_if() , . , - . , , , .

, , 4. , bool:

bool less_than_4 (int elem) {

return elem < 4;

}

typedef vector<int>::iterator VI;

VI p = find_if (v.begin(), v.end(), less_than_4);

if (p!= v.end())

cout << "element with index "<< p-v.begin() << " found" << endl;

:

class less_than {

int val;

public:

less_than (int i) { val = i; }

bool operator() (int elem) { return elem < val; }

};

VI p = find_if (v.begin(), v.end(), less_than(4));

(, <)

template <class T> class less_than {

T val;

public:

less_than (T i) { val = i; }

bool operator() (T elem) { return elem < val; }

};

VI p = find_if (v.begin(), v.end(), less_than<int>(4));

count_if() , . , find_if(), ( ).

, , 4.

less_than:

int n = count_if (v.begin(), v.end(), less_than<int>(4));

 

3.1.

 

, less_than. , , .

<functional>. logical_not (!) equal_to (==) not_equal_to (!=), greater (>), less (<), greater_equal (>=), less_equal (<=), logical_and (&&), logical_or (||). , , .

( ) . mismatch(), , , pair , .

pair <In,In> mismatch (In begin1,In end1,In begin2,BinPred p);

In - , BinPred - , . , , , .

, ==, :

//

pair<VI,VI> pv = mismatch (v1.begin(),v1.end(),v2.begin());

, , , < ( , , ), less:

pair<VI,VI> pv = mismatch (v1.begin(),v1.end(),v2.begin(),

less<int>());

cout << "mismatch at element " << pv.first-v1.begin() << endl;

// v1 (3,4,5,6) v2 (5,6,3,7) 2

.

mismatch() equal():

bool equal (first1, last1, first2 [,bin_pred]);

( - ). , equal() true, false.

. find_if() less_than.

less_than less , ( ). , less, .. - . - , .

, . , (binders), .

:

bind1st(fun,x) - fun

x

bind2nd(fun,x) -

bind2nd():

VI p = find_if (v.begin(), v.end(), bind2nd (less<int>(), 4));

less find_if() , - 4.

, 20:

int n = count_if (v.begin(), v.end(),

bind2nd(greater_equal<int>(),20));

(negators). , . : not1() , not2() . :

int n = count_if (v.begin(), v.end(), bind2nd (not2 (less<int>()), 20));

less, 20.

 

4.

 

, ,

. :

class X {

friend bool pred_X (X&);

int key;

public:

X(int i) { key = i; }

};

bool X_less_than_100 (X& elem) { return elem.key < 100; }

vector<X> v;

int n = count_if(v.begin(), v.end(), X_less_than_100);

,

. - ( - ). mem_fun() mem_fun_ref(). - , , mem_fun() (.. ), mem_fun_ref() - (.. ). :

class Y {

int key;

public:

Y(int i) { key = i; }

bool less_than_100 () { return key < 100; }

bool less_than (int n) { return key < n; }

int print () { cout << "Y(" << key << ") "; return 0; }

};

vector<Y*> vp;

vector<Y> vy;

// , -

// true

int nx = count_if(vp.begin(), vp.end(), mem_fun(&Y::less_than_100));

//

for_each (vy.begin(), vy.end(), mem_fun_ref(&Y::print));

- less_than_100().

- mem_fun1() mem_fun1_ref():

int nn = count_if(vp.begin(), vp.end(), bind2nd(mem_fun1(&Y::less_than),100));

- less_than(), 100. - , .

vector<int> vi;

bool b = equal (vp.begin(), vp.end(), vi.begin(),

mem_fun1(&Y::less_than));

vp - less_than() vi.

 

5. ,

 

, find(), find_if(), count(), count_if(), mismatch() equal(). (Iter - , first last Iter).

1) Iter find_first_of (first1, last1, first2, last2 [,bin_pred]);

, , . bin_pred , ( - ==)

(1 3 5 3 5 6) (7 4 5 3) -> "3"

2) Iter adjacent_find (first, last [,bin_pred]);

(1 2 3 3 5 6) -> "3"

3) Iter search (first1, last1, first2, last2 [,bin_pred]);

, , (1 3 5 3 5 6) (5 3 5) -> "5"

4) Iter find_end (first1, last1, first2, last2 [,bin_pred]);

, , (1 3 5 3 5 6) (5 3 5) -> "5"

5) Iter min_element(first, last[, bin_pred]);

. max_element().

 

6. . .

, sort(), . sort() less, true, , .

:

class Y {

friend class YCmp;

//...

};

class YCmp {

public:

bool operator() (const Y& y1, const Y& y2) {

return y1.key < y2.key;

}

};

vector<Y> vy;

sort (vy.begin(), vy.end(), YCmp());

- :

class Y {

//...

public:

bool less_than (Y& y) { return key < y.key; }

};

sort (vy.begin(), vy.end(), mem_fun1_ref(&Y::less_than));

less_than() . "". sort() , , :

class Y {

//...

public:

bool operator<(Y& y) { return key < y.key; }

};

sort (vy.begin(), vy.end());

 

, .

1) partial_sort(first, middle, last[, bin_pred]);

- first middle

, first last.

2) partial_sort_copy(first1, last1, first2, last2[, bin_pred]);

N , N -

.

3) binary_search(first, last, val [, bin_pred]);

val

, equal_range(), lower_bound() upper_bound(), multimap(), .

4) unique(first, last, [, bin_pred]);

, (.. ):

(1 2 2 1 4 5 3 2 1) -> (1 2 4 5 3 2 1 2 1) "3"

 

7. , -

 

, - copy(). :

copy (first, last, res);

copy(), . :

vector<int> v1(10); //

vector<int> v2;

copy (v1.begin(), v1.end(), v2.begin()); //

, , copy(), .

- , - . v2 .

, :

vector<int> v2(10);

copy (v1.begin(), v1.end(), v2.end()); //

 

- - (inserters). . :

back_inserter(c) - c

front_inserter(c) -

inserter(c,i) - c i

:

vector<int> v1(10);

vector<int> v2;

copy (v1.begin(), v1.end(), back_inserter(v2)); // OK

v2 .

 

.

1) copy_backward(first, last, res);

2) transform(first, last, res, un_op);

transform(first1, last1, first2, res, bin_op);

- , ( ) un_op bin_op ( , - ).

Y:

class Y {

friend int get_key(Y& y);

};

int get_key(Y &y) { return y.key; }

vector<Y> vy(10); //

vector<int> vi;

transform (vy.begin(), vy.end(), back_inserter(vi), get_key);

:

class Y {

int get_key() { return key; }

};

vector<Y> vy(10); //

vector<int> vi;

transform (vy.begin(), vy.end(), back_inserter(vi),

mem_fun_ref(&Y::get_key));

bin_op .

3) unique_copy(first, last, res [,bin_pred]);

unique(), .

4) replace. :

replace_if(first, last, pred, new_val);

, pred true, val:

replace_if(v.begin(),v.end(),bind2nd(less<int>(),4),6);

// , 4, 6

:

replace_copy (first, last, res, val, new_val);

replace_copy_if (first, last, res, pred, new_val);

5) fill(first, last, val);

val

fill_n(res, n, val);

res n val

fill_n(back_inserter(v), 10, 0);

// n 10 0

6) generate (first, last, gen_op);

gen_op(). gen_op , , , . , , , .

generate_n(res, n, gen_op);

res n gen_op()

generate_n(back_inserter(v), 10, rand);

v 10 .

 

8.

 

: plus (+), minus(-), multiplies(*), divides(/), modulus(%) negate( -).

, , , transform():

//

transform(v1.begin(),v1.end(),v2.begin(),back_inserter(res),

multiplies<int>());

<functional>.

, <numeric>. (T - , - ):

1) T accumulate<first, last, T init [, bin_op]);

. init, - :

int sum = accumulate(v.begin(), v.end(), 0);

int prod = accumulate(v.begin(), v.end(), 1, multiplies<int>());

. , - :

class Y {

friend int y_add(int, Y&);

};

int y_add(int val, Y& y) {

return val + y.key;

}

int ysum = accumulate(vy.begin(), vy.end(), 0, y_add);

2) T inner_product(first, last, first2, last2 [, bin_op1, bin_op2]);

.

( , ), , bin_op2, - bin_op1.

 

9.

 

, .. ( ), ( ).

:

ostream_iterator

istream_iterator

, :

1) -, - :

ostream_iterator<int> os(cout, " ");

2) - , , copy():

copy (v.begin(), v.end(), os);

transform():

//

transform(v1.begin(), v1.end(), v2.begin(), os, multiplies<int>());

, :

class Y {

friend ostream& operator<<(ostream&, const Y&);

};

ostream& operator<<(ostream& o, const Y& y) {

o << y.key; return o;

}

ofstream ofile ("test.txt");

ostream_iterator<Y> osy(ofile, " ");

vector<Y> vy; //

copy (vy.begin(), vy.end(), osy); //

3) - . istream_iterator<T>() (





:


: 2017-02-25; !; : 344 |


:

:

: , , , , .
==> ...

1689 - | 1544 -


© 2015-2024 lektsii.org - -

: 0.181 .