笔试面试相关记录(8)

文章详细介绍了C++中的类定义,包括构造函数的使用、运算符重载(如自增运算符),以及如何用栈实现双端队列和寻找两个有序数组的中位数。最后展示了几个编译相关的代码片段和输出结果。
摘要由CSDN通过智能技术生成

(1)

#include <iostream>

using namespace std;
class A {
private:
    int a1 = 0;
public:
    A() {}
    A(const A& a) {
        // cout << "A(const A& a)" << endl;
        // this->a1 = a.a1;
    }
    A operator++(int) {
        A T = *this;
        a1++; 
        cout << "++(int)" << " " << this->a1 << " " << T.a1 << endl;
        return T;
    }
    A& operator++() {
        A T = *this; 
        a1++; 
        cout << "++()" << " " << this->a1 << " " << T.a1 << endl;
        return *this;
    }
    A& operator=(const A& a) {
        cout << "=(const A& a)" << endl;
        this->a1 = a.a1;
    }
    void print() {cout << a1 << " "<< this->a1 << endl;}
};
int main()
{
    A obj;
    // 调用++(),返回*this&,输出1,在A T = *this时会调用拷贝构造函数,但是
    // 拷贝构造函数中并没有任何操作,所以T.a1 = 0, 返回*this对象调用print(),输出1 1
    (++obj).print();
    // 调用++(int),返回T,此时a1=2,A T = *this时会调用拷贝构造函数,但是
    // 拷贝构造函数中并没有任何操作,所以T.a1 = 0,输出++(int) 2 0
    obj++;
    // 输出2 2
    obj.print();
    // 调用++(int),T.a1 = 0, 输出++(int) 3 0,返回T,调用print输出0 0
    (obj++).print();
    // 调用++(),T.a1 = 0,输出++() 4 0,返回*this,调用print输出4 4
    (++obj).print();
    return 0;
}

输出:
++() 1 0
1 1
++(int) 2 0
2 2
++(int) 3 0
0 0
++() 4 0
4 4

(2)

#include <iostream>

using namespace std;
class A {
private:
    int a1 = 0;
public:
    A() {}
    A(const A& a) {
        cout << "A(const A& a)" << endl;
        this->a1 = a.a1;
    }
    A operator++(int) {
        A T = *this;
        a1++; 
        cout << "++(int)" << " " << this->a1 << " " << T.a1 << endl;
        return T;
    }
    A& operator++() {
        A T = *this; 
        a1++; 
        cout << "++()" << " " << this->a1 << " " << T.a1 << endl;
        return *this;
    }
    A& operator=(const A& a) {
        cout << "=(const A& a)" << endl;
        this->a1 = a.a1;
    }
    void print() {cout << a1 << " "<< this->a1 << endl;}
};
int main()
{
    A obj;
    // 调用++(),返回*this&,输出1,在A T = *this时会调用拷贝构造函数,
    // 所以T.a1 = 0, a1++, 返回*this对象调用print(),输出1 1
    (++obj).print();
    // 调用++(int),返回T,此时a1=2,A T = *this时会调用拷贝构造函数,
    // T.a1 = 1,输出++(int) 2 1
    obj++;
    // 输出2 2
    obj.print();
    // 调用++(int),T.a1 = 2, 输出++(int) 3 2,返回T,调用print输出2 2
    (obj++).print();
    // 调用++(),T.a1 = 3,输出++() 4 3,返回*this,调用print输出4 4
    (++obj).print();
    return 0;
}

输出:
A(const A& a)
++() 1 0
1 1
A(const A& a)
++(int) 2 1
2 2
A(const A& a)
++(int) 3 2
2 2
A(const A& a)
++() 4 3
4 4

(3)用两个栈来实现双端队列

#include <stack>
#include <iostream>
#include <string>
using namespace  std;

class MyDeque {
public:
    void push_back(int num) {
        stk1.push(num);
        size++;
    }

    void push_front(int num) {
        while (stk1.size()) {
            int t = stk1.top();
            stk2.push(t);
            stk1.pop();
        }
        stk1.push(num);
        while (stk2.size()) {
            int t = stk2.top();
            stk1.push(t);
            stk2.pop();
        }
        size++;
    }

    int pop_front() {
        while (stk1.size()) {
            int t = stk1.top();
            stk2.push(t);
            stk1.pop();
        }
        int res = stk2.top();
        stk2.pop();
        while (stk2.size()) {
            int t = stk2.top();
            stk1.push(t);
            stk2.pop();
        }
        size--;
        return res;
    }

    int pop_back() {
        int res = stk1.top();
        stk1.pop();
        size--;
        return res;
    }

    bool empty() const {
        return size == 0;
    }
private:
    stack<int> stk1, stk2;
    int size = 0;
};

int main() {
    string str;
    while (getline(cin, str)) {
        MyDeque mydeque;
        int len = str.size();
        int i = 0;
        // F1 F2 F3 F4 F5 B1 B2 B3 B4 B5 PF PF PB PB
        while (i < len) {
            if (str[i] == 'F') {
                int j = i+1;
                int num = 0;
                while (str[j] != ' ') {
                    num = num*10 + (str[j]-'0');
                    j++;
                }
                mydeque.push_front(num);
                i = j+1;
            } else if (str[i] == 'B') {
                int j = i+1;
                int num = 0;
                while (str[j] != ' ') {
                    num = num*10 + (str[j] - '0');
                    j++;
                }
                mydeque.push_back(num);
                i = j+1;
            } else if (str[i] == 'P' && str[i+1] == 'F') {
                cout << mydeque.pop_front() << " ";
                i += 2;
            } else if (str[i] == 'P' && str[i+1] == 'B') {
                cout << mydeque.pop_back() << " ";
                i += 2;
            } else if (str[i] == ' ') {
                i++;
            }
        }
    }
}

(4)找两个有序数组的中位数

输入:
// a : [1, 3, 5]
// b:  [2, 4, 6]

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// a : [1, 3, 5]
// b:  [2, 4, 6]
vector<int> getVector(string str) {
    int len = str.size();
    vector<int> ans;
    int t = 0;
    for (int i = 0; i < len; i++) {
        if (isdigit(str[i])) {
            t = t*10 + (str[i]-'0');
        } else if (str[i] == ',') {
            ans.push_back(t);
            t = 0;
        }
    }
    ans.push_back(t);
    return ans;
}

int main() {
    string str1, str2;
    while (getline(cin, str1)) { // 注意 while 处理多个 case
        getline(cin, str2);
        vector<int> num1 = getVector(str1);
        vector<int> num2 = getVector(str2);
        // for (int i = 0; i < num1.size(); i++) {
        //     cout << num1[i] << " - ";
        // }
        // cout << endl;
        // for (int i = 0; i < num2.size(); i++) {
        //     cout << num2[i] << " = ";
        // }
        int n = num1.size();
        vector<int> total(2*n);
        int i = 0, j = 0, k = 0;
        while (i < n && j < n) {
            if (num1[i] < num2[j]) {
                total[k++] = num1[i++];
            } else {
                total[k++] = num2[j++];
            }
        }
        while (i < n) {
            total[k++] = num1[i++];
        }
        while (j < n) {
            total[k++] = num2[j++];
        }
        cout << float(total[n-1] + total[n]) / 2 << endl;
    }
}

(5)

#include <iostream>
using namespace std;


int main()
{
    int a = 5;
    a += a*= a%= 3;
    cout << a;
    return 0;
}
输出: 8

(6)

#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    // auto f = [a, &b]()->int{return a+ ++b;};
    // int c = f();
    // cout << b;
    cout << a+ ++b << " " << a << " " << b << endl;
    a = 1;
    b = 2;
    cout << a+++b << " " << a << " " << b << endl;
    a = 1;
    b = 2;
    cout << a++ +b << " " << a << " " << b << endl;
    return 0;
}
输出:
4 1 3
3 2 2
3 2 2
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [a, &b]()->int{return a+ ++b;};
    int c = f();
    cout << b;
    return 0;
}
输出:3
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [a, &b]()->int{return a+++b;};
    int c = f();
    cout << b;
    return 0;
}
编译报错
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [&]()->int{return a+++b;};
    int c = f();
    cout << a << " " << b;
    return 0;
}
输出:2 2
#include <iostream>
using namespace std;


int main()
{
    int a = 1;
    int b = 2;
    auto f = [&]()->int{return a+ ++b;};
    int c = f();
    cout << a << " " << b;
    return 0;
}
输出:1 3
#include <iostream>
using namespace std;


int main()
{
    printf("%lld", long((int*)(0)+1) - long((int*)(0)));
    return 0;
}
输出:4

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值