程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ 算法庫(1) 不修改內容的序列操作

C++ 算法庫(1) 不修改內容的序列操作

編輯:C++入門知識

不修改內容的序列操作:

adjacent_find 查找兩個相鄰的等價元素 all_of C++11 檢測在給定范圍中是否所有元素都滿足給定的條件 any_of C++11 檢測在給定范圍中是否存在元素滿足給定條件 count 返回值等價於給定值的元素的個數 count_if 返回值滿足給定條件的元素的個數 equal 返回兩個范圍是否相等 find 返回第一個值等價於給定值的元素 find_end 查找范圍 A 中與范圍 B 等價的子范圍最後出現的位置 find_first_of 查找范圍 A 中第一個與范圍 B 中任一元素等價的元素的位置 find_if 返回第一個值滿足給定條件的元素 find_if_not C++11 返回第一個值不滿足給定條件的元素 for_each 對范圍中的每個元素調用指定函數 mismatch 返回兩個范圍中第一個元素不等價的位置 none_of C++11 檢測在給定范圍中是否不存在元素滿足給定的條件 search 在范圍 A 中查找第一個與范圍 B 等價的子范圍的位置 search_n 在給定范圍中查找第一個連續 n 個元素都等價於給定值的子范圍的位置

adjacent_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
25
26
27
28
// adjacent_find example
#include // std::cout
#include // std::adjacent_find
#include // std::vector

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

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

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

if (it!=myvector.end())
std::cout << "the first pair of repeated elements are: " << *it << '\n';

//using predicate comparison:
it = std::adjacent_find (++it, myvector.end(), myfunction);

if (it!=myvector.end())
std::cout << "the second pair of repeated elements are: " << *it << '\n';

return 0;
}

輸出:

1
2
the first pair of repeated elements are: 30
the second pair of repeated elements are: 10

all_of

檢測在給定范圍中是否所有元素都滿足給定的條件

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

int main () {
std::array foo = {3,5,7,11,13,17,19,23};

if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
std::cout << "All the elements are odd numbers.\n";

return 0;
}

輸出:

1
All the elements are odd numbers.

any_of

檢測在給定范圍中是否存在元素滿足給定條件

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

int main () {
std::array foo = {0,1,-1,3,-3,5,-5};

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

return 0;
}

輸出:

1
There are negative elements in the range.

count

返回值等價於給定值的元素的個數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// count algorithm example
#include // std::cout
#include // std::count
#include // std::vector

int main () {
// counting elements in array:
int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements
int mycount = std::count (myints, myints+8, 10);
std::cout << "10 appears " << mycount << " times.\n";

// counting elements in container:
std::vector myvector (myints, myints+8);
mycount = std::count (myvector.begin(), myvector.end(), 20);
std::cout << "20 appears " << mycount << " times.\n";

return 0;
}

輸出:

1
2
10 appears 3 times.
20 appears 3 times.

count_if

返回值滿足給定條件的元素的個數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// count_if example
#include // std::cout
#include // std::count_if
#include // std::vector

bool IsOdd (int i) { return ((i%2)==1); }

int main () {
std::vector myvector;
for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9

int mycount = count_if (myvector.begin(), myvector.end(), IsOdd);
std::cout << "myvector contains " << mycount << " odd values.\n";

return 0;
}

輸出:

1
myvector contains 5 odd values.

equal

返回兩個范圍是否相等

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

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::vectormyvector (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 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";

return 0;
}

輸出:

1
2
Two 30s found at position 2
Two 10s found at position 5

特別說明:函數的中文釋義來自:http://classfoo.cn/cpp/head/76573_319/,例子來自:http://www.cplusplus.com/reference/algorithm/


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved