Essential C++ 笔记 - 第一章C++编程基础

Essential C++ 笔记 - 第一章C++编程基础

一、array 和 vector

// 使用vector + 指针增加数组的灵活性
#include 
using namespace std;

vector<int> fibonacci, lucas, pell, triangular, square, pentagonal;

const int seq_cnt = 6;

// 一个指针数组,容量为seq_cnt
// 每个指针都指向vector对象
vector<int> *seq_addrs[seq_cnt] = {
	&fibonacci, &lucas, &pell,
	&triangular, &square, &pentagonal
};

// 通过一个索引值来存取个别的vector
vector<int> *current_vec = 0;

for (int ix = 0; ix < seq_cnt; ++ix) {
	current_vec = seq_addrs[ix];
	// 。。。
}

二、文件的读写

#include 

// 定义一个ofstream对象,并将文件名传入
// 如果指定的文件不存在,则会新创建一个文件
// 以下方式,文件中数据会被覆盖
ofstream outfile("seq_data.txt");

// 以下方式,文件中数据会被追加
ofstream outfile("sqe_data.txt", ios_base:app);

// 确认文件是否成功打开
if (!outfile) {
	cerr << "open file error" << endl; // cerr无缓冲输出
}
else {
	outfile << "tets" << endl;
}

// 定义一个ifstream对象,并将文件名传入
// 确认文件是否成功打开
ifstream infile("seq_data.txt");

if (!infile) {
	cerr << "open file error" << endl;
}

三、运算符优先级

逻辑运算符 NOT (!)
算术运算符 *, /, %
算术运算符 +, -
比较运算符 <, >, <=, >=
比较运算符 ==, !=
逻辑运算符 AND (&)
逻辑运算符 OR (|)
复制运算符 =
// 从上到下,优先级降低
// 同一行运算符具有相同优先级

你可能感兴趣的:(Essential,C++)