蓝桥杯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 a[N];在全局变量的时候会自动被初始化为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 buf[100];

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(s[i],s[s.length()-i-1]);

可以学习这种输入方式:

string s;

getline(cin,s);

相关推荐
愚润求学20 分钟前
【Linux】进程间通信(一):认识管道
linux·运维·服务器·开发语言·c++·笔记
珊瑚里的鱼35 分钟前
【滑动窗口】LeetCode 1658题解 | 将 x 减到 0 的最小操作数
开发语言·c++·笔记·算法·leetcode·stl
共享家95271 小时前
哈希的原理、实现
c++·算法
少了一只鹅2 小时前
字符函数和字符串函数
c语言·算法
*才华有限公司*2 小时前
gRPC开发指南:Visual Studio 2022 + Vcpkg + Windows全流程配置
c++·ide·visual studio
wefg12 小时前
【C++】类与对象
开发语言·c++
双叶8363 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)(网页版预告)(html)(js)(json)
c语言·javascript·数据结构·html·json
子豪-中国机器人3 小时前
C++ 蓝桥 STEMA 省选拔赛模拟测试题(第一套)
开发语言·c++·算法
callJJ3 小时前
Bellman - Ford 算法与 SPFA 算法求解最短路径问题 ——从零开始的图论讲解(4)
数据结构·算法·蓝桥杯·图论·单源最短路径·bellman- ford算法
虾球xz3 小时前
游戏引擎学习第286天:开始解耦实体行为
c++·人工智能·学习·游戏引擎