• (蓝桥杯C/C++)—— 编程基础



    一、C++基础格式

    1.打印hello, world

    #include   //万能头文件

    using namespeace std;

    int main()

    {

          cout << "hello, world" << endl;

          printf ("hello, world");

          return 0;

    }

    2.基本数据类型

    int  a = 1;                                //整数型

    dobule b = 3.14;                     //浮点型(小数)

    char c = 'A';                               //字符型

    char d[]  = "hello";                     //字符串

    bool e = 1(true) / 0(false);         //布尔类型(判断真假)

    二、string

    1.string简介    

    string是C++标准库的重要组成部分,主要用于字符串处理。

    使用string库需要在头文件中包括该库 #include
    string与char[]不同,string实现了高度的封装,可以很方便地完成各种字符串的操作,比如拼接、截取、匹配等等。

    (1)字符串管理:string封装了字符串的存储和管理。它自动处理字符串的内存分配和释放,避免了手动管理内存的麻烦。

    (2) 动态大小调整:string可以根据需要自动调整字符串的大小。在添加或删除字符时,string会自动调整内部的存储容量,确保足够的空间来容纳字符串。

    (3)安全性: string提供了一些方法来确保字符串的安全性。例如,它提供了越界访问检查,以避
    免访问超出字符串范围的字符。

    (4) 迭代器支持:string支持迭代器,可以使用迭代器遍历字符串中的字符,进行字符级别的操作。

    (5)兼容性: string是C++标准库的一部分,因此在C++中广泛使用,并且与其他标准库组件和 C++语言特性兼容。

    2.string的声明和初始化

    #include

    #include

    using namespeace std;

      int main()

    {

       string str1;                                 //声明并初始化空字符串

       string str2 = "hello world";            //用字符串字面量用始化字符串

    cout << "str1:"<

    cout << "str2:"<

    return 0;

    }

    3.string其他基本操作

    (1)获取字符串长度

      string str = "hello, world";

      int length = str.length();//或者  int length = str.size();

      cout<<"length"<

    (2) 拼接字符串(+ 或 append)

      string str1 = a;

      string str2 = b;

       string result1 =str1 + str2;                                           //使用 + 运算符

       string result2 = str1.append(", ").append(str2);           //使用 append 函数

      cout << "result 1 " << result1 << endl;

      cout << "result 1 "<< result1 << endl;

    (3)字符串查找(find)

     string str = "hello, world";

     size_t pos = str.find("world");

     if( pos !=  string::npos)

    {

       cout << "Substring found at position: " << pos endl;

    }

      else{

                 cout << "Substring not found." << endl:

             }

    (4)字符串替换

      string  str= "hello, world";

      replace(7, 5, a):

       cout << "Result: " << str << endl;

    (5)提取子字符串(substr)

      string str = "Hello, world!;

      string substr=str.substr(7,5); // 提取子字符串

      cout<< Substring: " << subStr << endl;

    (6)字符串比较(compare)

    字典序的比较方法是从小到大一个一个比较,一旦遇到不相等的字符就确定大小关系。

      string str1 = "Hello”;
      string str2 = "world ;
      int result = str1.compare(str2);// 比较字符串
         if(result == 0)

     {

         cout << "strings are equal." <

    }

        else if (result < 0)

         cout << "strings 1 is less than String 2." <

    }

          else 

    {

          cout << "strings 1 is greater than String 2." <

    }

    (7)遍历string

      循环枚举下标

     auto枚举(其中&表示取引用类型,如果对i修改将会改变原来的值)

    string s = "Hello";

    for(int i = 0; i < s.length(); ++ i)

    cout << s[i];

    cout << '\n';

    for(auto i :s)

    {
    cout << i;

    i='a';                           //此处的修改无效,因为这个主是拷贝出来的,而不是引用s的
    }

    cout << "\n";              //此时s = "Hello"

    for(auto &i : s)
    {

    cout << i;

    i='a';                           //此处修改会改变s的字符值

    }
    cout << '\n';              //此时s = "aaaaa"

    cout << s << '\n'

    三、输入输出


      1.scanf和printf

      int main()
    {

    int a, b;

    scanf("%d %d",&a, &b);

    printf("%d,%d\n",a,b);

    return 0;

    }

     

      int main()
    {

    doble a, b;

    scanf("%lf %lf",&a, &b);

    printf("%.2lf,%.3lf\n",a,b);    //自动四舍五入  (.x保留位小数)

    return 0;

    }

      int main()
    {

    char c1, c2;

    scanf("%c %c",&c1, &c2);

    printf("%c %c",c1, c2);

    return 0;

    }

     int main()
    {

    char s[10];

    scanf("%s , s);   //%s输入遇到空格或回车会停下

    printf("%s", s);

    return 0;

    }

     int main()
    {

    char s[15];

    scanf("%^\n] , s);     //^排除 \n回车

    printf("%s", s);

    return 0;

    }

    其中[]是一个正则表达式,表示只要不是回车就读进去。

    类型                                           对应标识符
    int                                               %d
    double                                        %lf

    char                                            %c

    char[]                                          %s

    scanf和sprintf的优势:

    (1)格式化输入和输出

    (2)效率高

    2.cin和cout

     int main()

    {

     char s[10];                 // cin输入字符串也是遇到空格或回车就结束

    cin >> s;

    cout << s;

    return 0;

    }    

    3.取消同步流

    由于cin和cout需要自动判断变量类型等内部原因,当数据量较大时,可能导致程序运行超时。

    我们可以通过取消同步流来加速cin和cout,加速后效率相差无几。

    int main()
    {                           

    ios::sync_with_stdio(e),cin.tie(e), cout.tie(e);        //取消同步流
                                                                                   //其他操作不变
    int x;cin >> x;

    cout << x << '\n';
    return 0;

    }


  • 相关阅读:
    C# 颜色与坐标
    高校教务系统登录页面JS分析——西安科技大学
    7.网络原理之TCP_IP(上)
    【SOPHON】算能盒子SE-16的C++模型转换
    java8 新特性2-接口中的默认方法和静态方法
    JVM监控和诊断的工具(MAT)
    蓝桥杯c++A组第二个填空题
    iOS App上传到苹果应用市场构建版本的图文教程
    创新性3D数据合成模型,微软推出EgoGen
    机器学习实战-系列教程8:SVM分类实战3非线性SVM(鸢尾花数据集/软间隔/线性SVM/非线性SVM/scikit-learn框架)项目实战、代码解读
  • 原文地址:https://blog.csdn.net/2402_82385099/article/details/143312434