蓝桥杯c/c++需要掌握的基础语法总结

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

2、using namespace std;

3、输出 cout<<""<<end1; (换行) printf("");

4、int x=3;整数

double d=3.14;小数

char ch='A';字符

char s\[\]="Hello";字符串

bool b=true;布尔值

\n换行符

5、数组

const int N=1e5+9;//const 定义的数后续不会被修改

int aN;在全局变量的时候会自动被初始化为0;

6、typdef 给类型重命名

7、char s\[\]="hello";

cout<<s<<'\n';输出s遇到\0就停下并换行;

8、交换变量的写法

9、String

(1)

java 复制代码
#include<iostream>
using namespace std;
#include<string>
int main(){
string str1;
string str2="Hello,baby!";
string str3=str2;
string str4=str2.substr(0,5);
const char*charArray="hello";
string str5(charArray);//hello付给了str5
string str6(5,'A');//五个A;
cout<<str1<<'\n';
cout<<str2<<'\n';
cout<<str3<<'\n';
cout<<str4<<'\n';
cout<<str5<<'\n';
cout<<str6<<'\n';
}

(2)print输出时

char buf100;

scanf ("%s",buf);

string str(buf);

printf("str=%s\n",str.c_str());使用print时要将string转换为c风格的字符串

10、获取字符串长度str.length();

11、拼接字符串 append(" ");

12、字符串查找

**str.find("aaa");**在str字符串中寻找aaa返回起始位置。

13、字符串替换

str.replace(7,5,"aaaa");子串的起始位置,替换的长度

14、提取子字符串substr

str.substr(7,5);提取str字符串第七位置开始长度5的字符串

15、字符串比较compare

str1.compare(str2);等于0,两个字符串想等,等于正数,等于负数

按照字典序一个一个比较

16、字符串遍历1-循环枚举下标for(int i=0;i<s.length();i++){}

17、auto枚举for(auto i:s){cout<<i;}

18、题目

思路1:直接倒着输出

思路2:真正实现反转

reverse(s.begin(),s.end());

或者:

for(int i=0;i<s.length()/2;++i)swap(si,ss.length()-i-1);

可以学习这种输入方式:

string s;

getline(cin,s);

相关推荐
誰能久伴不乏33 分钟前
桌面客户端的运行日志系统:SQLite 分块 + 独立写线程 + 文本降级
数据库·c++·qt·架构·sqlite
choumin10 小时前
C++中万能引用与std::forward的作用和用法
c++·万能引用·std forward
下一站春风里11 小时前
位运算完整学习笔记:按位与(&)+ 异或(^)
开发语言·c++·算法
lhbweilai14 小时前
分析FreeRTOS内核对象API的源码实现(八)
c语言·开发语言·stm32·单片机
努力努力再努力wz14 小时前
【C++高阶系列】C++20 协程底层原理:函数为什么能暂停?从状态机到 Task,彻底串起挂起、恢复与协程调度
开发语言·c++·c++20·1024程序员节
划破黑暗的第一缕曙光14 小时前
[C++]:4.模板
c++·template·模板·函数模版·类模版
十五年专注C++开发14 小时前
pybind11:一个跨C++和Python平台的编程库
开发语言·c++·python·pybind11
艾络迅Acceleronix14 小时前
环境监测终端:STM32+EG800Z AT+ 艾络迅平台实战|多传感器采集联动全流程
c语言·stm32·单片机·嵌入式硬件·物联网·1024程序员节
CodeYp201914 小时前
第11讲:stack 和 queue
开发语言·c++·queue·stack
Zwawa14 小时前
一只按键不只“按下”:短按、长按和连按如何共存
c语言·单片机·嵌入式