关于c++#include<algorithm>的用法,带讲解,包含c++11。(一)

非修改性序列操作12+5(c++11)

find

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int a[20]={0};
    a[0]=1;
    a[1]=2;
    a[2]=11;
    a[3]=19;
    a[4]=12;
    int *p;
    p=find(a,a+5,19);
    cout<<p-a<<endl;
    return 0;
}

返回第一个与之相同的地址,a表示头部指针,a+n中n表示元素个数,19表示查询的数值。
用p-a可以得到数组下标。
没有找到的话会p-a=n

find_if

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
bool cmp(int a)
{
    if(a==11)
        return 1;
    else
        return 0;
}
int main()
{
    int a[20]={0};
    a[0]=1;
    a[1]=2;
    a[2]=19;
    a[3]=11;
    a[4]=12;
    int *p;
    p=find_if(a,a+5,cmp);
    cout<<p-a<<endl;
    return 0;
}

可以自己写函数定义判断的规则,是不是感觉十分的费劲呢?
同理没有找到的话会p-a=n

count()

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
/*
bool cmp(int a)
{
    if(a==11)
        return 1;
    else
        return 0;
}*/
int main()
{
    int a[20]={0};
    a[0]=1;
    a[1]=2;
    a[2]=11;
    a[3]=11;
    a[4]=12;
    int p;
    p=count(a,a+5,11);
    cout<<p<<endl;
    return 0;
}

返回int数值,为个数。

count_if()

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
bool cmp(int a)
{
    if(a==11)
        return 1;
    else
        return 0;
}
int main()
{
    int a[20]={0};
    a[0]=1;
    a[1]=2;
    a[2]=11;
    a[3]=11;
    a[4]=12;
    int p;
    p=count_if(a,a+5,cmp);
    cout<<p<<endl;
    return 0;
}

可以自定义比较函数。

mismatch()
返回两个序列不同的地方
返回值为pair,没啥大用处,还不够麻烦的。
以下代码来自于官方。

// 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;
}

equal()
真不太清楚这玩意有啥大用途。
两个序列中的对应元素都相同时返回1,否则为0。
还是附上官方的代码样例。

// 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;
}

search()
这个东西好用,用于从a串中找出子串b的开始之前的一个。
没有任何优化和高级算法,就是双重循环而已。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char a[1000]="good good study!";
char b[100]="d s";
int main()
{
    char *p;
    p=search(a,a+strlen(a),b,b+strlen(b));
    cout<<p-a<<endl;
    return 0;
}

research_n()
在序列中找出一值的连续n次出现的位置
ps:手写一个不爽吗?

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int  a[1000]={1,2,3,4,4,5,6};
char b[100]="d s";
int main()
{
    int *p;
    p=search_n(a,a+7,2,4);
    cout<<p-a<<endl;
    return 0;
}

现在用的是老版本的codeblocks,所以暂时无法测试部分函数如c++11新增加的函数。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值