• C++中的各种函数及用法(1)


    getchar()

    getchar()//输入字符

    #include
    using namespace std;
    char a; 
    int main(){
    a=getchar();
    cout<<a;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    putchar()

    putchar()//输出字符

    #include
    using namespace std;
    char a; 
    int main(){
    a=getchar();
    putchar(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    getline(cin,)

    getline(cin,);//输入整行字符串

    #include
    using namespace std;
    string a;
    int main(){
    getline(cin,a);
    cout<<a;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    gets()

    gets()//字符数组输入函数

    #include
    using namespace std;
    char a[105]; 
    int main(){
    gets(a);
    puts(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    puts()

    puts()//字符数组输出函数

    #include
    using namespace std;
    char a[105]; 
    int main(){
    gets(a);
    puts(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    strlen()

    strlen();//获取字符数组的长度

    #include
    using namespace std;
    char a[105];
    int len;
    int main(){
    cin>>a;
    len=strlen(a);
    cout<<len;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Sleep()

    Sleep()//停留多少毫秒

    #include
    #include//sleep()函数的头文件
    using namespace std;
    char a[105];
    int len;
    int main(){
    cin>>a;
    Sleep(1000);//停留1000毫秒
    cout<<a;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    unique(,)

    unique(,)//去除重复元素,要有头文件#include

    #include
    #include
    using namespace std;
    int n,a[105];
    int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    int t=unique(a+1,a+n+1)-a;
    cout<<t;
        return 0;
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    sin()

    sin()//数学里的正弦函数。参数的单位是度。

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<sin(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    cos()

    cos()//数学里的余弦函数。参数的单位是度。

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<cos(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    tan()

    tan()//该函数返回弧度角(double 型)的正切。

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<tan(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    log()

    log()//该函数返回参数的自然对数。

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<log(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    log2()

    log2()//求以2为底的对数函数值

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<log2(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    log10()

    log10()//求以10为底的对数函数值

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<log10(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    hypot(,)

    hypot(,)//该函数返回两个参数的平方总和的平方根,参数为一个直角三角形的两个直角边,函数会返回斜边的长度。

    #include
    using namespace std;
    int x,y;
    int main(){
    cin>>x>>y;
    cout<<hypot(x,y);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    abs()

    abs()//返回整型参数的绝对值

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<abs(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    fabs()

    fabs()//返回双精度参数的绝对值

    #include
    using namespace std;
    double a;
    int main(){
    cin>>a;
    cout<<fabs(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ceil()

    ceil()//返回不小于参数的最小整数(上取整)

    #include
    using namespace std;
    double a;
    int main(){
    cin>>a;
    cout<<ceil(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    floor()

    floor()//返回不大于参数的最大整数(下取整)

    #include
    using namespace std;
    double a;
    int main(){
    cin>>a;
    cout<<floor(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    pow(,)

    pow(,) //返回x的y次幂的值

    #include
    using namespace std;
    int x,y;
    int main(){
    cin>>x>>y;
    cout<<pow(x,y);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    sqrt()

    sqrt()//返回参数的平方根

    #include
    using namespace std;
    int a;
    int main(){
    cin>>a;
    cout<<sqrt(a);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    cin.sync()

    cin.sync();//清除cin造成的缓冲区

    #include
    using namespace std;
    int a;
    int main(){
    cin.sync();
    cin>>a;
    cout<<a;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    memset(,)

    memset(x,y,z)//将x中的每个数都变成y,z是x的长度

    #include
    using namespace std;
    int z,x[105],y;
    int main(){
    cin>>z;
    for(int i=1;i<=z;i++)cin>>x[i];
    cin>>y;
    memset(x,y,z);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    memcpy(,)

    memcpy(d,s+14,4)//将s中第14个字符开始的4个连续字符复制到d中。(从0开始)

    #include
    using namespace std;
    char s[105]="Hello My name is Justa",d[105];
    int main(){
    memcpy(d,s+14,4);
    cout<<d;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    reverse(,)

    reverse(x,x+y)//在区间[x,y)内的元素反转

    #include
    using namespace std;
    int n,x,y,a[105];
    int main(){
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++)cin>>a[i];
    reverse(x,x+y);
    for(int i=1;i<=n;i++)cout<<a[i]<<" ";
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    fmod(,)

    fmod(x,y)//计算x对y的模

    #include
    using namespace std;
    int x,y;
    int main(){
    cin>>x>>y;
    cout<<fmod(x,y);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    strcat(,)

    strcat(x,y)//把x接到y的后面

    #include
    using namespace std;
    string x,y;
    int main(){
    cin>>x>>y;
    cout<<strcat(x,y);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    .insert(,)

    x.insert(a,b)//在x的第a个位置插入b

    #include
    using namespace std;
    string x,b;
    int a;
    int main(){
    cin>>x>>a>>b;
    x.insert(a,b);
    cout<<x;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    .substr(,)

    x.substr(a,b)//在x的第a个位置取b个字符

    #include
    using namespace std;
    string x,d;
    int a,b;
    int main(){
    cin>>x>>a>>b;
    d=x.substr(a,b);
    cout<<d;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    .erase(,)

    x.erase(a,b)//在x的第a个位置删b个字符

    #include
    using namespace std;
    string x;
    int a,b;
    int main(){
    cin>>x>>a>>b;
    x.erase(a,b);
    cout<<x;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    replace(,)

    x.replace(a,b,c)//在x的第a个位置删b个字符 ,替换成c

    #include
    using namespace std;
    string x,c;
    int a,b;
    int main(){
    cin>>x>>a>>b>>c;
    x.replace(a,b,c);
    cout<<x;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    .find(,)

    x.find(a,b)//在x的第b个字符查找a的位置,找不到则返回string::npos

    #include
    using namespace std;
    string x,a;
    int b;
    int main(){
    cin>>x>>a>>b;
    cout<<x.find(a,b);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    fill(,)

    fill(x+1,x+n+1,d)//将x+1到x+n+1的数都变成d

    #include
    using namespace std;
    int n,x,d,a[105]; 
    int main(){
    cin>>n>>x>>d;
    for(int i=1;i<=n;i++) cin>>a[i];
    fill(x+1,x+n+1,d);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    isupper()

    isupper()//判断是否大写

    #include
    using namespace std;
    char n;
    int main(){
    cin>>n;
    if(isupper(n)) cout<<"YES";
    else cout<<"NO";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    islower()

    islower()//判断是否小写

    #include
    using namespace std;
    char n;
    int main(){
    cin>>n;
    if(islower(n)) cout<<"YES";
    else cout<<"NO";
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    strlwr()

    strlwr(x)//将x中的大写字母变小写字母

    #include
    using namespace std;
    char x[105];
    int main(){
    cin>>x;
    strlwr(x);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    strupr()

    strupr(x)//将x中的大写字母变小写字母

    #include
    using namespace std;
    char x[105];
    int main(){
    cin>>x;
    strupr(x);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    setw()

    setw()//设置场宽

    #include
    using namespace std;
    int main(){
    for(int i=1;i<=10;i++)
        cout<<setw(10)<<i;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    day02IP地址详解以及简单的DOS命令
    【无标题】
    Python基础语法:数据分析利器
    Word控件Spire.Doc 【图像形状】教程(3) :在 C#/VB.NET 中的指定位置插入图像
    第七章《Java的异常处理》第5节:自定义异常
    飞机机翼机身对接结构数值计算分析(ANSYS)
    Oauth2认证及Spring Security Oauth2授权码模式
    WPS未登录情况下的无法编辑,变灰色
    【开源】基于Vue.js的社区买菜系统的设计和实现
    【云原生】Rancher 从搭建到使用详解
  • 原文地址:https://blog.csdn.net/lzx_xzl_______/article/details/126089404