阶段练习- C++智能指针和使用流输入输出(初学者)

1. 练习题一:使用智能指针管理动态分配的内存。

#include <iostream>
#include <memory>

int main() {
    std::unique_ptr<int> ptr = std::make_unique<int>(42);
    std::cout << "Value: " << *ptr << std::endl;
    return 0;
}

解释:本练习题中,我们使用了 std::unique_ptr 来管理一个动态分配的整数对象。通过 std::make_unique 函数创建了一个包含初始值 42 的整数对象,并将其赋值给 ptr。然后,我们通过解引用操作符 * 获取该对象的值,并输出到标准输出流中。

2. 练习题二:使用智能指针进行深复制。

#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass(int value) : value_(value) {}
    int getValue() const { return value_; }
private:
    int value_;
};

int main() {
    std::unique_ptr<MyClass> original = std::make_unique<MyClass>(42);
    std::unique_ptr<MyClass> copy = std::make_unique<MyClass>(*original);
    std::cout << "Original value: " << original->getValue() << std::endl;
    std::cout << "Copy value: " << copy->getValue() << std::endl;
    return 0;
}

解释:本练习题中,我们定义了一个名为 MyClass 的类,其中包含一个整数值成员变量。我们使用 std::unique_ptr 来管理 MyClass 对象的生命周期。首先,我们创建了一个原始对象 original,其值为 42。然后,我们使用深复制的方式创建了一个新的对象 copy,将原始对象的值复制给了新对象。最后,我们分别输出了原始对象和新对象的值。

3. 练习题三:使用智能指针进行引用计数。

#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass(int value) : value_(value) {}
    int getValue() const { return value_; }
private:
    int value_;
};

int main() {
    std::shared_ptr<MyClass> obj1 = std::make_shared<MyClass>(42);
    std::shared_ptr<MyClass> obj2 = obj1;
    std::cout << "Object 1 value: " << obj1->getValue() << std::endl;
    std::cout << "Object 2 value: " << obj2->getValue() << std::endl;
    std::cout << "Reference count: " << obj1.use_count() << std::endl;
    return 0;
}

解释:本练习题中,我们同样定义了一个名为 MyClass 的类,并使用 std::shared_ptr 来管理 MyClass 对象的生命周期。我们创建了一个对象 obj1,其值为 42。然后,我们将 obj1 赋值给另一个 std::shared_ptr 对象 obj2,实现了对同一个对象的共享访问。最后,我们分别输出了两个对象的值以及它们的引用计数。

4. 练习题四:使用流输入输出读取和写入文件。

#include <fstream>
#include <iostream>
#include <string>

int main() {
    // 读取文件内容
    std::ifstream inputFile("input.txt");
    if (!inputFile) {
        std::cerr << "Error opening input file." << std::endl;
        return 1;
    }
    std::string line;
    while (std::getline(inputFile, line)) {
        std::cout << line << std::endl;
    }
    inputFile.close();

    // 写入文件内容
    std::ofstream outputFile("output.txt");
    if (!outputFile) {
        std::cerr << "Error opening output file." << std::endl;
        return 1;
    }
    outputFile << "Hello, World!" << std::endl;
    outputFile.close();

    return 0;
}

解释:本练习题中,我们使用 std::ifstreamstd::ofstream 分别读取和写入文件。首先,我们打开名为 “input.txt” 的文件,并逐行读取文件内容,将其输出到标准输出流中。然后,我们关闭输入文件。接着,我们打开名为 “output.txt” 的文件,并向其中写入一行文本 “Hello, World!”。最后,我们关闭输出文件。

5. 练习题五:使用流输入输出格式化输出。

#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.14159265358979323846;
    int width = 10;
    std::cout << std::setw(width) << std::right << std::fixed << std::setprecision(2) << pi << std::endl;
    return 0;
}

解释:本练习题中,我们使用 std::setwstd::rightstd::fixedstd::setprecision 等操纵符来格式化输出浮点数。我们设置了宽度为 10,右对齐,保留两位小数,并将圆周率的值输出到标准输出流中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值