2019-10-25

关于类的复制(深层and浅层)和移动构造
附一份郑莉老师讲解的类代码(一直在copycopy..万一以后还要用到呢)

class Point {
public:
    Point() :x(0), y(0) {
        cout << "Dafault Constructor called." << endl;
    }
    Point(int x, int y) :x(x), y(y) {
        cout << "Constructor called." << endl;
    }
    ~Point() {
        cout << "Destructor called." << endl;
    }
    int getX()const { return x; }
    int getY()const { return y; }
    void move(int newX, int newY) {
        x = newX;
        y = newY;
    }
private:
    int x, y;

};
class ArrayOfPoints {
public:
    ArrayOfPoints(int size) :size(size) {
        points = new Point[size];
    }
    ~ArrayOfPoints() {
        cout << "Deleting..." << endl;
        delete[]points;
    }
    Point&element(int index) {
        assert(index >= 0 && index < size);
        return points[index];
    }
private:
    Point *points;
    int size;
};

新学习:
1.移动构造
当临时对象复制后不再利用,就可以使用移动构造(不用复制构造),可以节省内存

class_name(class_name&&)

貌似效率高一点。。但是没太看懂,囫囵吞枣了一遍,有空再看看primer
2.string类 #include头文件
比c风格字符串省事
①string[];这样就可以直接定义,从类里调用出来一个原型
后面再赋值时执行类似new str的形式,动态内存,好用
②getline输入字符串
getline一共有三个参数,第一个为输入流(eg:cin),第二个为字符串对象,第三个参数可选:作用为结束的标志。若没有则默认为回车为此语句结束标志

    string city,state;
    getline(cin,city,',');
    getline(cin,state);
    cout<<"City:"<

3.‘0’是0的ASCII码,用字符型的数字减去零的ASCII就得到整型的数字,或者是减去48
洛谷P1055解法分析

#include
using namespace std;
int main(){
    char a[14],mod[12]="0123456789X";
   for(int i=0;i<13;i++){
       cin>>a[i];
   }
    int i,j=1,t=0;
    for(i=0;i<12;i++){
        if(a[i]=='-') continue;
        t+=(a[i]-48)*j++;            //48可以更改为‘0’
    }
    if(mod[t%11]==a[12])
        cout<<"Right";
    else{
        a[12]=mod[t%11];
        for(int i=0;i<13;i++){
        cout<

你可能感兴趣的:(2019-10-25)