码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 24.【特殊函数篇==(十篇一章,持续更新~)】


    特殊函数

    • 特殊函数 (一直更新)
      • 1.isprintf(s[i]);判断一个字符是否能输出
          • 1.1代码展示:
          • 1.2效果展示:
      • 2.setw()函数的运用
          • 2.1理论知识:
          • 2.2空格补齐型隔离(n>1)
          • 代码展示:
          • 效果展示:
          • 2.3整体输出型 紧接(n<1)
          • 效果展示:
          • 2.4只对接着的有效
          • 代码展示:
          • 效果展示:
      • 3.setfill()补充填充
          • 代码展示:
          • 效果展示
      • 4.sort()函数排序问题
          • 4.1理论展示:
          • 4.2 默认不写(为升序)
          • 代码展示
          • 效果展示:
          • 4.3写了cmp依旧为升序:
          • 代码展示
          • 效果展示:
          • 4.4写了cmp为降序
          • 代码展示
          • 效果展示:
      • 5.求最大公因数(gcd)
          • 1.头文件 #include
          • 2.原型gcd(非float,非float);
          • 3.基本思路和方法
          • 3.代码展示:
          • 4.效果展示:
    • (六)、put级联
      • 1.定义
          • 代码展示:
          • 效果展示:
    • (七)、pow(int a,int b)函数用来实现a的b次方
          • 代码展示:
          • 效果展示:
    • (八)、延时操作 (Sleep)
      • 代码展示
      • 效果展示:

    特殊函数 (一直更新)

    1.isprintf(s[i]);判断一个字符是否能输出

    1.1代码展示:

    #include 
    #include 
    using namespace std;
    int main()
    {
     string s = "12d1 2 ddsd";
     int d = 0;
     for (int i = 0; i<s.length(); i++)
     {
      if (s[i] != ' ' && isprint(s[i])) //判断不为空格 且能输出;
      {
       s[d++] = s[i];
      }
     }
     cout << "改变后的字符串是:" << endl;
     for (int i = 0; i < d; i++)
     {
      cout << s[i];
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    1.2效果展示:

    在这里插入图片描述

    2.setw()函数的运用

    2.1理论知识:

    setw(int n)是c++中在输出操作中使用的字段宽度设置,设置输出的域宽,n表示字段宽度。只对紧接着的输出有效,紧接着的输出结束后又变回默认的域宽。

    当后面紧跟着的输出字段长度小于n的时候,在该字段前面用空格补齐;当输出字段长度大于n时,全部整体输出。
    在这里插入图片描述

    2.2空格补齐型隔离(n>1)

    代码展示:

    #include 
    #include 
    using namespace std;
    int main()
    {
        int a[3] = {1,2,3};
        for (int i = 0; i < 3; i++)
        {
            cout << setw(2) << a[i];     //setw(n)
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    效果展示:

    在这里插入图片描述

    2.3整体输出型 紧接(n<1)

    #include 
    #include 
    using namespace std;
    int main()
    {
     int a[3] = {1,2,3};
     for (int i = 0; i < 3; i++)
     {
      cout << setw(0) << a[i];     //setw(n)
     }
     return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    效果展示:

    在这里插入图片描述

    2.4只对接着的有效

    代码展示:

    #include 
    #include 
    using namespace std;
    int main()
    {
     int a[3] = {1,2,3};
     for (int i = 0; i < 3; i++)
     {
      cout << setw(2) << a[i]<<4;
     }
     return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    效果展示:

    在这里插入图片描述

    3.setfill()补充填充

    代码展示:

    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
     string s = "123";
     cout << setw(5) << setfill('*') << s << endl; // 共设置5个空间,用不满的在前面加*,用不满的话不用
     return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    效果展示

    在这里插入图片描述

    4.sort()函数排序问题

    4.1理论展示:

    1.如果用sort进行排序,那么需要用头文件#include

    2、sort模板含三个参数:

    sort (begin,end,cmp)

    参数begin:要排序数组的起始地址(第一个数据的地址)

    参数end:最后一个数据的下一个数据的地址

    参数cmp:若这个参数不写,默认为升序

    4.2 默认不写(为升序)

    代码展示

    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
     int i;
     string s;
     cin >> s;
     sort(s.begin(), s.end()); 
     for (i = 0; i < s.length(); i++)
     {
      cout << s[i] << " ";
     }
     return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    效果展示:

    在这里插入图片描述

    4.3写了cmp依旧为升序:

    代码展示

    #include 
    #include 
    using namespace std;
    bool cmp(int a,int b)
        {
            return a<b;     //或则在cmp函数里面写return a
        }
    int main()
    {
        int a[]={6,5,8,4,3,2},i;
        sort(a,a+6,cmp);
        for(i=0;i<5;i++)
        {
            cout<<a[i]<<"    ";
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    效果展示:

    在这里插入图片描述

    4.4写了cmp为降序

    代码展示

    #include 
    #include 
    using namespace std;
    bool cmp(int a,int b)
        {
            return a>b;     //注意这里是a>b是指降序
        }
    int main()
    {
        int a[]={6,5,8,4,3,2},i;
        sort(a,a+6,cmp);
        for(i=0;i<5;i++)
        {
            cout<<a[i]<<"    ";
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    效果展示:

    在这里插入图片描述

    5.求最大公因数(gcd)

    1.头文件 #include

    2.原型gcd(非float,非float);

    3.基本思路和方法

    while()循环方法

    int gcd(int a,int b) {
    int r;
    while (a%b!=0)
    {
    r=a%b;
    a=b;
    b=r;
    }
    return b; }

    2.递归+三元运算符

    int gcd(int a, int b) { return b > 0 ? gcd(b, a % b) : a; }

    3.递归+ if 语句

    求x 和 y 的最大公约数,就是求 y 和 x % y 的最大公约数

    int gcd(int a,int b) {
    if(a%b==0)
    return b;
    else
    return (gcd(b,a%b)); }

    3.代码展示:

    #include 
    #include 
    using namespace std;
    int gcd(int a, int b)
    {
    	return b > 0 ? gcd(b, a % b) : a;
    }
    int main()
    {
    	int a, b;
    	cin >> a >> b;
    	cout << gcd(a, b) << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.效果展示:

    在这里插入图片描述

    (六)、put级联

    1.定义

    级联(cascade)在计算机科学里指多个对象之间的映射关系,建立数据之间的级联关系提高管理效率

    代码展示:

    #include 
    #include 
    using namespace std;
    int main()
    {
    	cout.put('C').put('+').put('+').put('\n');
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    效果展示:

    在这里插入图片描述

    (七)、pow(int a,int b)函数用来实现a的b次方

    代码展示:

    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a = 3, b = 0;
    	int c=pow(a, 1);
    	cout << c << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    效果展示:

    在这里插入图片描述

    (八)、延时操作 (Sleep)

    #include
    Sleep(1000); //以毫秒为单位

    代码展示

    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a;
    	Sleep(1000);
    	cin >> a;
    	cout << a << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    效果展示:

    在这里插入图片描述

  • 相关阅读:
    科普文章|一文了解平行链及其优势
    亚商投资顾问 早餐FM/12022023年开展第五次全国经济普查
    排序:快速排序(C/C++)
    Linux操作系统:函数、CronTab及定时任务和站点可用性监测
    IT廉连看——C语言——操作符
    2.【openCV常用函数模板】
    yolov7模型部署——环境搭建(python 导出onnx模型,c# OnnxRunTime-GPU版调用) 踩坑记录
    关于python创建项目时的一些基础的概念
    大家都能看得懂的源码之ahooks useInfiniteScroll
    9、大小屏分离与精细化审核
  • 原文地址:https://blog.csdn.net/qq_69683957/article/details/126336269
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号