Essential C++ review notes (1)

I haven't written code for a long time, and I forgot many things. review

Basics of C++ Writing

Header files and input and output

#include<iostream>
using namespace std;
cout<<"Hello world!";
cin>>use_name;

operator precedence

logical operator NOT
arithmetic operator */%
arithmetic operator + -
relational operator > < <= > =
relational operator == !=
logical operator AND
logical operator OR
assignment operator

How to use Array and Vector

vector, which can store 18 elements, and the initial value of each element is 0;

#include<vector>
const int seq_siz = 18;
vector<int> pell_seq(seq_size);

file reading and writing

#include<fstream>
//供输出的文件
ofstream outfile("seq_data.txt");//如果该文件不存在就新建一个文件以供使用
ofstream outfile("seq_data.txt",ios_base::app);
//如果文件已经存在且我们不希望丢弃原来的内容,而是希望将新数据增加到该文件中
//以追加模式打开该文件
if(!outfile)  //检查文件是否成功打开
//共读取的文件
ifstream infile("seq_data.txt");

object-oriented programming style

quote

int ival = 1024;
int &rval = ival;//reference(引用),代表一个int对象
int jval = 4096;
rval = jval;//就是将rval也就是ival设置为了4096;
            //无法令rval转而代表jval,必须从一而终
//当我们以by reference方式将对象作为函数参数传入时,对象本身并不会复制出另一份--复制的是
//对象的地址
//pointer可能并没有指向某个实际对象,所以使用前需要检查
//而引用则必定会代表某个对象

Scope and scope

With the exception of static, objects defined within a function only exist during the execution of the function.

If you want to return the address of a so-called local object, it will cause a runtime error

dynamic memory management

new Type;
int *pi;
pi = new int(1024);
delete pi;
delete [] pi;//删除数组中的所有对象

Provide default parameter values

void bubble_sort(vector<int> &vec,ofstream *ofil = 0);
//给*0fil提供了一个默认参数0
//默认值的解析操作由最右边开始执行,意思就是,一个带有默认参数的参数,它右边的所有参数必须也由默认参数
//默认值只能指定一次
//为了高可见性,有时会将默认值放在函数声明中,包含在头文件里
void display(const vector<int>&,ostream&=cout);
//而在函数定义处,并没有指定参数的默认值

Use local static objects

const vector<int>* fibon_seq(int size)
{ 
  static vector<int> elems;
  //函数的工作逻辑放在此处
  return &elems;
}

elems is defined as a local static object in the fibon_seq() function, the existence of this object will not be re-established and destroyed due to the call and end of the function

Provide overloaded functions

void display_message(char ch);
void display_message(const string&, int);
//编译器无法根据函数返回类型来区分两个具体相同名称的函数

define and use a template function

function template (function template)

Extract the type information of all (or some) parameters specified in the parameter list

template <typename elemType>
void display_message(const string &msg,const vector<elemType> &vec)
{
  cout<<msg;
  for(int ix = 0;ix < vec.size();++ix)
  {
    elemType t = vec[ix];
    cout<<t<<'';
  }
}

The identifier acts as a placeholder for the function parameter list and some actual data types in the function body

How to use function templates:

vector<int> ivec;
string mag;
//...
display_message(msg,ivec);

The editor will bind elemType to int type, and then generate a display_message() function instance, so the type of its second parameter becomes vector<int>, and the local object type inside the function body also becomes int

Function pointers bring more flexibility

const vector<int>* (*seq_ptr)(int);
bool seq_elem(int pos,int &elem,const vector<int>* (*seq_ptr)(int))
{
  //调用seq_ptr所指的函数
  const vector<int>*pesq = seq_ptr(pos);
  if(!pesq)
  {elem = 0;return false;}
  elem = (*pseq)[pos - 1];
  return true;
}
//提供函数的名字就可以将函数的地址交给指针
//将pell_seq()的地址赋值给seq_ptr
seq_ptr = pell_seq;

set header file

Header file naming suffix.h

There can only be one definition of a function, and multiple declarations. Do not put the declaration of the function into the header file, because multiple code files of the same program may include this header file

//这是可以放在头文件中的一份声明
extern const vector<int>* (*seq_array[seq_cnt])(int);
//这会被视为定义
const vector<int>* (*seq_array[seq_cnt])(int);

generic programming style

STL

Container: vector list set map etc.

Generic algorithms: find(), sort(), replace(), merge(), etc.

iterator iterator: first last

Each standard container provides an operator function called begin() that returns an iterator pointing to the first element, and another operator function called end() that returns an iterator pointing to the next position of the last element

Understand Iterator (generic pointer)

object-based programming style

class Stack
{
  public:
    //...public接口
  private:
    //...private实现部分
}

All member functions must be declared in the class body, and the definition can be outside the class. If it is defined in the class body, the member function will be automatically regarded as an inline function

//类体外定义函数,如果希望是inline函数,要使用inline关键字
inline bool Stack::empty()
{...}
bool Stack pop()
{...}
//Stack:: 这两个冒号就是所谓的class scpoe resolution (类作用域解析)运算符

Constructors and Destructors

//构造函数的名称必须与类名相同,不需要返回值,可以被重载
class Triangular{
public:
  Triangular();//default constructors
  Triangular(int len);
  Triangular(int len,int beg_pos);
  //...
}
Triangular t3 =  8;//调用第二个构造函数

Member initialization list Member initialization List

Triangular::Triangular(const Triangular &rhs)
  :_length (rhs._length),_beg_pos(rhs._beg_pos),
  _next(rhs._beg_pos-1)  //不要在这里写分号  ;
{} //空的

destructor

//名称:class的名称再加一个~,没有返回值,也没有任何参数,所以也不会被重载
//一旦某个class提供有destrustor,当其object结束生命时,便会自动调用destructor处理善后
//destructor主要用来释放在constructor中或对象生命周期中分配的资源
class Matrix{
public:
  ...
  ~Matrix()
  {
    delete [] _pmat;
  }
}

mutable (variable) and const (unchanged)

Guess you like

Origin blog.csdn.net/zaizai1007/article/details/129695844