C++ 算法库(1) 不修改内容的序列操作(二)

2014-11-24 08:15:54 · 作者: · 浏览: 4

bool mypredicate (int i, int j) {
return (i==j);
}

int main () {
int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100
std::vector myvector (myints,myints+5); // myvector: 20 40 60 80 100

// using default comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";

myvector[3]=81; // myvector: 20 40 60 81 100

// using predicate comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";

return 0;
}

输出:

1
2
The contents of both sequences are equal.
The contents of both sequence differ.

find

返回第一个值等价于给定值的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// find example
#include // std::cout
#include // std::find
#include // std::vector

int main () {
int myints[] = { 10, 20, 30 ,40 };
int * p;

// pointer to array element:
p = std::find (myints,myints+4,30);
++p;
std::cout << "The element following 30 is " << *p << '\n';

std::vector myvector (myints,myints+4);
std::vector ::iterator it;

// iterator to vector element:
it = find (myvector.begin(), myvector.end(), 30);
++it;
std::cout << "The element following 30 is " << *it << '\n';

return 0;
}

输出:

1
2
The element following 30 is 40
The element following 30 is 40

find_end

查找范围 A 中与范围 B 等价的子范围最后出现的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// find_end example
#include // std::cout
#include // std::find_end
#include // std::vector

bool myfunction (int i, int j) {
return (i==j);
}

int main () {
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector haystack (myints,myints+10);

int needle1[] = {1,2,3};

// using default comparison:
std::vector ::iterator it;
it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);

if (it!=haystack.end())
std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';

int needle2[] = {4,5,1};

// using predicate comparison:
it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);

if (it!=haystack.end())
std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';

return 0;
}

输出:

1
2
needle1 found at position 5
needle2 found at position 3

find_first_of

返回第一个值不满足给定条件的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// find_first_of example
#include // std::cout
#include // std::find_first_of
#include // std::vector
#include // std::tolower

bool comp_case_insensitive (char c1, char c2) {
return (std::tolower(c1)==std::tolower(c2));
}

int main () {
int mychars[] = {'a','b','c','A','B','C'};
std::vector haystack (mychars,mychars+6);
std::vector ::iterator it;

int needle[] = {'A','B','C'};

// using default comparison:
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);

if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';

// using predicate comparison:
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+3, comp_case_insensitive);

if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';

return 0;
}

输出:

1
2
The first match is: A
The first match is: a

for_each

对范围中的每个元素调用指定函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// for_each example
#include // std::cout
#include // std::for_each
#include // std::vector

void myfunction (int i) { // function:
std::cout << ' ' << i;
}

struct myclass { // function object type:
void operator() (int