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

2014-11-24 08:15:54 · 作者: · 浏览: 3
i) {std::cout << ' ' << i;}
} myobject;

int main () {
std::vector myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);

std::cout << "myvector contains:";
for_each (myvector.begin(), myvector.end(), myfunction);
std::cout << '\n';

// or:
std::cout << "myvector contains:";
for_each (myvector.begin(), myvector.end(), myobject);
std::cout << '\n';

return 0;
}

输出:

1
2
myvector contains: 10 20 30
myvector contains: 10 20 30

mismatch

返回两个范围中第一个元素不等价的位置

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
// mismatch algorithm example
#include // std::cout
#include // std::mismatch
#include // std::vector
#include // std::pair

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

int main () {
std::vector myvector;
for (int i=1; i<6; i++) myvector.push_back (i*10); // myvector: 10 20 30 40 50

int myints[] = {10,20,80,320,1024}; // myints: 10 20 80 320 1024

std::pair ::iterator,int*> mypair;

// using default comparison:
mypair = std::mismatch (myvector.begin(), myvector.end(), myints);
std::cout << "First mismatching elements: " << *mypair.first;
std::cout << " and " << *mypair.second << '\n';

++mypair.first; ++mypair.second;

// using predicate comparison:
mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);
std::cout << "Second mismatching elements: " << *mypair.first;
std::cout << " and " << *mypair.second << '\n';

return 0;
}

输出:

1
2
First mismatching elements: 30 and 80
Second mismatching elements: 40 and 320

none_of

检测在给定范围中是否不存在元素满足给定的条件

1
2
3
4
5
6
7
8
9
10
11
12
13
// none_of example
#include // std::cout
#include // std::none_of
#include // std::array

int main () {
std::array foo = {1,2,4,8,16,32,64,128};

if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
std::cout << "There are no negative elements in the range.\n";

return 0;
}

输出:

1
There are no negative elements in the range.

search

在范围 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
33
34
35
36
// search algorithm example
#include // std::cout
#include // std::search
#include // std::vector

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

int main () {
std::vector haystack;

// set some values: haystack: 10 20 30 40 50 60 70 80 90
for (int i=1; i<10; i++) haystack.push_back(i*10);

// using default comparison:
int needle1[] = {40,50,60,70};
std::vector ::iterator it;
it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4);

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

// using predicate comparison:
int needle2[] = {20,30,50};
it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, mypredicate);

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

return 0;
}

输出:

1
2
match1 found at position 3
match2 not found

search_n

在给定范围中查找第一个连续 n 个元素都等价于给定值的子范围的位置

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
33
// search_n example
#include // std::cout
#include // std::search_n
#include // std::vector

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

int main () {
int myints[]={10,20,30,30,20,10,10,20};
std::vector myvector (myints,myints+8);

std::vector ::iterator it;

// using default comparison:
it = std::search_n (myvector.begin(), myvector.end(), 2, 30);

if (it!=myvector.end())
std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';
else
std::cout << "match not found\n";

// using predicate comparison:
it = std::search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);

if (it!=myvector.end())
std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';
else
std::cout << "match not found\n";

retu