來自Stanley B.Lippman的《Essential C++》第一章重要內容的總結,第一章目錄:
一、對象定義
對象命名
數據類型:1、內置數據類型如 int 、 string等
string user_Name;
2、用戶自定義class類型,complex是一個templex class,所以定義時complex後有尖括號
#include<complex>
complex<double> pure(0,7);
二、Array和vector
array定義:元素類型+名稱+尺度大小
int arr[3];
vector定義:Vector時template class,必須在尖括號內指定元素類型
vector<int> vec(18);
arrray初始化:用逗號分隔開數據
int arr[3]={1,2,3};
vector初始化:1、爲每個元素指定值 2、利用已經初始化array作爲初值
int elem_vals[3]={1,3,4};
vector<int> elem_seq(elem_vals,elem_vals+3);
vector有一點方便的,它知道自己的大小是多大。通過vector.size();
三、指針帶來彈性
其他都和C差不多,考慮一個實際例子,若要利用指針操作6個Vector,有兩種方法
1、指針指向“元素類型爲int“的Vector
vector<int> fibonacci,lucas,pell;
vector<int> *py=0; //指針指向vector<int>
py=&fibonacci;
...
py=&pell;
2、將每個數列的內存地址存入某個Vector,通過索引訪問這些Vector。
//seq_addrs是個array,其元素類型是 vector<int> *
vector<int> *seq_addrs[3]={&fibonacci,&lucas,&pell};
//因此可以通過一個索引指而非其名稱來訪問每個vector
vector<int> *current_vec=0;
//...
for(int ix=0;ix<3;ix++)
{
current_vec = seq_addrs[ix];
}
四、程序練習
Task1:
//任務介紹,猜出數列的下一個數是什麼
//Fibonacci [1,1,2,3,5,8,13,21]
//lucas [1,3,4,7,11,18,29,47]
//Pell [1,2,5,12,29,70,169,408]
//Triangluar[1,3,6,10,15,21,28,36]
//在容器內放入16個數據,分爲四組,每一組的前三個數據用於顯示,第四個數值顯示數列中的下一元素
//在循環迭代過程中,每次索引增加4,依次走訪完4組數據。每組數據從第二個開始
#include<iostream>
#include<vector>
using namespace std; //mark!!!!!!!!!!!!!!!!!!!!!
int cur_turple = 0;
bool next_seq = true;
int seq[16] = { 1,2,3,5,3,4,7,11,2,5,12,29,3,6,10,15 };
vector<int> sequence(seq, seq + 16);
int main()
{
while (next_seq == true && cur_turple < 16)
{
cout << sequence[cur_turple] << " " << sequence[cur_turple + 1] <<" " << sequence[cur_turple + 2] << endl;
cout << "guess the forth number :";
int temp;
cin >> temp;
if (temp == sequence[cur_turple + 3]) {
cout << "correct" << endl;
cur_turple = cur_turple + 4;
}
else {
cout << "incorrect"<<endl;
cout << "try again? Y/N"<<endl;
char usr_choose;
cin >> usr_choose;
if (usr_choose =='N')
next_seq = false;
}
}
getchar();
getchar();
return 0;
}
Task2:利用指針增加彈性
//Tast2 隨機選擇數列,隨機選擇索引
#include<iostream>
#include<vector>
#include<cstdlib>
#define random(x) (rand()%x)
#include<time.h>
using namespace std;
int a[8] = { 1, 1, 2, 3, 5, 8, 13, 21 };
int b[8] = { 1,3,4,7,11,18,29,47 };
int c[8] = { 1, 2, 5, 12, 29, 70, 169, 408 };
int d[8] = { 1, 3, 6, 10, 15, 21, 28, 36 };
vector<int> Fibonacci(a, a + 8);
vector<int> lucas(b, b + 8);
vector<int> Pell(c, c + 8);
vector<int> Triangluar(d, d + 8);
int seq_index = 0;
vector<int> *current_vec = 0;
bool next_seq = true;
const int seq_cnt = 4;
vector<int> *seq[seq_cnt] = { &Fibonacci, &lucas, &Pell, &Triangluar };
char usr_choose;
int main()
{
srand((int)time(0));
while (next_seq == true )
{
seq_index = random(seq_cnt);
current_vec=seq[seq_index];
bool answer_flag = true;
while (answer_flag) {
cout << (*current_vec)[0];//mark!!!!!!!!!!!!!!!!!!!!
cout << " " << (*current_vec)[1] << " " << (*current_vec)[2] << endl;
cout << "guess the forth number :";
int temp;
cin >> temp;
if (temp == (*current_vec)[3]) {
cout << "correct" << endl;
answer_flag = false;
}
else {
cout << "incorrect" << endl;
cout << "try again? Y/N" << endl;
cin >> usr_choose;
if (usr_choose == 'N'){
next_seq = false;
answer_flag = false;
}
else
answer_flag = true;
}
}
}
getchar();
getchar();
return 0;
}
五、編程過程遇到的問題&&總結
1、對齊方法:Ctrl+K+F對單行對齊,若要對所有代碼對齊,先Ctrl+A選中全體
2、容易忘記寫
using namespace std;
3、產生隨機數
https://www.cnblogs.com/vectors07/p/8185215.html
4、在一個項目中寫多個包含main函數的源文件並分別調試運行
https://blog.csdn.net/qq_35556064/article/details/84586081
5、提領(deference) vector<int> * 向量變量中的值(Task2中的內容)
(*current_vec)[3]) //由於下標運算符的優先級較高,因此提領操作的兩旁必須加上小括號