当前位置: 首页 > news >正文

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

文章目录

一、C++基础格式

1.打印hello, world

2.基本数据类型

二、string

1.string简介    

2.string的声明和初始化

3.string其他基本操作

(1)获取字符串长度

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

(3)字符串查找(find)

(4)字符串替换

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

(6)字符串比较(compare)

(7)遍历string  循环枚举下标

三、输入输出

  1.scanf和printf

2.cin和cout

3.取消同步流



一、C++基础格式

1.打印hello, world

#include <bits/stdc++.h>  //万能头文件

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>
string与char[]不同,string实现了高度的封装,可以很方便地完成各种字符串的操作,比如拼接、截取、匹配等等。

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

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

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

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

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

2.string的声明和初始化

#include<iosteram>

#include<string>

using namespeace std;

  int main()

{

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

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

cout << "str1:"<<str1<<endl;

cout << "str2:"<<str2<<endl;

return 0;

}

3.string其他基本操作

(1)获取字符串长度

  string str = "hello, world";

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

  cout<<"length"<<endl

(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." <<endl;

}

    else if (result < 0)

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

}

      else 

{

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

}

(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;

}



http://www.mrgr.cn/news/60707.html

相关文章:

  • 情感咨询小程序的市场需求大吗?
  • 如何将markdown文件转换为pdf
  • Excell 中几种自定义公式
  • python:如何判断一个数是否为素数
  • Ajax学习笔记
  • 矩阵运算的基本性质
  • Lomda表达式与函数式接口
  • 爬虫ip与反爬虫的“猫鼠游戏”
  • java基础(Object篇)
  • Python 基础语法 - 赋值运算符
  • auto占位符(C++11~C++17)
  • C语言实现高并发编程——线程池
  • Open3D-Geometry-14:Distance Queries距离查询方法将网格生成为隐式表示
  • 【专题】关系模型的基本理论
  • 使用chatglm API处理论文
  • 排序算法简记
  • 五、Hadoop 分布式文件系统(HDFS)的原理与架构专业解析
  • python 数据结构 1
  • 一文贯通RAG的技术介绍和构建(简易版+附详细代码)
  • 2024年【制冷与空调设备安装修理】考试内容及制冷与空调设备安装修理最新解析
  • Java程序设计:spring boot(12)——定时调度集成 - Quartz
  • 怎么把照片恢复至手机?一文读懂详细教程与多种方法!
  • 从JDK 17 到 JDK 21:Java 新特性
  • 北理工计算机考研难度分析
  • ctfshow(151->154)--文件上传漏洞--.user.ini
  • 热门四款深度数据恢复软件大比拼!!!