Essential C++浓缩笔记(一)——C++编程基础

来自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])    //由于下标运算符的优先级较高,因此提领操作的两旁必须加上小括号

 

 

发布了31 篇原创文章 · 获赞 143 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章