INF442 Amphi 1:Introduction / C++ as C
4月9日 Pâle Machine
0. Introduction to Data Science
表示方法1:Vector
我们用向量表示数据,每一行表示一个Observation,每一列是它的变量
- Categorical Variables:值域有限
- Continuous Variables
表示方法2:Metric
( i , j ) (i,j) (i,j)表示observation i和observation j之间的距离或者similarity。
将数据变成这两个的过程称为Feature Extraction
Learning Paradigms
- Supervised Learning: 训练数据有label
- classification (categorical label), regression (continuous label), forecasting (regression on time series)
- Unsupervised Learning
- Clustring, Dimensionality Reduction, Anomaly Detection, Noise Removal
- Semi-supervised Learning:有一部分的数据有标签
- Reinforcement Learning:
- Markov Decision Process
- Find policy that minimizes the regret
- Exploration vs. exploitation
- Control Learning
2. C++ as C
2 main components:
- A direct mapping of hardware features (malloc / free)
- Zero-overhead abstractions (new / delete)
2.1 Hello world
#include<iostream>
int main(){
std::cout << "Hello, world\n"; //把helloworld输出<<到std.out
return 0; //Unix下一定要返回一个结果
}
2.1.2 编译方法一:
编译过程:
g++ helloworld.cpp
//不加o默认的输出名字为a.out
g++ -o helloworld helloworld.cpp
//指定输出的名字为helloworld
会得到一个.out文件,是一个可执行文件。不需要像Java那样用一个虚拟。
2.1.2 编译方法二:makefile
命令行:
touch makefile
vi makefile
按i,然后按照makefile的逻辑
target: dependicies
system command
我们往makefile中写入
helloworld: helloworld.cpp
g++ -o helloworld helloworld.cpp
之后在命令行输入makefile就可以编译了,而且make也可以检测updates。
2.2 Built-in types
具体大小与编译器和运行的机器有关。
类型 | 大小(bit) |
---|---|
bool | 1 |
char | 8 |
shor | 16 |
int | 32 |
long | 64 |
float | 32=1+8+23 |
double | 64 |
对于float,它有1bit的符号位,8个bit用于2的次数,其余用于小数表示。
⚠️:
对于一些compilateur,在int i;之后i为1,但一般情况下光写int i;i的初始值是随机的。
2.3 🌟Enumerations
用于给int赋予一个名字,增强程序的可读性
enum MyEnum{
MyFirstValue = 17,
MySecondValue = 8,
};
int main(){
MyEnum v = MyFirstValue;
return 0;
}
2.4 Arrays
- Arrays are instantiated,但是内部值是随机的。
- 无.length
int t[2]; // 数值是随机的
int t[] = {1,2,3};
// 初始化
int * t = new int[2]();
std::cout << t[0] << "\t" << t[1]; // 0,0
2.5 Standard Input and Output
standard input --> program --> standard output / standard error
2.6 Switch
switch(bool/char/int/long/enum expression){
// 如果expression==constant1
case constant1: ... break;
case constant2: ... break;
default: break;
}
2.7 Reference
Java上没有。
char i = 71;
char& r = i; //reference r必须立马给它赋值,i和r是同一个东西。
r++;
std::cout << i; //72
2.8 Function
可以用surcharge(Overloading),自动为输入类型选择函数。
int square(int i){
return i*i;
}
double square(double d){
return d+d;
}
int main(){
int i=3;
double d=3;
std::cout<< square(i) << " "<<square(d);
}
Java中只能用by value,但在C++可以用value和reference。
- ⚠️ when an argument is passed by value, a copy of it is made in the function
- when an argument is passed by reference, it can be accessed in the function
对比
void incr(int i){
i++;
}
int main(){
int i=65;
incr(i);
std::cout << i << std::endl; // 65
return 0;
}
和
void incr(int& r){
r++;
}
int main(){
int i=65;
incr(i);
std::cout << i << std::endl; // 66
return 0;
}
2.9 其他:
i++,后赋值
++i,先赋值