(1)
常识:page 1 - 25
布尔值:
字符:
整数:
浮点数:算数运算符:
关联运算符:
逻辑运算符:
等号运算符:
小于等于符:
赋值运算符:
累加运算符:
条件运算符:
复合运算符:
条件分支:
循环控制:
抽象化程序库:字符串,向量vector
示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string user_name;
cout << "Please enter your name: ";
cin << '\n'
<< "Hello, "
<< user_name
<< " ...and goodbye!\n";
return 0;
}
namespace :命名空间
初始化 :
1.int a = 0; //源自c语言
2.int a(0); //构造函数语法
array和vector的用法
vector是个class template
#include<vector>
vector<int> elem_seq(seq_size);
elem_seq[0] = 1;
elem_seq[1] = 2;
//……初始化的一种方法
//-------------------
int elem_val[seq_size] = {……};//array初始化
vector<int> elem_seq{elem_val , elem_val + seq_size};
//用array的值来初始化elem_seq
elem_seq.size()返回vector的大小
#include <iostream>
#include <cstdlib>//
using namespace std;
const int seq_cnt = 6;
vector<int> *seq_addre[ seq_cnt ] = {
&fibonacci,&lucas,&pell,
&triangular,&square,&pentagonal
};
vector<int> *current_vec = 0;
for (int ix = 0;ix < seq_cnt; ++ix)
{
current_vec = seq_cnt[ix];
}
文件的读写:
头文件:fstream
#include <fstream>
ofstream outfile("seq_data.txt");//供输出用的对象
//追加模式是 ofstream outfile("seq_data.txt" , ios_base::app);
//追加模式是原文件已经存在,并且不希望丢弃原来的内容
if( ! outfile) //判别文件是否成功开启
//未能开启,写入cerr,标准错误输出设备,立即显示于终端上,无缓存
cerr << "Oops!Unable to save session data!\n";
else
//开启成功,将信息写入
outfile << usr_name << ' '
<< num_tries << ' '
<< num_right << endl;
//endl插入一个换行字符,并清除输出缓冲区的内容
//开启一个可供读取的文件,可定义ifstream对象
ifstream infile("seq_data.txt");
int num_tries = 0;
int num_cor = 0;
if (! infile)
{
//由于某种原因,文件无法开启
//我们将假设这是一个新的用户
}
else
{
//ok,读取文件中的每一行
//检查用户是否曾经用过这个程序
//每一行的格式是:
//name num_tries num_correct
// Anna 24 19
// ……
// nt:猜过的总次数(num_tries)
// nc:才对的总次数(num_correct)
string name;
int nt;
int nc;
while (infile >> name )//每次迭代读取文件下一行,直到文件尾端
{
infile >> nt >> nc;
if (name == usr_name )
{
cout << "Welcome back, " << usr_name
<< "\nYour current score is " << nc
<< " out of " << nt << "\nGood Luck!\n";
num_tries = nt;
num_cor = nc;
}
}
}
//同时读写同一个文件,定义一个fstream对象,为了以追加模式开启,
//掺入第二个参数值 ios_base::in|ios_base::app
fstream iofile("seq_data.txt",
ios_base::in|ios_base::app );
if (! iofile )
//由于某种原因,文件无法开启
else
{
//开始读取之前,将文件重新定位至起始处
iofile.seekg( 0 );
//当以附加模式来开启文件时,文件位置会位于尾端,如果没有重新定位
//那么立刻就会遇上“读到文件尾”的状况,seekg()可将文件位置重新定位到起始处
}