1、变量,输入输出
1.1 各类型存储大小
1.2 输入输出
1.2.1 数
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl; // endl: 输出结束,换行
return 0;
}
1.2.2 字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
cout << str;
return 0;
}
1.2.3 类型转换
#include <iostream>
#include <string>
using namespace std;
int main()
{
float x = 123.12;
int y = (int)x;
cout << x << ' ' << y << endl;
return 0;
}
1.2.4 printf输出
(1) int:%d
(2) float: %f, 默认保留6位小数,保留4位小数 %.4f
(3) double: %lf, 默认保留6位小数,保留4位小数 %.3lf
(4) char: %c, 回车也是一个字符,用’\n’表示
(5) %8.3f, 表示这个浮点数的最小宽度为8,保留3位小数,当宽度不足时在前面补空格
(6) %-8.3f,表示最小宽度为8,保留3位小数,当宽度不足时在后面补上空格
(7) %08.3f, 表示最小宽度为8,保留3位小数,当宽度不足时在前面补上0
2、循环
for (init-statement : condition: expression)
{
statement
}
while()
{
statement
}
3、数组
3.1 定义数组
3.1.1 一维
定义在 main 外面,置0
定义在 main 里面,随机赋值
3.1.1 多维
int a[3][4]; // 大小为3的数组,每个元素是含有4个整数的数组。
int arr[10][20][30] = {0}; // 将所有元素初始化为0
// 大小为10的数组,它的每个元素是含有20个数组的数组
// 这些数组的元素是含有30个整数的数组
3.2 常用配套函数
3.2.1 reverse(a+m,a+n)
反转 [ m , n ) 内数据
# include <iostream>
# include <algorithm>
using namespace std;
int main() {
int a[5] = {1,2,3,4,5};
reverse(a,a+3);
for(int x : a){
cout << x << ' ';
}
return 0;
}
// 3 2 1 4 5
class Solution {
public:
string reverseLeftWords(string s, int n) {
int len = s.size();
reverse(s.begin(),s.end());
reverse(s.begin(),s.begin()+len-n);
reverse(s.begin()+len-n,s.end());
return s;
}
};
3.2.2 swap(a[m],a[n])
4、字符串
4.1 字符数组
字符串 = 字符数组 + ‘\0’
可以使用字符串初始化字符数组,因为每个字符串结尾会有一个’\0’,所以字符数组的长度要比字符串 多1
char a3[3] = "C++" 错误!!!
4.2 字符数组输入
输入字符串时,遇到空格和回车停止读取
#include "iostream"
using namespace std;
int main(){
char str[3];
cin >> str;
cout << str;
return 0 ;
}
读取字符串**(包括空格)**方法:
fgets(指向字符串的指针,读取含空格的最大字符数,stdin)
注:fgets不会删除行末的回车符,所以strlen时长度会多1
#include "iostream"
using namespace std;
int main(){
char str[100];
fgets(str,100,stdin);
cout << str;
return 0 ;
}
4.3 字符数组常用操作
(1)strlen(str),求字符串的长度
(2)strcmp(a, b),比较两个字符串的大小,a < b返回-1,a == b返回0,a > b返回1。这里的比较方式是字典序!
(3)strcpy(a, b),将字符串b复制给从a开始的字符数组。
4.4 string.h 标准库(可变长的字符序列,比字符数组更加好用)
4.4.1 读写(读不了空格)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
cout << s1 << s2 << endl;
return 0;
}
string不能直接用printf输出,需要写成
string str1 = "abcd";
printf(“%s”, str1.c_str());
4.4.2 读写(读的了空格)
fgets(cin,string): 读取一行
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
cout << s << endl;
return 0;
}
4.4.3 empty,size
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2 = "abc d";
cout << s1.empty() << endl; // 1
cout << s2.empty() << endl; // 0
cout << s2.size() << endl; // 5
return 0;
}
4.4.4 string 相加(加法运算符的两侧的运算对象至少有一个是string)
string s1 = "hello", s2 = "world"; // 在s1和s2中都没有标点符号
string s3 = s1 + ", " + s2 + '\n';
string s4 = s1 + ", "; // 正确:把一个string对象和有一个字面值相加
string s5 = "hello" + ", "; // 错误:两个运算对象都不是string
string s6 = s1 + ", " + "world"; // 正确,每个加法运算都有一个运算符是string
string s7 = "hello" + ", " + s2; // 错误:不能把字面值直接相加,运算是从左到右进行的
4.4.5 处理string对象中的字符(遍历的几种方式)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello world";
// 法一
for (int i = 0; i < s.size(); i ++ )
cout << s[i] << endl;
// 法二
for (char c: s) cout << c << endl;
return 0;
}