杂七杂八的“笔记”

实际上就是便签啦

ASCII

  • A ~ Z == '65' ~ '90'

  • a ~ z == '97' ~ '122'

  • 0 ~ 9 == '48' ~ '57'

    • 数字需要引号扩起来

int a = 1;
char b = 'a' + a;
cout << b; // b == b
//自动转为scii码

进制问题

  • 8 bit (char myVal): -128 - 127

  • 16 bit(short myVal): -32768 - 32767

  • 32bit(long myVal, int myVal): -2147483648 - 2147483647

  • 64bit(longlong myVal): 9223372036854775808

  • ‘a' 表示一个字符,“a“ 表示字符串

  • ‘ ‘ 里面只能放一个字符

  • “ “ 自动在末尾补0

    • “a“ == ‘a‘ + ‘\0‘

  • "\n" : new line

  • "\t" : tab

  • "\'" : single quote

  • "\"" : double qupte

  • "\\" : backslash

    • Ex: string a = "a\"b" == a"b

      • a后面是“不是\

rand() 函数

rand() 函数产生伪随机数,根据一个数(seed)为基准敌退出来的一系列数

rand()出自于 the c standard library

0 - RAND_MAX(32767)

[a, b) : (rand() % (b - a)) + a
[a, b] : (rand() % (b - a + 1)) + a
(a, b] : (rand() %(b - a)) + a + 1

奇奇怪怪的知识增长了...

  1. 赋值可以放进if的判断中

int A = 3
if (A = 10) {
    A += 1;
}
cout << A;

虽然会报错,但是可以跑,结果为11

2. switch/case中,注意看break的位置

char a = 'A'
switch (a) {
    case 'A':
        cout << "ABC";
    case 'B':
        cout << "BAC";
    case 'C':
        cout << "CAB";
}

//输出结果为CAB,因为case A的cout之后没有break

3. 简写(最喜欢的part哈哈哈)

python:
if x == 0
    y = 1
else: 
    y = 0
    
==>

y = 1 if x == 0 else 0
C++
if (condition) {
    y = 1;
} else {
    y = 0;
}

==> 

y = (condition) ? 1 : 0;

4.

  • int abs(int i) 返回整型参数i的绝对值

  • double cabs(struct complex znum) 返回复数znum的绝对值

  • double fabs(double x) 返回双精度参数x的绝对值

  • long labs(long n) 返回长整型参数n的绝对值

5.

vector<类型> ___名字___ (个数); //位置从0开始

  • vector时间复杂度:O(1);

  • list时间复杂度:O(n);

  1. vector又连续的内存空间,可随机存取

  2. list内存空间不连续,不支持"+" / "+=" / "<" 等

  3. list:大量插入和删除

  4. vector / list <int>::interator 重载运算符

  5. 对于array和vector来说out of range是可以存在的

    1. vecotrlist[n] = x (true)

    2. vectorlist.at(n) = x (false)

push_back(c) : 末尾添加
insert(indx, substr) : 第几位加什么
replace(indx, num, substr) : 第几位后的几位替换成什么
back() : 最后一个元素
pop_back() : 删除最后一个元素

如果find()的东西没有,返回 string:: npos

string_name .substr(str_name.size() - n, n) //返回最后n位字符

6.

getline() : 用户输入的全部东西直到换行(全部东西包含空格)
clear() : 清除缓存(错误状态,比如输入类型错误)
str_name.find(c) -> 返回第一个找到的位置
str_name.substr(a,b) -> 返回第a位到第 b-1 位的内容
string comparisons

1) "Apples" < "Oranges" //比较首字母对应码
2) "merry" > "Merry" //小写字母码一定比大写字母大
3) "banana" < "bananarama" //如果等量字符相同且更长,则小

7.

递归函数:

  • A base case returns a value without performing a recursive call

  • The recursive case calls itself

8.

for loop: (A, B, C)

  • A: initialize

  • B: continue condition

  • C: update

type of control:

  • counting

  • sentinel

  • flag

Declaration:

  • say what an identifier is and what type of object it refers to

  • connects a name to an object

Define:

set aside memory for the variable

Initialize: assign value to variable for the first time

9.

c++ 通过throw语句和try...catch实现对异常的处理

try...catch:

  • 执行try块,没有异常抛出执行最后一个catch块后面的语句(没有catch)

  • 如果有异常,跳转到匹配的catch执行try之后必须紧跟catch,不然报错

  • (就算type1不匹配,也先执行)

10.

fixed: from<ios> 
                //12.34000
                // fixed << 12.34
scientific: from<ios> 
                //1.234e+01
                //scienfic << 12.34
setprecision: from<iomanip>
                1) 如果没有fixed or scienfic 有几位就几位 
                //setprecision(5) << 12.34
                //12.34
                2)正常设置准确度
showpoint: from<ios>
                //setprecision(3) << showpoint << 99.0
                //99.0

Last updated

Was this helpful?