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

算法(Algorithm)为一个计算的具体步骤,常用于计算、数据处理和自动推理。C++ 算法库(Algorithms library)C++ 程序提供了大量可以用来对容器及其它序列进行算法操作的函数。这些组件可以为函数或函数模板,大部份由头文件 <algorithm> 提供,一小部份位于 <numeric><cstdlib> 中。

 

不修改内容的序列操作:

adjacent_find 查找两个相邻的等价元素
all_ofC++11 检测在给定范围中是否所有元素都满足给定的条件
any_ofC++11 检测在给定范围中是否存在元素满足给定条件
count 返回值等价于给定值的元素的个数
count_if 返回值满足给定条件的元素的个数
equal 返回两个范围是否相等
find 返回第一个值等价于给定值的元素
find_end 查找范围A 中与范围B 等价的子范围最后出现的位置
find_first_of 查找范围A 中第一个与范围B 中任一元素等价的元素的位置
find_if 返回第一个值满足给定条件的元素
find_if_notC++11 返回第一个值不满足给定条件的元素
for_each 对范围中的每个元素调用指定函数
mismatch 返回两个范围中第一个元素不等价的位置
none_ofC++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 <iostream>    
// std::cout
#include <algorithm>   
// std::adjacent_find
#include <vector>      
// 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<int> myvector (myints,myints+8);
  std::vector<int>::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 <iostream>    
// std::cout
#include <algorithm>   
// std::all_of
#include <array>       
// std::array

int main () {
  std::array<int,8> 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 <iostream>    
// std::cout
#include <algorithm>   
// std::any_of
#include <array>       
// std::array

int main () {
  std::array<int,7> 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 <iostream>    
// std::cout
#include <algorithm>   
// std::count
#include <vector>      
// 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<int> 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 <iostream>    
// std::cout
#include <algorithm>   
// std::count_if
#include <vector>      
// std::vector

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

int main () {
  std::vector<int> 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 <iostream>    
// std::cout
#include <algorithm>   
// std::equal
#include <vector>      
// 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::vector<int>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 <iostream>    
// std::cout
#include <algorithm>   
// std::find
#include <vector>      
// 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<int> myvector (myints,myints+4);
  std::vector<int>::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 <iostream>    
// std::cout
#include <algorithm>   
// std::find_end
#include <vector>      
// 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<int> haystack (myints,myints+10);

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

 
// using default comparison:
  std::vector<int>::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 <iostream>    
// std::cout
#include <algorithm>   
// std::find_first_of
#include <vector>      
// std::vector
#include <cctype>      
// 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<char> haystack (mychars,mychars+6);
  std::vector<char>::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 <iostream>    
// std::cout
#include <algorithm>   
// std::for_each
#include <vector>      
// 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<int> 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 <iostream>    
// std::cout
#include <algorithm>   
// std::mismatch
#include <vector>      
// std::vector
#include <utility>     
// std::pair

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

int main () {
  std::vector<int> 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<std::vector<int>::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 <iostream>    
// std::cout
#include <algorithm>   
// std::none_of
#include <array>       
// std::array

int main () {
  std::array<int,8> 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 <iostream>    
// std::cout
#include <algorithm>   
// std::search
#include <vector>      
// std::vector

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

int main () {
  std::vector<int> 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<int>::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 <iostream>    
// std::cout
#include <algorithm>   
// std::search_n
#include <vector>      
// 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<int> myvector (myints,myints+8);

  std::vector<int>::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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值