C++中的装饰器模式:灵活地扩展功能

简介: C++中的装饰器模式:灵活地扩展功能

装饰器模式简介(Introduction to the Decorator Pattern)

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许我们在不修改现有类的基础上,通过使用包装对象来动态地添加新的行为或者修改原有行为。装饰器模式可以在运行时为对象添加职责,而不会影响其他对象。

装饰器模式UML图

+-------------------+     +---------------------+     +-------------------+
|   Component       |<----|   Decorator         |<----| ConcreteDecorator |
+-------------------+     +---------------------+     +-------------------+
| + operation()     |     | # component         |     | + operation()     |
+-------------------+     | + operation()       |     +-------------------+
                           | + setComponent(comp)|
                           +---------------------+

在这个UML图中,我们可以看到以下几个部分:

  1. Component(抽象组件):定义了一个接口,用于规定具体组件和装饰器所需要实现的方法。
  2. Decorator(抽象装饰器):继承自Component,同时包含一个Component类型的成员变量,用于保存被装饰的对象。Decorator类可以覆盖Component中的方法,并在方法中调用被装饰对象的对应方法。
  3. ConcreteDecorator(具体装饰器):继承自Decorator,实现了Decorator中定义的方法。ConcreteDecorator可以在被装饰对象的基础上添加新的行为或者修改原有行为。

模式定义与解释(Pattern Definition and Explanation)

装饰器模式的主要目的是在不修改现有类的前提下,为对象添加新的行为。这主要通过以下方式实现:

  1. 将被装饰对象(具体组件)作为装饰器的成员变量。
  2. 通过继承和组合的方式,使装饰器能够在运行时动态地修改被装饰对象的行为。
  3. 可以将多个装饰器叠加使用,实现更丰富的功能组合。

装饰器模式的优点:

  1. 可以在不修改原有类的基础上,动态地为对象添加新的功能。
  2. 可以根据需要叠加多个装饰器,实现灵活的功能组合。
  3. 装饰器与被装饰对象之间的关系是松散的,易于扩展。

装饰器模式的缺点:

  1. 会产生许多小的装饰器类,增加系统复杂性。
  2. 在多层装饰器叠加的情况下,可能会导致调试和维护困难。

装饰器模式中的角色与组件(Roles and Components in Decorator Pattern)

抽象组件(Component)

抽象组件是装饰器模式中的核心组件。它定义了一个接口,用于表示需要被装饰的对象。所有具体组件和抽象装饰器都需要实现这个接口。

class Component {
public:
    virtual ~Component() {}
    virtual void operation() = 0;
};

具体组件(Concrete Component)

具体组件是抽象组件的实现。它提供了实际需要被装饰的功能。具体组件可以独立使用,也可以和装饰器一起使用。

class ConcreteComponent : public Component {
public:
    void operation() override {
        // 实际功能的实现
    }
};

抽象装饰器(Decorator)

抽象装饰器是装饰器模式中的关键部分。它继承自抽象组件,同时包含一个抽象组件的引用。抽象装饰器可以在运行时为具体组件动态地添加额外的功能。

class Decorator : public Component {
public:
    explicit Decorator(Component* component) : component(component) {}
    virtual ~Decorator() {}
protected:
    Component* component;
};

具体装饰器(Concrete Decorator)

具体装饰器继承自抽象装饰器,并实现了抽象组件接口。它为具体组件添加新的功能,同时保持了原有功能不变。

class ConcreteDecoratorA : public Decorator {
public:
    explicit ConcreteDecoratorA(Component* component) : Decorator(component) {}
    void operation() override {
        component->operation();
        // 在原有功能基础上添加新功能
    }
};
class ConcreteDecoratorB : public Decorator {
public:
    explicit ConcreteDecoratorB(Component* component) : Decorator(component) {}
    void operation() override {
        component->operation();
        // 在原有功能基础上添加新功能
    }
};

以上就是装饰器模式中的各个角色与组件。装饰器模式允许在运行时动态地为对象添加额外的功能,而不需要修改原有的代码。通过使用抽象装饰器和具体装饰器,我们可以灵活地扩展和修改组件的功能。

C++实现装饰器模式(Implementing Decorator Pattern in C++)

使用C++构建抽象组件类(Creating the Component Class in C++)

在本例中,我们将使用C++17和C++20的特性来实现一个简单的图形绘制程序。我们将使用抽象组件类来定义一个通用的绘制接口,并使用装饰器模式来实现各种图形的绘制功能。

首先,让我们定义一个抽象组件类。我们将使用C++20的concept特性来约束抽象组件类的接口。为了展示C++17特性,我们也将使用std::variant来表示可能的颜色选项。

#include <iostream>
#include <variant>
#include <string>
#include <concepts>
// 使用C++17的std::variant定义颜色类型
using Color = std::variant<std::string, unsigned>;
// 定义Drawable概念
template <typename T>
concept Drawable = requires(T t) {
    { t.draw() } -> std::same_as<void>;
};
// 抽象组件类
class Component {
public:
    virtual ~Component() = default;
    virtual void draw() const = 0;
};

使用C++构建具体组件类(Creating the Concrete Component Class in C++)

现在,让我们创建一个具体的组件类,例如一个矩形。我们将实现矩形的绘制方法,并使用C++17的结构化绑定来获取颜色信息。

class Rectangle : public Component {
public:
    Rectangle(int width, int height, Color color)
        : width_(width), height_(height), color_(std::move(color)) {}
    void draw() const override {
        auto [r, g, b] = getColor();
        std::cout << "Drawing a rectangle with width " << width_
                  << ", height " << height_ << " and color ("
                  << r << ", " << g << ", " << b << ")\n";
    }
private:
    int width_;
    int height_;
    Color color_;
    std::tuple<int, int, int> getColor() const {
        if (const auto* colorName = std::get_if<std::string>(&color_)) {
            if (*colorName == "red")
                return {255, 0, 0};
            else if (*colorName == "green")
                return {0, 255, 0};
            else if (*colorName == "blue")
                return {0, 0, 255};
        }
        else if (const auto* colorValue = std::get_if<unsigned>(&color_)) {
            int r = (*colorValue >> 16) & 0xFF;
            int g = (*colorValue >> 8) & 0xFF;
            int b = *colorValue & 0xFF;
            return {r, g, b};
        }
        // Default color (black)
        return {0, 0, 0};
    }
};

使用C++构建抽象装饰器类(Creating the Decorator Class in C++)

接下来,我们创建一个抽象装饰器类,它将包含一个指向Component的指针。这个类还将重写draw()方法,以便我们可以在运行时动态地为组件添加新功能。

class Decorator : public Component {
public:
    explicit Decorator(Component* component) : component_(component) {}
    void draw() const override {
        component_->draw();
    }
private:
    Component* component_;
};

使用C++构建具体装饰器类(Creating Concrete Decorator Classes in C++)

现在我们将创建具体的装饰器类。在这个示例中,我们将创建一个实现边框功能的BorderDecorator类,以及一个实现阴影功能的ShadowDecorator类。

首先,让我们创建BorderDecorator

class BorderDecorator : public Decorator {
public:
    BorderDecorator(Component* component, int borderWidth)
        : Decorator(component), borderWidth_(borderWidth) {}
    void draw() const override {
        Decorator::draw();
        std::cout << "Adding a border with width " << borderWidth_ << std::endl;
    }
private:
    int borderWidth_;
};

接下来,我们创建ShadowDecorator

class ShadowDecorator : public Decorator {
public:
    ShadowDecorator(Component* component, int shadowOffsetX, int shadowOffsetY)
        : Decorator(component), shadowOffsetX_(shadowOffsetX), shadowOffsetY_(shadowOffsetY) {}
    void draw() const override {
        Decorator::draw();
        std::cout << "Adding a shadow with offset (" << shadowOffsetX_
                  << ", " << shadowOffsetY_ << ")\n";
    }
private:
    int shadowOffsetX_;
    int shadowOffsetY_;
};

现在,我们可以将这些装饰器类应用到组件上。例如,我们可以创建一个带有边框和阴影的矩形。

int main() {
    Component* rectangle = new Rectangle(10, 20, "blue");
    Component* borderedRectangle = new BorderDecorator(rectangle, 2);
    Component* borderedAndShadowedRectangle = new ShadowDecorator(borderedRectangle, 5, 5);
    std::cout << "Drawing bordered and shadowed rectangle:\n";
    borderedAndShadowedRectangle->draw();
    delete borderedAndShadowedRectangle;
    delete borderedRectangle;
    delete rectangle;
    return 0;
}

输出结果:

Drawing bordered and shadowed rectangle:
Drawing a rectangle with width 10, height 20 and color (0, 0, 255)
Adding a border with width 2
Adding a shadow with offset (5, 5)

在这个示例中,我们使用了C++17和C++20的特性来创建一个支持动态功能添加的图形绘制程序。装饰器模式使我们能够在运行时为对象添加新功能,而无需修改其现有实现。

示例:用装饰器模式解决实际问题(Examples: Solving Real-World Problems with Decorator Pattern)

案例一:饮料定制系统(Example 1: Beverage Customization System)

在这个案例中,我们将使用C++20的concept, constexprstd::format 特性实现一个饮料定制系统。

首先,我们创建一个基本的抽象饮料类,它包含了一个抽象方法cost(),用于计算饮料的价格。然后,我们将创建一个具体的饮料类,例如咖啡(Coffee),以及一些装饰器类,例如牛奶(Milk)和糖(Sugar)。

#include <iostream>
#include <memory>
#include <string>
#include <format>
// 定义Beverage概念
template <typename T>
concept Beverage = requires(T t) {
    { t.cost() } -> std::same_as<double>;
    { t.description() } -> std::same_as<std::string>;
};
// 抽象饮料类
class AbstractBeverage {
public:
    virtual ~AbstractBeverage() = default;
    virtual double cost() const = 0;
    virtual std::string description() const = 0;
};
// Coffee类
class Coffee : public AbstractBeverage {
public:
    double cost() const override {
        return 2.0;
    }
    std::string description() const override {
        return "Coffee";
    }
};

接下来,我们将创建一个抽象装饰器类,以及具体的装饰器类,如牛奶和糖。

// 抽象装饰器类
class BeverageDecorator : public AbstractBeverage {
public:
    explicit BeverageDecorator(std::unique_ptr<AbstractBeverage> beverage)
        : beverage_(std::move(beverage)) {}
    virtual ~BeverageDecorator() = default;
protected:
    std::unique_ptr<AbstractBeverage> beverage_;
};
// Milk装饰器类
class Milk : public BeverageDecorator {
public:
    using BeverageDecorator::BeverageDecorator;
    double cost() const override {
        return beverage_->cost() + 0.5;
    }
    std::string description() const override {
        return std::format("{} with Milk", beverage_->description());
    }
};
// Sugar装饰器类
class Sugar : public BeverageDecorator {
public:
    using BeverageDecorator::BeverageDecorator;
    double cost() const override {
        return beverage_->cost() + 0.3;
    }
    std::string description() const override {
        return std::format("{} with Sugar", beverage_->description());
    }
};

现在我们已经创建了咖啡、牛奶和糖装饰器类,我们可以使用C++20的constexpr特性实现一个简单的饮料定制系统。

int main() {
    constexpr auto addMilk = true;
    constexpr auto addSugar = true;
    std::unique_ptr<AbstractBeverage> beverage = std::make_unique<Coffee>();
    if (addMilk) {
        beverage = std::make_unique<Milk>(std::move(beverage));
    }
    if (addSugar) {
        beverage = std::make_unique<Sugar>(std::move(beverage));
    }
    std::cout << "Order: " << beverage->description() << std::endl;
    std::cout << "Total cost: " << beverage->cost() << std::endl;
    return 0;
}

案例二:图形界面组件(Example 2: Graphical User Interface Components)

在这个案例中,我们将使用装饰器模式为图形界面组件添加功能。我们将创建一个基本的抽象组件类,并为其添加各种具体的组件(例如按钮和文本框)和装饰器(例如边框和滚动条)。

首先,我们创建一个基本的抽象组件类,它包含了一个抽象方法display(),用于显示组件。

#include <iostream>
#include <memory>
#include <string>
// 抽象组件类
class GUIComponent {
public:
    virtual ~GUIComponent() = default;
    virtual void display() const = 0;
};

接下来,我们将创建一些具体的组件类,例如ButtonTextBox

class Button : public GUIComponent {
public:
    explicit Button(std::string text) : text_(std::move(text)) {}
    void display() const override {
        std::cout << "Button: " << text_ << std::endl;
    }
private:
    std::string text_;
};
class TextBox : public GUIComponent {
public:
    explicit TextBox(std::string text) : text_(std::move(text)) {}
    void display() const override {
        std::cout << "TextBox: " << text_ << std::endl;
    }
private:
    std::string text_;
};

现在,我们创建一个抽象装饰器类,以及一些具体的装饰器类,例如BorderDecoratorScrollbarDecorator

class ComponentDecorator : public GUIComponent {
public:
    explicit ComponentDecorator(std::unique_ptr<GUIComponent> component)
        : component_(std::move(component)) {}
    virtual ~ComponentDecorator() = default;
protected:
    std::unique_ptr<GUIComponent> component_;
};
class BorderDecorator : public ComponentDecorator {
public:
    using ComponentDecorator::ComponentDecorator;
    void display() const override {
        component_->display();
        std::cout << " with border" << std::endl;
    }
};
class ScrollbarDecorator : public ComponentDecorator {
public:
    using ComponentDecorator::ComponentDecorator;
    void display() const override {
        component_->display();
        std::cout << " with scrollbar" << std::endl;
    }
};

最后,我们将创建一些示例组件,并为它们添加装饰器。

int main() {
    std::unique_ptr<GUIComponent> button = std::make_unique<Button>("Submit");
    std::unique_ptr<GUIComponent> textBox = std::make_unique<TextBox>("Enter your name");
    std::unique_ptr<GUIComponent> borderedButton = std::make_unique<BorderDecorator>(std::move(button));
    std::unique_ptr<GUIComponent> borderedTextBox = std::make_unique<BorderDecorator>(std::move(textBox));
    std::unique_ptr<GUIComponent> scrollableTextBox = std::make_unique<ScrollbarDecorator>(std::move(borderedTextBox));
    std::cout << "Displaying components:" << std::endl;
    borderedButton->display();
    scrollableTextBox->display();
    return 0;
}

输出结果:

Displaying components:
Button: Submit
 with border
TextBox: Enter your name
 with border
 with scrollbar

案例三:文本编辑器功能扩展(Example 3: Text Editor Functionality Extension)

在这个案例中,我们将使用C++14的std::make_unique和返回类型后置语法,C++17的结构化绑定和if constexpr,以及C++20的concept, std::spanstd::format特性来实现一个具有功能扩展的文本编辑器。

首先,我们创建一个基本的抽象编辑器类,它包含了抽象方法processText(),用于处理文本。

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <format>
#include <span>
// 定义Editor概念
template <typename T>
concept Editor = requires(T t) {
    { t.processText(std::string{}) } -> std::same_as<std::string>;
};
// 抽象编辑器类
class AbstractEditor {
public:
    virtual ~AbstractEditor() = default;
    virtual std::string processText(const std::string &text) const = 0;
};

接下来,我们创建一个具体的编辑器类,例如BasicEditor,并实现processText()方法。

class BasicEditor : public AbstractEditor {
public:
    // C++14特性:返回类型后置语法
    auto processText(const std::string &text) const -> std::string override {
        return text;
    }
};

现在,我们创建一个抽象功能扩展类,以及一些具体的功能扩展类,例如WordCounterLineCounter

class EditorExtension : public AbstractEditor {
public:
    explicit EditorExtension(std::unique_ptr<AbstractEditor> editor)
        : editor_(std::move(editor)) {}
    virtual ~EditorExtension() = default;
protected:
    std::unique_ptr<AbstractEditor> editor_;
};
class WordCounter : public EditorExtension {
public:
    using EditorExtension::EditorExtension;
    std::string processText(const std::string &text) const override {
        auto processedText = editor_->processText(text);
        // C++17特性:结构化绑定
        auto [wordCount, _] = countWordsAndLines(processedText);
        return std::format("{} ({} words)", processedText, wordCount);
    }
private:
    std::pair<int, int> countWordsAndLines(const std::string &text) const {
        int wordCount = 0, lineCount = 0;
        bool newWord = true;
        for (char c : text) {
            if (c == ' ' || c == '\t') {
                newWord = true;
            } else if (c == '\n') {
                newWord = true;
                ++lineCount;
            } else if (newWord) {
                newWord = false;
                ++wordCount;
            }
        }
        return {wordCount, lineCount};
    }
};
class LineCounter : public EditorExtension {
public:
    using EditorExtension::EditorExtension;
    std::string processText(const std::string &text) const override {
        auto processedText = editor_->processText(text);
        // C++17特性:结构化绑定
        auto [_, lineCount] = countWordsAndLines(processedText);
        return std::format("{} ({} lines)", processedText, lineCount);
    }
private:
    // 使用已在 WordCounter 中定义的 countWordsAndLines 方法
};

最后,我们使用C++20的 std::span 特性创建一个简单的文本编辑器功能扩展系统。

int main() {
    constexpr auto useWordCounter = true;
    constexpr auto useLineCounter = false;
    std::unique_ptr<AbstractEditor> editor = std::make_unique<BasicEditor>();
    // C++17特性:if constexpr
    if constexpr (useWordCounter) {
        editor = std::make_unique<WordCounter>(std::move(editor));
    }
    if constexpr (useLineCounter) {
        editor = std::make_unique<LineCounter>(std::move(editor));
    }
    // C++20特性:std::span
    std::vector<std::string> lines = {"Hello, World!", "This is a sample text"};
    std::span<std::string> text(lines);
    std::cout << "Processing text:" << std::endl;
    for (const auto &line : text) {
        std::cout << editor->processText(line) << std::endl;
    }
    return 0;
}

在这个案例中,我们使用了C++14、C++17和C++20的特性来创建一个具有功能扩展的文本编辑器。这些功能扩展可以通过使用装饰器模式来添加或移除,而无需修改编辑器类的实现。通过使用if constexprstd::span特性,我们可以在编译时决定是否使用某个功能扩展,并在运行时处理一系列的文本数据。

装饰器模式的优缺点(Pros and Cons of Decorator Pattern)

优点(Pros)

  1. 更灵活的功能扩展:装饰器模式可以在不修改原有类的基础上为对象添加新功能。这种模式通过继承和组合实现,可以在运行时动态地修改被装饰对象的行为。这对于需要对现有类进行功能扩展或修改的场景非常有帮助,同时减少了对原有代码的侵入性。
  2. 遵循单一职责原则:装饰器模式可以将具体的功能扩展分散到不同的装饰器类中,使得每个装饰器类只关注单一的功能。这样做有利于提高代码的可读性和可维护性,同时遵循了单一职责原则。
  3. 可以组合多种功能:使用装饰器模式,可以将多个装饰器按需叠加使用,从而实现更丰富的功能组合。这种方式不仅能保持各个装饰器类的简单性,还可以提高代码的复用性。
  4. 易于扩展和修改:装饰器模式的结构使得它易于扩展和修改。当需要添加新的功能时,我们可以简单地创建一个新的装饰器类,而无需修改现有的代码。这有助于降低软件维护的难度和成本。
  5. 保持接口的一致性:装饰器模式使得装饰器类和被装饰类可以共享相同的接口。这意味着在使用装饰器的情况下,我们无需修改客户端代码,因为客户端只关心接口,而不关心具体的实现。

总之,在C++中使用装饰器模式可以带来许多优点,如灵活的功能扩展、遵循单一职责原则、易于扩展和修改等。然而,也需要注意到使用装饰器模式可能会导致系统中出现许多小的装饰器类,增加系统的复杂性。因此,我们应该根据实际需求和场景来权衡装饰器模式的使用。

缺点(Cons)

虽然装饰器模式在C++中具有许多优点,但它也存在一些缺点。以下是在C++中使用装饰器模式的一些潜在问题:

  1. 系统复杂性增加:使用装饰器模式会导致系统中出现许多小的装饰器类。这些类之间的关系可能会变得复杂,增加了系统的复杂性。这可能会让开发者和维护者在理解和修改代码时感到困难。
  2. 调试和维护困难:当多个装饰器叠加使用时,调试和维护可能会变得困难。这是因为在运行时,被装饰对象的行为可能会经过多个装饰器类。要理解这些行为,开发者需要追踪多个类的执行过程,这可能会消耗大量的时间和精力。
  3. 性能影响:装饰器模式中的对象层次较深,当叠加多个装饰器时,可能会对性能产生一定的影响。这是因为每个装饰器都会调用它所装饰的对象的方法,从而产生额外的开销。在性能要求较高的场景下,这种开销可能是不可忽视的。
  4. 代码可读性降低:虽然装饰器模式可以遵循单一职责原则,将功能拆分到不同的装饰器类中,但是过多的装饰器类可能会导致代码可读性降低。当需要理解一个被多个装饰器修饰的对象的行为时,开发者需要查看多个类的实现,这可能会让人感到困惑。

总之,在C++中使用装饰器模式时,需要权衡其优缺点。在某些场景下,装饰器模式可以带来很大的便利,但在另一些场景中,它可能会导致代码变得复杂且难以维护。因此,在实际项目中,我们应根据具体需求和场景来选择是否使用装饰器模式。

总结(Conclusion)

在本篇博客中,我们探讨了C++中装饰器模式的概念、优点和缺点。从心理学的角度来看,这种设计模式提供了一种灵活的方法来扩展现有类的功能,同时遵循了单一职责原则,有助于保持代码的可读性和可维护性。然而,我们也应当意识到装饰器模式可能会导致系统复杂性增加,调试和维护困难,以及性能影响等问题。

在实际应用中,我们应该权衡装饰器模式的优缺点,根据具体需求和场景来决定是否使用这种设计模式。从心理学角度来看,人类在面对复杂问题时往往会寻求简化和抽象的方法。装饰器模式正是一种通过将功能拆分到不同的装饰器类中,达到简化和抽象的目的。当我们使用装饰器模式时,应该关注以下几点:

  1. 良好的设计原则:尽量遵循单一职责原则,将不同的功能拆分到不同的装饰器类中。这有助于提高代码的可读性和可维护性。
  2. 保持简洁:避免过度使用装饰器模式,以免导致系统过于复杂。在实际项目中,我们需要找到一个平衡点,确保代码既具有足够的灵活性,又不会过于复杂。
  3. 明确的需求和场景:在决定使用装饰器模式之前,确保它适用于当前的需求和场景。在某些情况下,其他设计模式可能会更适合解决问题。
  4. 注意性能影响:在性能要求较高的场景下,使用装饰器模式可能会导致性能问题。在这种情况下,我们需要评估是否有其他更高效的方法来实现功能扩展。

总之,装饰器模式是一种在C++中实现功能扩展的有效方法。从心理学角度来看,它可以帮助我们简化和抽象问题,提高代码的可读性和可维护性。然而,在使用装饰器模式时,我们也需要关注其潜在的缺点和挑战。通过对这些优缺点进行权衡,我们可以更好地利用装饰器模式,为我们的代码带来更多的价值。

目录
相关文章
|
2月前
|
设计模式 程序员 C++
【C++ 泛型编程 高级篇】C++模板元编程:使用模板特化 灵活提取嵌套类型与多容器兼容性
【C++ 泛型编程 高级篇】C++模板元编程:使用模板特化 灵活提取嵌套类型与多容器兼容性
261 2
|
2月前
|
算法 C语言 C++
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)(一)
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)
141 1
|
2月前
|
算法 数据处理 C++
【C++ 20 新特性 算法和迭代器库的扩展和泛化 Ranges】深入浅出C++ Ranges库 (Exploring the C++ Ranges Library)
【C++ 20 新特性 算法和迭代器库的扩展和泛化 Ranges】深入浅出C++ Ranges库 (Exploring the C++ Ranges Library)
105 1
|
2月前
|
缓存 算法 安全
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)(三)
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)
42 1
|
2月前
|
安全 调度 C++
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)(二)
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)
87 0
|
2月前
|
算法 程序员 C语言
C++设计哲学:构建高效和灵活代码的艺术
C++设计哲学:构建高效和灵活代码的艺术
61 1
|
2月前
|
存储 安全 C语言
C++中灵活 处理可变参数的多种方式:优缺点、应用场景及示例对比
C++中灵活 处理可变参数的多种方式:优缺点、应用场景及示例对比
75 1
|
2月前
|
算法 Java C++
【C/C++ 内存知识扩展】内存不足的可能性分析
【C/C++ 内存知识扩展】内存不足的可能性分析
12 0
|
2月前
|
算法 程序员 C语言
【C++ 模板和迭代器的交融】灵活多变:使用C++模板精准打造自定义迭代器
【C++ 模板和迭代器的交融】灵活多变:使用C++模板精准打造自定义迭代器
48 0
|
搜索推荐 算法 大数据
【C++ 标准库排序算法】C++标准库中的排序算法深入解析:功能、原理与应用
【C++ 标准库排序算法】C++标准库中的排序算法深入解析:功能、原理与应用
49 0