写法

  1. lower_bound()
  2. 字符串toupper
  3. to_string()
  4. gcd()

定义二维vector

vector<vector <int> > vec(n ,vector<int>(m)); 

生成组合数

//求组合数的模板要背会
for (int i = 0; i <= n; i ++ )
    for (int j = 0; j <= i; j ++ )
        if (!j) C[i][j] = 1;
    else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;

lower_bound()

返回值是一个迭代器,返回指向大于等于key的第一个值的位置

对象:有序数组或容器

数组:

#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int a[]={1,2,3,4,5,7,8,9};
    printf("%d",lower_bound(a,a+8,6)-a); 

    return 0;    
} 

输出:5

将key换成10,所有val都小于key,返回last的位置

#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int a[]={1,2,3,4,5,7,8,9};
    printf("%d",lower_bound(a,a+8,10)-a); 

    return 0;    
} 

输出: 8

vector:

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

int main()
{
    vector<int> A;
    A.push_back(1); 
    A.push_back(2); 
    A.push_back(3); 
    A.push_back(4); 
    A.push_back(5); 
    A.push_back(7); 
    A.push_back(8); 
    A.push_back(9); 

    int pos = lower_bound(A.begin() , A.end() , 6)-A.begin();
    cout << pos << endl;
    return 0;  
}

输出还是5

对应lower_bound()函数是upper_bound()函数,它返回大于等于key的最后一个元素

也同样是要求有序数组,若数组中无重复元素,则两者返回值相同

判断a数组中有没有x:(a数组有序)

int t = lower_bound(a,a+n,x)-a;
if(a[t] != x)cout << "NO" << endl;
else cout << "YES" << endl;

字符串toupper

string s;

cout << (char)toupper(s[i]);

cout<<char(st[i]-32)

to_string()

看某个数字的某一位是否有7:

to_string(i).find(“7”) != -1

gcd()

int gcd(int a,int b){

​ return b ? gcd(b, a % b) :a;

}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1149440709@qq.com

×

喜欢就点赞,疼爱就打赏