位运算与常用库函数

本文介绍了C++中的位运算,包括与(AND)、或(OR)、取反(NOT)、异或(XOR)以及右移和左移操作,并提供了实例。此外,还讲解了常用的C++库函数,如reverse用于数组或vector的翻转,uniqe进行元素去重,random_shuffle打乱序列,以及sort进行排序。文章还提到了如何自定义比较函数进行排序和结构体的排序方法。
摘要由CSDN通过智能技术生成

位运算:

1.与(AND):0&0 = 0 , 0&1 = 0 , 1&0 = 0 , 1&1 = 1;

2.或(OR):0|0 = 0 , 0|1 = 1 , 1|0 = 1 , 1|1 = 1;

3.取反(NOT):~0 = 1 , ~1 = 0;

4.异或(XOR):0^0 = 0 , 1^1 = 0 , 1^0 = 1 , 0^1 = 1;

cout << (3^6) << endl;
    //int类型的&,|,~,^运算是将每一位上的数分别进行相应运算
    //3^6 = (011) ^ (110)
    //    = (0^1)(1^1)(1^0)
    //    = 101
    //    = 5

5.右移: a >> k ( a = a / pow(2 , k);

6.左移:a << k ( a = a * pow(2 , k);

int a = 1110011;
a >> 1 : a = 111001
a >> 2 : a = 11100

int b = 11
b << 1 : b = 110
b << 2 : b = 1100

常用操作
1.求x的第k位数字是0还是1(二进制)(从个位开始) : x >> k & 1
int a = 13; // a = 1101
for(int i = 0 ; i < 5 ; i ++)
    cout << (a >> 2 & 1 ) << ' '; 
// 1 0 1 1 0

2.lowbit(x) = x & -x || lowbit(x) = x & ~x+1 ,返回x的最后一位1

常用库函数:

1.reverse翻转

(1)翻转一个vector

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

(2)翻转一个数组,元素下标0~n-1

reverse(a , a + n );

示例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    vector<int>a ({1,2,3,4,5});
    reverse(a.begin() , a.end());

    int b[] = {1,2,3,4,5};
    reverse(a , a+5);

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

    for(int x : b) cout << x << ' ';

    return 0;
}

2.uniqe去重

返回去重(只去掉相邻的相同元素)之后的尾迭代器(或指针),仍然为前闭后开,即这个迭代器是去重之后末尾元素的下一个位置。该函数常用于离散化,利用迭代器(或指针)的减法,可计算出去重后的元素个数。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
	//数组去重
    int a[] = {1,1,2,2,3,3,4};
    int m = unique(a , a+7) - a; //数组中不重复元素个数
    cout << m << endl;
    for(int i = 0 ; i < m ; i ++) cout << a[i] << ' ';
    cout << endl;
	
	//vevtor去重
    vector<int>b({1,1,2,2,3,3,4});
    int n = unique(b.begin() , b.end()) - b.begin();
    cout << n << endl;
    for(int i = 0 ; i < n ; i ++) cout << b[i] << ' ';
    cout << endl;

    b.erase(unique(b.begin() , b.end()), b.end()); //删除去重后面的重复数,只留下去重后的数

    return 0;
}

3.random_shuffle随机打乱

用法与reverse相同。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9};
    random_shuffle(a , a+9);

    for(int i = 0 ; i < 9 ; i ++) cout << a[i] << ' ';
    
    return 0;
}

由于random_shuffle函数用到的随机种子是默认不变的,所以每次运行打乱顺序都一样,要想每次运行打乱顺序都不一样,一般可以把时间传进去当成随机种子,每次运行时间不一样,随机种子也就不一样了。

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
    srand(time(0));
    int a[] = {1,2,3,4,5,6,7,8,9};
    random_shuffle(a , a+9);

    for(int i = 0 ; i < 9 ; i ++) cout << a[i] << ' ';

    return 0;
}

4.sort排序

sort()参数跟reverse一样,对两个迭代器(或指针)指定的部分进行快速排序。

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
    srand(time(0));
    vector<int>a ({1,2,3,4,5,6,7,8,9});

    random_shuffle(a.begin() , a.end());
    for(int x : a) cout << x << ' ';
    cout << endl;

    sort(a.begin() , a.end()); //默认从小到大排序
    //sort(a.begin() , a.end() , greater<int>()); //从大到小排序
    for(int x : a) cout << x << ' ';
    cout << endl;


    return 0;
}

可以在第三个参数传入定义大小比较的函数,或者重载“小于号”运算符。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool cmp(int a , int b ) //自己编写函数判断a在什么情况下在b的前面
{}
int main()
{
    vector<int>a ({1,2,3,4,5,6,7,8,9});

    sort(a.begin() , a.end() , cmp); 
    for(int x : a) cout << x << ' ';
    cout << endl;
    

    return 0;
}

排序结构体可以自己定义比较函数

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

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 = 0 ; 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); 

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

也可以重载小于号

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Rec
{
    int x,y;
    bool operator < (const Rec &t) const{
        return x < t.x;
    }
}a[5];

int main()
{
    for(int i = 0 ; 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); 

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

5.lower_bound/upper_bound二分

前两个参数跟revesre一样
lower_bound的第三个参数传入一个元素x,在两个迭代器(指针)指定的部分上执行二分查找,返回指向第一个大于等于x的元素的位置的迭代器(指针)。
upper_bound的用法和lower_bound大致相同,唯一的区别是查找第一个大于x的元素。当然,两个迭代器(指针)指定的部分应该是提前排好序的。

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int a[] = {1 , 2 , 4 , 5 , 6};
    int *p = lower_bound(a , a+5 , 4);
    cout << *p << endl;

    return 0;
}

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

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

(2)在有序vector中查找大于等于x的最大整数(假设一定存在):

int i = lower_bound(a.begin() , a.end() , x) - a.begin();

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

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

6.next_permutation排列

next_permutation的两个参数跟reverse一样,返回传入数组的下一个排列(按字典序从小到大),返回的是最大序列时返回false。时间复杂度O(n)。

求数字数组全排列

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

using namespace std;

vector<vector<int> > permutation(vector<int>& nums)
{
    sort(nums.begin() , nums.end());
    vector<vector<int> > res;
    do res.push_back(nums); while(next_permutation(nums.begin() , nums.end()));
         
    return res;
}

int main()
{
    vector<int> a ({1,2,3});
    vector<vector<int> > ans =  permutation(a);

    for(vector<int> x : ans)
    {
        for(int y : x) cout << y << ' ';
        cout << endl;
    }

    return 0;
}

输出:

1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐川҉ ღ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值