.


:




:

































 

 

 

 


.




STL . , , . , , . , . , , . , . f in d, a d ja c e n t_ f in d, co u n t, f o r _ e a c h, m ism atch, e q u a l

s e a r c h. a d j a c e n t_ f in d . , , . c o u n t , , . f o r _ e a c h f . m ism atch e q u a l . -: f i r s t l, l a s t l f i r s t 2. e q u a l tr u e, f i r s t l + / f i r s t 2 + i f i r s t l + i [ f i r s t l; l a s t l), f a ls e . m ism atch ( p a ir),11 f i r s t l + in f i r s t 2 + i, , . , l a s t l f i r s t 2 + (l a s t l - f i r s t l). s e a r c h , . strstr.

5.4. f in d i f

05-04." 97 =

#include <iostream>

#include <algorithm>

#include <cassert>

#include <vector>

using namespace std;

// :

class GreaterThanSO {

public:

bool operator()(int x) const { return x > 50; }

};

int main()

{

cout << " "

<< " find_if." << endl;

//

// 0, 1, 4, 9, 16,..., 144:

vector<int> vectorl;

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

vectorl.push_back(i * i);

vector<int>::iterator where;

where = find_if(vectorl.begin(), vectorl.e nd(),

GreaterThanSO());

assert (*where == 64);

cout << " -- Ok." << endl;

return 0;

}

5.5.

adj a c e n t f in d

"05-05." 976 =

#include <iostream>

#include <string>

#include <algorithm>

#include <cassert>

#include <functional>

#include <deque>

using namespace std;

int main()

{

cout << " "

<< " adjacent_find." << endl;

deque<string> player(5);

deque<string>::iterator i;

// :

player[0] = "Pele";

player[1] = "Platini";

player[2] = "Maradona";

player[3] = "Maradona";

player[4] = "Rossi";

// :

i = adjacent_find(player.begin(), player.e n d ());

assert (*i == "Maradona" && *(i+l) == "Maradona");

// , ,

// :

i = adjacent_find(player.begin(), player.e n d (),

greater<string>());

assert (*i == "Platini" && *(i+l) == "Maradona");

cout << " -- Ok." << endl;

return 0;

}

, .

, u n iq u e . , , , .. , .

co p y _ b ack w ard . copy(firstl/ lastl, first2) [ f i r s t l; l a s t l) [ f i r s t 2; l a s t 2) la s t2, la s t2 == f i r s t 2 + (l a s t l - f i r s t l). , f i r s t l, f i r s t l + 1,..., l a s t l - 1,

, f i r s t 2. , , , . copy_backw ard: cpy_backward(firstl, lastl, last2) [ f i r s t l; l a s t l) [ f i r s t 2; l a s t 2) f i r s t 2, f i r s t 2 == la s t2 - (l a s t l - f i r s t l). , l a s t l - 1, l a s t l - 2,..., f i r s t l. , , l a s t 2.

5.10. copy backward

"05- 1 0." 104 =

#include <iostream>

#include <cassert>

#include <algorithm>

#include <vector>

#include <string>

#include <iostream>

using namespace std;

int main()

{

cout << " "

<< " copy_backward." << endl;

string s ("abcdefghihklmnopqrstuvwxyz");

vector<char> vectorl(s.begin(), s.end());

vector<char> vector2(vectorl.size());

// vectorl vector2:

copy(vectorl.begin(), vectorl.e nd(),

vector2.begin());

assert (vectorl == vector2);

// vectorl 4 :

copy(vectorl.begin() + 4, vectorl.e nd(),

vectorl.begin());

assert (string(vectorl.begin(), vectorl.end()) ==

string("efghihklmnopqrstuvwxyzwxyz"));

// 2 :

copy_backward(vectorl.begin(), vectorl.e nd() - 2,

vectorl.e nd());

assert (string(vectorl.begin(), vectorl.end()) ==

string("efefghihklmnopqrstuvwxyzwx"));

cout << " -- Ok." << endl;

return 0;

}

copy v e c to r l v e c to r 2. , v e c to r l . , , ab ed, , , wxyz, . co p y _ b ack w ard ; . , e f, , , yz, . f i l l f i l l _ n . fill(first, last, value) l a s t - f i r s t v a lu e [ f i r s t; l a s t). fill_n(first, n, value) n v a lu e [ f i r s t; f i r s t + n). g e n e r a te [ f i r s t; l a s t) , l a s t - f i r s t g en ( g e n e r a te). , g en . ran d o m _ sh u f f le [ f i r s t; l a s t), , . , ran d o m _ sh u f f le, , .. N\ N \/N\. remove , . , .. . r e p la c e , , . r o t a t e . r o t a t e (f i r s t, m iddle, la s t) [ f i r s t; l a s t) m id d le - f i r s t . STL , s o r t, p a r t i a l s o r t s ta b le _ s o r t. , . p a r tia l_ _ s o r t , s o r t . , , s ta b le _ s o r t . STL : a c c u m u la te, p a r tia l_ s u m, a d ja c e n t_ d if f e r e n c e in n e r _ j? r o d u c t.

. a c c u m u la te .

 

49. STL. . . std::bind.

, / . C++ , ( ), o p e r a to r (). . PairSelect, , , . . . , , . , LessThan, , , < , CompareLastDigits, , , , , , true, , , false - .

// funobj3.cpp: operator!)

// .

#include <iostream.h>

template <class T>

struct LessThan {

bool operator()(const T &x, const T &y)const

{ return x < y;

}

};

struct CompareLastDigits {

bool operator()(int x, int y)const

{ return x % 10 < % 10;

}

};

141

template <class , class Compare>

class PairSelect {

public:

PairSelect(const T &x, const T &y): a(x), b(y){}

void PrintSmaller()const

{ cout << (Compare()(a, b)? a: b) endl;

}

private:

, b;

};

int main()

{ PairSelect<double, LessThan<double> > P (123.4, 98.7);

P.PrintSmaller(); // : 98.7

PairSelect<int, CompareLastDigits> Q (123, 98);

Q.PrintSmaller(); // : 123

return 0;

}

98.7, Q - 123.4. 123, - 3 - , 98. , , , , . (binder) . STL , . , , bind2nd. bind2nd(less<int>(), 100) , , 100. , // binder.: bind2nd , , 100.

♦include <iostream>

♦include <algorithm>

♦include <functional>

using namespace std;

int main()

{ int a [10] = {800, 3, 4, 600, 5, 6, 800, 71, 100, 2},

n = 0;

n = count_if(a, a + 10, bind2nd(less<int>(), 100));

cout << n endl; // : 6

return 0;

}

bindXst. , < 100 100 > , > 100. , binder. , c o u n t if : n = count_if(a, + 10, bindlst(greater<int>(), 100));

 

 





:


: 2016-07-29; !; : 742 |


:

:

, .
==> ...

1343 - | 1261 -


© 2015-2024 lektsii.org - -

: 0.027 .