变量
- 10e9以内或2e31以内或32位整数用int,
- 10e18以内或2e63以内或64位整数用long long。超过int范围的初值要带LL
- 浮点就用double,有效精度15~16位
INT_MAX
、INT_MIN
、LLONG_MAX
在#include <climits>
头⽂件⾥⾯,int
最⼤值为2147483647,共10位数字;LLONG_MAX
最⼤值有19位数字,以9开头。所以说储存13位的学号可以⽤long long int
,输出的时候使⽤%013lld
- bool变量,非零都是真
- 小写字母比大写字母ASCII值大32
- 宏定义
#define ADD(a,b) ((a)+(b))
基本运算
- 整型除法是直接舍去小数部分==向下取整
- 条件运算符
A ? B : C
- 位运算符 --《算法笔记》p28
输入
1、不指定读入个数
while(cin >> n)
2、读入一整行
char *gets(char *s);
遇到换行符或EOF时替换成’\0’并读取结束。读取成功时,返回s地址;失败时返回null。
#includecin.getline()
针对char*,换行符结束getline(is,s)
针对string,换行符结束,返回值是is,可以作判定条件。- 要读入换行符
getchar()
- 利用
stringstream
读入一整行以空格分隔的数据
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
int n;
string s;
getline(cin,s);
stringstream ss(s);
while(ss >> n){
......
}
return 0;
}
3、c风格处理string
string s;
s.resize(8); //一定要提前开辟空间
scanf("%s", &s[0]);
printf("%s", s.c_str());
输出
输出格式
printf("%7d", n);
printf("%.3f", d);
#include <iomanip>
cout << setw(3) << fixed << setprecision(4) << d;
```
# class template: Pair
This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members *first* and *second*.