筆記:https://blog.csdn.net/ClaireSy/article/details/108422945
- 第一個程序
- 注釋
- 變量
- 常量
- 關鍵字
- 標識符命名規則
- 數據類型
- sizeof 關鍵字
- 浮點型
- 字符型
- 轉義字符
- 字符串型
- 布爾類型
- 數據的輸入 cin
- 運算符
- 程序流程結構
- 跳轉語句
- 數組
- 冒泡排序
- 二維數組
- 函數
- 指針
- 結構體
#include<iostream> using namespace std; int main() { cout << "HelloWorld" << endl; system("pause"); }
單行注釋 //
多行注釋 /* 內容 */
數據類型 變量名稱 = 初始值 ;
// 單行注釋 /* 1. 多行注釋 2.... */ int a = 10;
用於記錄程序中不可更改的數據,常量一旦定義不可修改
#define 宏常量
const 修飾的變量
#include<iostream> using namespace std; #define DAY 7 int main() { cout << "一周有" << DAY << "天" <<endl; const int month = 12; cout << "一年有" << month << "月" << endl; system("pause"); }
- 標識符不能是關鍵字
- 標識符只能是字母、數字、下划線
- 第一個字符必須是字母或者下划線
- 標識符區分大小寫
變量定義: 變量類型 變量名 = 變量初始值;
變量類型存在的意義是為變量分配合適的內存空間
short 2個字節 (-32768~32767)
int 4個字節 (-2147483648~2147483637)
long 32位4字節,64位 8 字節
long long 8字節
short num_1 = 10; int num_2 = 10; long num_3 = 10; long long num_4 = 10;
int x = sizeof(類型);
cout << sizeof(short) << endl; cout << sizeof(int) << endl; cout << sizeof(long) << endl; cout << sizeof(long long) << endl;
單精度:float
雙精度:double
float 4字節,7有效位數
double 8字節,15位精度
默認情況下,小數顯示6位有效數字
科學計數法
float f1 = 3e2; cout << f1 << endl;
float f2 = 3e-2;
定義 char ch = 'a';
字符類型占用的內存空間: 1 字節
- 只能使用 ‘ ’ ,不能使用雙引號
- 單引號內不能超過4個字符,但是始終只識別最后一個字符、
- 字符對於的數字 ASCII
ASCII 表: http://ascii.911cha.com/
換行 \n
水平制表 \t
反斜杠 \\
cout << "he\tWorld"<<endl; cout << "hel\tWorld" << endl; cout << "hedd\tWorld" << endl; cout << "heddd\tWorld" << endl;
有兩種風格
1.C語言風格的串
char 變量名[] = "字符串";
2.C++ 風格字符串
string 變量名 = “字符串”;
//[] 必須放在變量后 char str[] = "hello world"; cout << str << endl; string str2 = "Hello ,World 2.0"; cout << str2 << endl;
string 類型需要包含新的頭文件
#include<string.h>
- true 本質是1
- false 本質是0
占用 1 個字節的內存,本質上 0 代表假 , 1 代表真。 布爾類型非0即為真
關鍵字 cin
格式: cin >> 變量 ;
int a; cout << "請輸入a:"; cin >> a; cout << "a:"<<a<<endl;
算術運算符: + - * / % ++ --
賦值運算符:+= -= *= /= %=
比較運算符:== , !=, >,<,<=,>=,
邏輯運算符:! && ||
- 整數相除,只保留整數部分
- 取余 % 只能作用在整數
- 對整數賦值小數,真正生效的只有整數部分
- && 前置為假,則后條件不判斷, || 前置為真,后條件不判斷,即短路
- 順序結構 順序執行,不發生跳轉
- 選擇結構 if 語句
- 循環結構 while, for, do-while
分支包括:單個 if ,if -else , if -else if - else ,嵌套if 等
三目運算符: 表達式1 ? 表達式2 : 表達式3;
int i = 0; int tag = 1; do { cout << i << endl; i++; if (i == 50) { tag = 0; } } while (tag);
- break
可以在 switch 中跳出,可以在循環中 ,可以出現在多層循環中
- continue 跳過當前行以下的代碼
- goto 可以無條件的跳轉語句
語法: goto 標記
goto flag; cout << "aaa"<<endl; cout << "bbb"<<endl; flag: cout << "ccc"<<endl;
存放相同的數據元素
數據類型 數組名[數組長度];
數據類型 數組名[數組長度] = {值1,值2。。。};
數據類型 數組名[] = {值,,,};
一維數組數組名的用途:
- 可以統計整個數組在內存中的長度
- 可以獲取數組在內存中首地址
//數組大小 cout << sizeof(arr) << endl; //數組第一個元素的大小 cout << sizeof(arr[0]) << endl; //數組長度 cout << sizeof(arr) / sizeof(arr[0]) << endl; //數組首地址 cout << arr << endl;
//數組首地址 cout << (int)arr << endl; //第一個元素地址 = 數組首地址 cout << (int)&arr[0] << endl; //第二個元素地址 = 第一個元素地址 + 類型大小 cout << (int)&arr[1] << endl;
原理:遍歷數組,每一次遍歷都將昂一個極大或者極小的值放在數組的一端
void bubbleSort(int data[],int size) { for (int i = 0; i < size-1; i++) { for (int j = 0; j < size-i-1; j++) { if (data[j] < data[j + 1]) { int temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; } } } }
數據類型 數組名 [行數][列數] ;
int arr[2][3]; int arr2[2][3] = {1,2,3,4,5,6}; int arr3[2][3] = { {1,2,3},{4,5,6} }; int arr4[][3] = {1,2,3,4,5,6}
函數參數傳遞:實參、形參
值傳遞:
形參的變化不會影響到原來的值
引用傳遞:
傳遞的是應用的地址
函數聲明:提前告訴編譯器函數的存在,聲明可以多次,但是定義只能用有一次
函數的分文件編寫:
- 創建 .h 文件
- 創建 .cpp 文件
- 在 .h 文件創建聲明
- 在 cpp 文件包含頭文件 (使用 ” “)
#pragma once #include<iostream> using namespace std; void swap(); void swap() { cout << "swap..." << endl; }
Main
#include "swap.h" int main() { swap(); return 0; }
int a = 10; int *p = &a; cout << "a:" << a << endl; cout << "p:" << p << endl; cout << "*p:" << *p << endl;
指針變量 32位為 4 字節 ,64 位 8字節
空指針&野指針
空指針:指針變量指向地址編號為0 的指針(0~255 內存 編號是系統占用,不能訪問)
用於對指針變量進行初始化,空指針不可以進行訪問
int *p =NULL;
野指針:指針變量指向非法的內存空間
int* p = (int*)0x00110; cout << *p << endl;
const 修飾指向
- const 修飾指向 指針的指向可以修改,指針指向的值不可修改
- const 修飾常量
- const 修飾指針以及常量
//常量指針 int a = 10; int b = 20; const int* p = &a; //*p = 20; 不可修改 p = &b;
//指針常量 int a = 10; int b = 20; int * const p = &a; //p = &b; 指針不可修改 *p = 30;
//const 修飾指針與常量 int a = 10; int b = 20; const int * const p = &a; //p = &b; 指針不可修改 //*p = 30; 值不可修改
構建用戶自定義的數據類型
語法:
struct 結構體名稱{
成員列表
};
結構體數組: struct 結構體名稱 數組名[] = {...}
void print(const int * a){ //a = 10; }