C++ 11 commonly used library functions

 

 

 

  1. Bit operation

& versus

| Or

~ Non

^ XOR

>> Right shift (a right shift by k bits is equivalent to a / 2 to the k power)

<< Left shift  (a left shift by k bits is equivalent to a * (2 to the k power))

#include<iostream>
using namespace std;
int main()
{
    int a = 4;
    //2
    cout << (a >> 1) << endl;
}

 

Common operations :

  1. Find the k-th digit of x x >> k & 1
  2. lowbit(x) = x & -x, returns the last digit of x 1 such as: 100010100 returns 100

 

Common library functions,

  1. reverse

① Flip a vector:

#include<iostream>
#include<vector>

//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    vector<int> a({1,2,3,4,5});
    reverse(a.begin(),a.end());
    for (auto x : a) cout << x << " ";
    cout << endl;
}

The results are as follows:

 

② Flip an array, the elements are stored in subscripts 1~n:

#include<iostream>
#include<vector>

//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    int a[] = {1,2,3,4,5};
    //reverse函数里面的两个参数分别为需要翻转的起始位置和结束位置的下一个位置
    //翻转整个数组 {5,4,3,2,1}
    reverse(a,a + 5);
    for (auto x : a) cout << x << " ";
    cout << endl;
    //翻转前三个元素 {3,4,5,2,1}
    reverse(a,a + 3);
    for (int x : a) cout << x << " ";
    cout << endl;
}

     Code running results:

     

 

 

     2.unique to heavy

     Return the tail iterator (or pointer) after deduplication, which is still closed before opening, that is, this iterator is the next position of the last element after deduplication. This function is often used for discretization, using iterator (or pointer) subtraction to calculate the number of elements after repetition. Simply put, calling the unique function can deduplicate the array and return the number of elements in the array after deduplication

 

#include<iostream>

//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    int a[] = {1,1,2,2,3,3,4};
    //对数组a去重并返回去重之后数组中的元素个数
    int n = unique(a,a + 7) - a;
    cout << n << endl;
    //遍历数组
    for (int i = 0;i < n;i++) cout << a[i] << " ";
    cout << endl;
    return 0;
}

      operation result:

      

 

 

      3.random_shuffle random shuffle (the usage is the same as reverse)

#include<iostream>
#include<vector>

//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    vector<int> a({1,2,3,4,5});
    //打乱vector中元素的顺序
    random_shuffle(a.begin(),a.end());
    for (auto x : a) cout << x << " ";
    cout << endl;
    return 0;
}

      operation result:

      

 

      4.sort

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

//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    vector<int> a({1,2,3,4,5});
    //设置随机种子,使每次打乱的顺序都不相同
    srand(time(0));
    //打乱vector中元素的顺序
    random_shuffle(a.begin(),a.end());
    //遍历vector
    for (auto x : a) cout << x << " ";
    cout << endl;
    //排序,默认从小到大
    sort(a.begin(),a.end());
    for (auto x : a) cout << x << " ";
    cout << endl;
    return 0;
}

  

 

 

 

 

       5.lower_bound/upper_bound  二分

       The third parameter of lower_bound passes in an element x, performs a binary search on the part specified by the two iterators (pointers), and returns an iterator (pointer) that points to the position of the first element greater than or equal to x .

       The usage of upper_bound is roughly the same as lower_bound, the only difference is to find the first element greater than x . Of course, the parts specified by the two iterators (pointers) should be sorted in advance.

#include<iostream>

//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    int a[] = {1,2,4,5,6};
    //在下标0-5的范围内查找第一个大于等于3的元素  4
    int *p = lower_bound(a,a + 5,3);
    cout << *p << endl;
    //在下标1-3的范围内查找第一个严格大于4的元素  5
    int *q = upper_bound(a + 1,a + 3,4);
    cout << *q << endl;
}

 

 

#include<iostream>
#include<vector>
//algorithm为算法库
#include<algorithm>

using namespace std;
int main()
{
    vector<int> a = {1,2,4,5,6};
    //t表示的是vector中第一个大于等于4的元素的下标
    int t = lower_bound(a.begin(),a.end(),4) - a.begin();
    cout << a[t] << endl;
    //r表示的是vector中第一个严格大于4的元素的下标
    int r = upper_bound(a.begin(),a.end(),4) - a.begin();
    cout << a[r] << endl;
    return 0;
}

 

 

 

Guess you like

Origin blog.csdn.net/smallrain6/article/details/105944538