知识点
- cpp里面会经常查看数据类型的大小,python倒不会用太多这些东西
- 条件符号
- 类型模版
- 结构体,指针类型, &在等号左边是取引用,在等号右边是取地址,这里是取地址
- C语言的强转类型
- 引用的案例
- 指针案例
#include <iostream>
#include <math.h>
void size_of_demo() {
int a = 1000000;
std::cout << "size int: " << sizeof(int) << std::endl;
std::cout << "size a: " << sizeof(a) << std::endl;
return;
}
int condition_demo(int a) {
int b = (a == 5) ? 5 : 0;
return b;
}
void comma_demo() {
float x;
std::pair<float, float> y = {x = 2.7, sqrt(x)};
std::cout << "Second value of y is: " << y.second << std::endl;
}
struct Conv
{
int pad = 0;
int stride = 1;
};
void point_demo(){
Conv conv;
conv.pad = 10;
std::cout << "pointer demo pad result is :" << conv.pad << std::endl;
Conv* conv_2 = &conv;
std::cout << "pointer demo stride result is :" << conv_2 ->stride << std::endl;
conv_2 ->stride = 100;
std::cout << "pointer demo stride result is :" << conv_2 ->stride << std::endl;
return;
}
void cast_demo() {
float a = 3.3;
std::cout << "value a: " << (int)a << std::endl;
return;
}
void reference_demo(){
int a = 10;
int b = a;
b = 20;
std::cout << "First try of reference demo result: " << a << std::endl;
int& c = a;
c = 20;
std::cout << "Second try of reference demo result: " << a << std::endl;
}
void ptr_demo(){
int a = 10;
int* b = &a;
std::cout << "ptr demo result is: " << (*b) << std::endl;
*b = 6;
std::cout << "ptr demo result is: " << a << std::endl;
}
int main(){
point_demo();
return 0;
}
cmake_minimum_required(VERSION 3.16)
project(operator_demo)
set(CMAKE_CXX_STANDARD 14)
add_executable(operator_demo operator_demo.cpp)
- 循环
- while for 用的比较多
- do_while, goto用的很少了
# include <iostream>
void while_demo(){
int a = 10;
while (a < 20) {
std::cout << "value a: " << a << std::endl;
++a;
}
}
void for_demo(){
for (int a = 10; a < 20; ++a) {
std::cout << "value a: " << a << std::endl;
}
}
void do_while_demo(){
int a = 10;
do {
std::cout << "value a: " << a << std::endl;
++a;
}while (a < 20);
}
void goto_demo() {
int a = 10;
LOOP: do {
std::cout << "goto demo value: " << a << std::endl;
++a;
}while (a < 20);
}
int main() {
goto_demo();
return 0;
}
cmake_minimum_required(VERSION 3.16) # cmake版本限制
project(operator_demo) # 项目名
set(CMAKE_CXX_STANDARD 14) # cpp 版本
add_executable(loop_demo loop_demo.cpp) # 编译成可执行文件
条件判断
#include <iostream>
void switch_case_demo(char grade) {
switch(grade) {
case 'A':
std::cout << "very good" << std::endl;
break;
case 'B':
case 'C':
std::cout << "passed" << std::endl;
break;
case 'D':
std::cout << "failed" << std::endl;
break;
default:
std::cout << "Invaild" << std::endl;
break;
}
}
void condition_demo() {
int x, y = 10;
int a = 55;
if (y < 10) {
x = 30;
}
else if (a = 55) {
x = 200;
}
else {
x = 4;
}
std::cout << "x value is: "<< x << std::endl;
}
int main() {
condition_demo();
return 0;
}