225.用队列实现栈

(写给未来遗忘的自己)

题目:

代码: 


class MyStack {
public:
    std::queue<int> first_queue;
    std::queue<int> last_queue;

    MyStack() {
    }

    void push(int x) {
        first_queue.push(x);
    }

    int pop() {
        while (first_queue.size() > 1) {
            last_queue.push(first_queue.front());
            first_queue.pop();
        }
        int element = first_queue.front();
        first_queue.pop();
        // 将元素移回 first_queue
        while (!last_queue.empty()) {
            first_queue.push(last_queue.front());
            last_queue.pop();
        }
        return element;
    }

    int top() {
        int element_top = this->pop();
        first_queue.push(element_top); // 再次将弹出的元素推回队列
        return element_top;
    }

    bool empty() {
        return first_queue.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

解析:

1.使用两个队列,一个队列作为执行队列,一个队列作为过渡队列。

2. top 函数的实现和pop函数的一部分 所以利用this指针(介绍放后面)来调用函数,避免代码的复写。

this指针的使用:

this 指针是C++中的一个特殊指针,用于指向当前对象在一个类的成员函数中,this 指针指向调用该成员函数的对象this 指针的使用场景很多,以下是几个常见的用法:

1. 访问对象的成员

class MyClass {
public:
    int x;
    MyClass(int x) {
        this->x = x;  // 通过 this 指针访问成员变量
    }
    void print() {
        std::cout << "x = " << this->x << std::endl;  // 使用 this 访问成员变量
    }
};

2. 返回对象自身 

在实现链式调用时,this 指针常用于在成员函数中返回对象自身的引用

class MyClass {
public:
    MyClass& setX(int x) {
        this->x = x;
        return *this;  // 返回对象自身的引用 
    }
    MyClass& setY(int y) {
        this->y = y;
        return *this;  // 返回对象自身的引用
    }
    void print() {
        std::cout << "x = " << x << ", y = " << y << std::endl;
    }
private:
    int x, y;
};

int main() {
    MyClass obj;
    obj.setX(10).setY(20).print();  // 链式调用
    return 0;
}

/*

在 MyClass& setX(int x) 函数中,*this 是一个表达式,
用于解引用 this 指针,得到当前对象的引用。

具体来说:

this 是一个指向当前对象的指针,其类型是 MyClass*。
*this 解引用 this 指针,将指针转换为该对象的引用,其类型是 MyClass&。
因此,return *this; 的意思是返回当前对象的引用。
由于 setX 函数的返回类型是 MyClass&,
这允许该函数返回对当前对象的引用,使得链式调用成为可能。


MyClass obj;
obj.setX(10);

在 obj.setX(10) 这行代码中:

this 指针指向 obj。
*this 就是 obj 本身的引用。
return *this; 语句返回了 obj 的引用,
使得在调用 setX 函数之后,你可以继续对 obj 进行其他操作,
例如链式调用 setY 函数或其他成员函数。

*/

3. 区分成员变量和局部变量

当成员变量和函数参数(或局部变量)同名时,this 指针可以帮助区分它们。

class MyClass {
public:
    int x;
    MyClass(int x) {
        this->x = x;  // 使用 this 指针区分成员变量和参数 
                      //如果x=x是将参数赋值给自己
    }
};

4. 在运算符重载中使用

在运算符重载中,this 指针常用于返回当前对象的引用,特别是在重载赋值运算符时。

class MyClass {
public:
    int x;
    MyClass& operator=(const MyClass& other) {
        if (this == &other)  // 检查自赋值
            return *this;
        this->x = other.x;  // 赋值操作
        return *this;  // 返回对象自身的引用
    }
};

5. 智能指针场景

在某些高级编程场景中,this 指针可以与智能指针(如 std::shared_ptr)结合使用,例如在一个对象内部创建其自身的智能指针。

class MyClass : public std::enable_shared_from_this<MyClass> {
public:
    std::shared_ptr<MyClass> getPtr() {
        return shared_from_this();  // 返回指向自身的智能指针
    }
};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值