c++algorithm下的函数调用

这篇博客详细介绍了C++标准库中的一些常见算法,包括max和min用于找到最大值和最小值,abs获取整数绝对值,swap交换变量值,以及fill、sort、unique等操作。通过实例展示了如何在实际编程中应用这些算法,帮助提升代码效率和可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • max(x,y):返回x、y中的最大值。
  • min(x,y):返回x、y中的最小值。
// max example
#include <iostream>     // std::cout
#include <algorithm>    // std::max

int main () {
  std::cout << "max(1,2)==" << std::max(1,2) << '\n';
  std::cout << "max(2,1)==" << std::max(2,1) << '\n';
  std::cout << "max('a','z')==" << std::max('a','z') << '\n';
  std::cout << "max(3.14,2.73)==" << std::max(3.14,2.73) << '\n';
  return 0;
}
  • abs(x):返回整数x的绝对值,注意x必须是整数。浮点型的绝对值用math头文件下的fabs(x).
  • swap(x,y):交换x,y的值。 reverse(it,it2):将数组或容器的[it,it2]区间内的元素反转,it、it2为指针或者迭代器。常用于字符串的反转。
 int main () {

  int x=10, y=20;                              // x:10 y:20
  std::swap(x,y);                              // x:20 y:10

  std::vector<int> foo (4,x), bar (6,y);       // foo:4x20 bar:6x10
  std::swap(foo,bar);                          // foo:6x10 bar:4x20

  std::cout << "foo contains:";
  for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
  • fill(it,it2,value):把数组或容器中的某一段区间赋为某个值。
  • sort( 首元素地址(必填),尾元素地址(必填),比较函数(选填) ):实现数组或容器内的排序。
  • lower_bound(first,last,value):用来寻找在数组或容器的[first,last)范围内第一个值大于等于value的元素位置,如果是数组,则返回该位置的指针,如果是容器。则返回该位置的迭代器。
  • upper_bound(first,last,value):用来寻找在数组或容器的[first,last)范围内第一个值大于value的元素位置,如果是数组,则返回该位置的指针,如果是容器。则返回该位置的迭代器。
  • copy: copy(InputIterator first, InputIterator last, OutputIterator result)
    copy-backward(以相反的顺序copy)
  int myints[]={10,20,30,40,50,60,70};
  
  int copyArray[7];
  
  copy(myints,myints+7,copyArray);
  
  cout<<"copyArray:";
  
  for(int i=0;i<7;i++)
  {
  	cout<<copyArray[i]<<" ";
  }
  • count:功能类似于find,这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果
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;
}

运行结果

  • equal :比较范围内的元素和first2开始的范围的元素是否相等,如果范围内全部匹配,则返回true,否则返回false.
// 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;
}
  • unique:删除重复值,并返回最后位置
// unique algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::unique, std::distance
#include <vector>       // std::vector

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

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

  // using default comparison:
  std::vector<int>::iterator it;
  it = std::unique (myvector.begin(), myvector.end());   // 10 20 30 20 10 ?  ?  ?  ?
                                                         //                ^

  myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10

  // using predicate comparison:
  std::unique (myvector.begin(), myvector.end(), myfunction);   // (no changes)

  // print out content:
  std::cout << "myvector contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值