// for循環
#include "iostream"
const int arSize = 16;
int main() {
using std::cout;
using std::endl;
long long factorials[arSize];
factorials[0] = factorials[1] = 1;
for (int i = 2; i < arSize; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i < arSize; i++)
cout << "factorials[" << i << "] = " << factorials[i] << endl;
return 0;
}
運作結果
// for循環
// for循環
#include "iostream"
using namespace std;
const int arSize = 16;
int main() {
string str;
cout << "enter a string : " << endl;
cin >> str;
for (int i = str.size() - 1; i >= 0; i--)
cout << str[i];
cout << "\n end " << endl;
int guests = 0;
while (guests++ < 10)
cout << guests << endl;
return 0;
}
由于guest++ < 10, 是先拿guest和10比較後, 再對guest+1, 然後才運作輸出語句, 是以運作結果如上
基于範圍的for循環
注意其中一個能改變prices數組裡的值, 另一個卻不能
double prices[5] = {1.11, 1.22, 1.33, 1.44, 1.55};
for (double x : prices)
cout << x << endl;
// 這種寫法能夠改變prices裡的數值, 上面的寫法不行
// 符号&表明x是一個引用變量
for (double &x : prices)
x = x * 0.8;
再看一下下面這個輸出設計到++運算符
int x = 1;
int y = (4 + x++) + (6 * x++);
cout << x << endl;
cout << y << endl;
運作結果為
3
17
++運算符和指針混合一起使用:
++和指針運算
// for循環
#include "iostream"
using namespace std;
const int arSize = 16;
int main() {
double arr[5] = {1.11, 1.22, 1.33, 1.44, 1.55};
// 指針pt指向了數組的首位址
double* pt = arr;
// 相當于pt向後移動了一個double的長度, 也就是指向了數組的第二個元素
++pt;
cout << *pt << endl;
double x = *++pt;
// 1.33
cout << x << endl;
// 先是取出pt指針指向的位址裡的數值, 然後+1, 指派給y
double y = ++*pt;
// 2.33
cout << y << endl;
cout << "arr[2] = " << arr[2] << endl;
cout << "before (*pt)++ , *pt = " << *pt << endl;
double z = *pt++;
cout << z << endl;
cout << *pt << endl;
return 0;
}
程式運作結果為:
1.22
1.33
2.33
arr[2] = 2.33
before (*pt)++ , *pt = 2.33
2.33
1.44
在看一個利用for循環做字元串反轉
字元串翻轉
#include "iostream"
#include "string"
using namespace std;
int main()
{
cout << "Enter a wrod : ";
string word;
cin >> word;
for (int j = 0, i = word.size() - 1; j < i; j++, i--)
{
// temp 在使用完就會回收
char temp;
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
cout << word << endl;
cout << "Enter second wrod : ";
char word2[5];
cin >> word2;
// 這裡之是以寫-2 是因為數組最後一位應該留給\0, 如果寫-1會直接列印空字元串, 因為上來就是\0導緻直接結束
for (int j = 0, i = sizeof(word2) - 2; j < i; j++, i--)
{
char temp;
temp = word2[i];
word2[i] = word2[j];
word2[j] = temp;
}
cout << word2 << endl;
return 0;
}
程式運作結果為:
逗号運算符花絮:
#include "iostream"
#include "string"
using namespace std;
int main()
{
int i = 20, j = 2 * i;
cout << "j = " << j << endl;
// c++規定 逗号運算符的值是第二部分的值
int x = (10, 20);
cout << "x = " << x << endl;
int y = 1;
// 注意這種寫法是不對的 int y = 10, 20;
// 逗号運算符的優先級是最低的, 是以下面這句話被解釋為 (y = 10), 20; 是以20沒有起作用
y = 10, 20;
cout << "y = " << y << endl;
return 0;
}
程式輸出結果:
程式需要注意的都寫在注釋中
字元串比較
先看c語言的字元串比較
// c風格字元串比較
#include "iostream"
#include "cstring"
using namespace std;
int main()
{
char word[5] = "?ate";
for (char c = 'a'; strcmp(word, "mate"); c++)
{
cout << word << endl;
word[0] = c;
}
cout << "finish word is : " << word << endl;
return 0;
}
運作結果為:
c++中使用c語言風格比較兩個字元串是否相同要用strcmp函數, 記得需要導入cstring頭檔案
strcmp(s1, s2); s1, s2可是是數組名也可以是指針,
如果s1和s2相同傳回0,
如果s1在s2的前面傳回-1,
如果s1在s2的後面傳回1
c++裡0在bool類型裡是false, 非0是true, 是以在上面的demo中可以使用strcmp(word, "mate");做for循環是否結束的判斷
再看使用string類進行字元串比較
// string風格字元串比較
#include "iostream"
#include "string"
using namespace std;
int main()
{
string word = "?ate";
for (char c = 'a'; word != "mate"; c++)
{
cout << word << endl;
word[0] = c;
}
cout << "finish word is : " << word << endl;
return 0;
}
運作結果:
c++中的string類重載了運算符!=, 使得能夠在至少有一個操作數是string對象, 而另一個操作數可以使string對象, 也可是是c風格字元串的時候進行比較兩個字元串是否相等.
while循環
// while循環
#include "iostream"
#include "cstring"
using namespace std;
const int ArrSize = 20;
int main()
{
char name[ArrSize];
cout << "please input your name : ";
cin >> name;
cout << "Here is your name : " << endl;
int i = 0;
while (name[i] != '\0')
{
cout << name[i] << " : " << int(name[i]) << endl;
i++;
}
string name2;
cout << "please input your name2 : ";
cin >> name2;
cout << "Here is your name : " << endl;
i = 0;
while (name2[i] != '\0')
{
cout << name2[i] << " : " << int(name2[i]) << endl;
i++;
}
return 0;
}
運作結果:
利用while循環做一個延遲
#include "iostream"
#include "ctime"
using namespace std;
int main() {
cout << "Enter the delay time in secod : " << endl;
float secs;
cin >> secs;
// clock_t 是clock()傳回類型的别名
// CLOCKS_PER_SEC是一個常量表示每秒鐘包含的系統時間機關數
// 都包含在ctime頭檔案中
clock_t delay = secs * CLOCKS_PER_SEC;
cout << "starting\a\n";
clock_t start = clock();
// 利用while做了一個延遲
while (clock() - start < delay);
cout << "done\a\n";
return 0;
}
類型别名
c++為類型建立别名的方式有兩種:
1.使用預處理器
#define BYTE char
這樣預處理器将在編譯程式時使用char代替所有的BYTE, 進而使BYTE稱為char的别名.
2.第二種方法是使用c++的關鍵詞typedef來建立别名
typedef char byte;
typedef char * byte_pointer; // 将byte_pointer聲明為char指針