很長時間沒有再復習C++的基礎知識,現在將一些容易遺忘的知識點做一個簡單的匯總。
1、注釋
??分為單行注釋和多行注釋
//cout<<endl;/*int i=1;cout<<i<<endl;*/
2、常量
??宏常量:#define ,宏常量沒有類型,一般在預編譯階段進行字符替換,不需要創建堆棧;
??const: 表示常量,且不能修改。
#include <iostream>
using namespace std;
#define a 1
int main()
{const int b = 2;int c = a + b;cout << c << endl;
}
3、標識符的四個規則
??1,字母區分大小寫。
??2,需使用字母或者下劃線作為開始。
??3,只可以用字母、數字和下劃線組成。
??4,不能是關鍵字。
4、變量字節數
類型 | 字節數 |
---|---|
char | 1 |
short | 2 |
bool | 1 |
int | 4 |
long | 4 |
long long | 8 |
float | 4 |
double | 8 |
long double | 8 |
指針* | x64:8 x86:4 |
5、轉義字符
cout << "taiyang\n";//換行符
cout << "\\\n";//反斜杠
cout << "I\tLove\tMaoMao";//水平制表符
5、數據的輸入輸出
C++:
int b = 0;
cin >> b;//輸入
cout << b << endl;//輸出
C#:
Console.Read();//返回int類型
Console.ReadLine(); //返回string類型
Console.Write();//輸出數據
Console.WriteLine();//輸出數據并換行
Console.ReadKey();//用于暫停程序
python:
x=int(input("x="))
y=int(input("y="))
z=x+y
print("x+y=", z)
7、遞增
int b = 1;int c = 2;//后置遞增先進行表達式運算,再自增int d = b++ * c;cout << d << endl;//前置遞增先自增,再進行表達式的運算int e = ++b * c;cout << e << endl;
8、continue和break
??continue:一般在循環結構中使用,表示程序執行到這一步不再執行后續代碼,重新執行下一次循環。
??break:退出當前循環。
9、VS鼠標變成黑框
??按Insert鍵取消。
10、查看數組地址
??&在這里的用法是取地址。還有一個用法是引用。
int b[3] = {1,3,5};
cout << "數組b占用的內存:" << sizeof(b) << endl;
cout << "數組b中第一個元素占用的內存:" << sizeof(b[0]) << endl;
cout << "數組b的首地址:" << (int)b << endl;
cout << "數組b第一個元素的首地址:" << (int)&b[0] << endl;
數組b占用的內存:12
數組b中第一個元素占用的內存:4
數組b的首地址:11532692
數組b第一個元素的首地址:11532692
11、冒泡排序
??最經典的排序算法,依次兩兩對比進行置換。
int b[10] = { 3,21,25,48,5,76,7,28,43,10 };int n = sizeof(b) / sizeof(b[0]);//外循環需要進行9輪對比for (int i = 0; i < n; i++){//進行9-i次對比for (int j = 0; j < n - i - 1; j++){if (b[j] > b[j + 1]){int temp = b[j];b[j] = b[j + 1];b[j + 1] = temp;}}}for (int i = 0; i < 10; i++){cout << b[i] << endl;}
12、值傳遞
??在進行函數調用時,形參發生改變并不會影響實參。原理是因為在內存空間中只是形參的空間內容發生了改變,實參的空間并未受到影響。
int Swap(int b, int c)
{int temp = b;b = c;c = temp;return 0;
}
int main()
{//定義實參int e = 2;int f = 3;int d = Swap(e, f);cout << e << endl;cout << f << endl;
}
結果:
2
3