10min快速回顾C++语法(九)位运算与常用库函数专题

C++语法基础(九)位运算与常用库函数

12.1 位运算

符号 运算
& 与 AND
| 或 OR
~ 非 NOT
^ 异或 XOR
>> 右移
<< 左移
  • a >> k 等价于

  • a << k 等价于

  • 常用操作:

    • 求x的第k位数字 x >> k & 1

    • lowbit(x) = x & -x,返回x的最后一位1,以及它后后面的0

      在计算机中,-x = (~x+1)

      证明:

      image-20220826175429685

12.2 常用库函数

常用库函数一般都在#include<algorithm>中

12.2.1 reverse翻转

翻转一个vector:

reverse(a.begin(), a.end());

翻转一个数组:

//元素存放在下标1 ~ n
reverse(a + 1, a + n + 1);
//元素存放在下标0 ~ n-1
reverse(a , a + n );

12.2.2 unique去重

首先要保证相同元素在一起。

返回去重(只去掉相邻的相同元素)之后的尾迭代器(或指针),仍然为前闭后开,即这个迭代器是去重之后末尾元素的下一个位置

该函数常用于离散化,利用迭代器(或指针)的减法,可计算出去重后的元素个数。

把一个vector去重:

//差值就是数组中不同元素的数量
int m = unique(a.begin(), a.end()) – a.begin();
//通过erase函数将去重的后面的空闲元素删除
a.erase(unique(a.begin(),a.end()),a.end());

把一个数组去重,元素存放在下标1 ~ n:

//同上
int m = unique(a + 1, a + n + 1) – (a + 1);

12.2.3 random_shuffle随机打乱

用法与reverse相同。

由于随机种子是不变的,因此随机多次后的结果一样,如果想要不一样,可以把时间作为随机种子。

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
    //int a[] = {1,1,2,2,3,3,4};
    vector<int> a({1,2,3,4,5});
    srand(time(0));//time(0)返回现在到1970年1月1日的秒数
    random_shuffle(a.begin(), a.end( ));

    for(int x : a)cout << x << ' ';
    cout <<endl;

    return 0;
}

12.2.4 sort

对两个迭代器(或指针)指定的部分进行快速排序。可以在第三个参数传入定义大小比较的函数,或者重载“小于号”运算符。默认是从小到大排序。

sort(a.begin(),a.end());//从小到大
sort(a.begin(),a.end(),greater<int>());//从大到小

把一个int数组(元素存放在下标1 ~ n)从大到小排序,传入比较函数:

int a[MAX_SIZE];
bool cmp(int a, int b)//a是否应该排在b的前面
{
    return a > b;//如果a>b,则a应该排在b的前面
}
sort(a + 1, a + n + 1, cmp);//采用自定义排序
#include <ctime>

using namespace std;

/*struct Rec
{
    int x,y;
    bool operator < (const Rec &t) const
    {
        return x < t.x;//x小于t.x 的话,排在前面
    }        //前者x相当于a[0],后者t相当于a[1]
}a[5];

    //a[0] < a[1]
*/


struct Rec
{
int x,y;
}a[5];

bool cmp(Rec a,Rec b)//a是否应该排在b的前面
{
    return a.x < b.x;
}//可以定义一个比较函数,或者重载<号
int main()
{
    for ( int i = ; i < 5; i ++ )
    {
        a[i].x = -i;
        a[i].y = i;
    }
    for (int i = 0; i < 5; i ++ ) printf(" (%d ,%d) ",a[i].x,a[i].y);
    cout << endl;

    sort(a,a + 5, cmp);
    //sort(a,a + 5); 如果上面重载了,可以直接采用这种方式

    for (int i = 0; i < 5; i ++ ) printf("(%d ,%d) ",a[i].x,a[i].y);
    cout<< endl;

    return 0;
}

把自定义的结构体vector排序,重载“小于号”运算符:

struct Rec
{
    int id, x, y;
};

vector<Rec> a;

bool operator <(const Rec &a, const Rec &b)
{
        return a.x < b.x || a.x == b.x && a.y < b.y;
}

sort(a.begin(), a.end());

12.2.5 lower_bound/upper_bound 二分

首先必须保证有序

lower_bound的第三个参数传入一个元素x,在两个迭代器(指针)指定的部分上执行二分查找,返回指向第一个大于等于x的元素的位置的迭代器(指针)。否者返回空指针。

upper_bound的用法和lower_bound大致相同,唯一的区别是查找第一个大于x的元素。当然,两个迭代器(指针)指定的部分应该是提前排好序的。

在有序int数组(元素存放在下标1 ~ n)中查找大于等于x的最小整数的下标:

int i = lower_bound(a + 1, a + 1 + n, x) - a;

在有序vector<int>中查找小于等于x的最大整数(假设一定存在):</int>

int y = *--upper_bound(a.begin(), a.end(), x);

example

#include <iostream >
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

int main()
{
    int a[] = {1,2,4,5,6};

    int *p = lower_bound(a,a + 5,7);

    cout << *p << endl;

    return 0;
}
#C/C++##算法学习#
全部评论
带你10min快速回顾C++语法
点赞 回复
分享
发布于 2022-08-28 12:55 河南

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务