Posts

Strategy Pattern

  Comparison of Modern C++ Approaches Implementation Type Mechanism Resolution Time Memory Overhead Use Case Runtime (Functional) std::function & Lambdas Runtime Small heap allocation (if lambda captures exceed small-buffer optimization) When algorithms must change on the fly during program execution. Compile-time (Static) Templates & Concepts ( requires ) Compile-time Zero runtime overhead (inlines perfectly) High-performance systems where strategies are known at compile time. Example 1: #include <bits/stdc++.h> class SortingStrategy { public: virtual void sort(std::vector<int>& arr) = 0; }; class BubbleSort : public SortingStrategy { public: void sort(std::vector<int>& arr) override { // Implement Bubble Sort algorithm } }; class QuickSort : public SortingStrategy { public: void sort(std::vector<int>& arr) override { // Implement Quick Sort algorithm } }; // Add more sorting algorithms as ne...

Flyweight Pattern

The Flyweight design pattern is a structural pattern designed to minimize memory usage by sharing as much data as possible with other similar objects. It divides an object's data into two categories: intrinsic state(heavy, constant, and shareable data) and extrinsic state (lightweight, unique, context-specific data) #include <iostream> #include <string> #include <string_view> #include <unordered_map> #include <vector> #include <memory> #include <shared_mutex> // 1. INTRINSIC STATE (The Flyweight) // This class contains heavy, immutable data shared across thousands of instances. class TreeModel { public: TreeModel(std::string name, std::string color, std::vector texture_data) : name_(std::move(name)), color_(std::move(color)), texture_bytes_(std::move(texture_data)) { std::cout texture_bytes_; // Simulates heavy mesh/texture data }; // 2. FLYWEIGHT FACTORY // Manages the shared pool of flyweights. It ensures obje...

Design Patterns

Creational: Factory Method (class) Abstract Factory (object) Builder  (object) Prototype  (object) Singleton  (object) Structral: Adapter (class/object) Bridge (object) Composite (object) Decorator  (object) Facade  (object) Flyweight   (object) Proxy  (object) Behavioral: Interpreter (class) Template Method (class) Chain of Responsibility (object) Command (object) Iterator (object) Mediator (object) Memento (object) Observer (object) State (object) Strategy (object) Visitor (object)

Adding Chinese watermarks to images using ffmpeg on Windows 10

Code: ffmpeg -i i1.jpg -vf "drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=168:fontcolor=white@0.5:x=(w-text_w)/2:y=(h-text_h)/2" output.jpg Multi Line Version: ffmpeg -i i1.jpg -vf "drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.1:y=h*0.2,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.4:y=h*0.2,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.7:y=h*0.2,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.1:y=h*0.5,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.4:y=h*0.5,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x...

Using spdlog in a C++ project

Image
Tree structure of the project. The code of main.cc. #include <iostream> #include "spdlog/spdlog.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h" void do_work_in_another_file(); void rotating_example() { try { // Create a file rotating logger with 5 MB size max and 3 rotated files auto max_size = 1048576 * 5; auto max_files = 3; // Set output time pattern // %f: Time accurate to microseconds // %n: Output mode "some_logger_name" // %l: Prints the severity level of the log message. Example include info, warn, error, debug or critical. // %v: Prints the actual text or payload of the log message that you passed to the function. spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%n] [%l] %v"); // Set it as default so you can spdlog::info() directly. auto logger = spdlog::rotating_logger_mt("som...

vim的智能配置

" 显示行号 set number " 将Tab键自动转换为空格 set expandtab " 设置Tab键本身的宽度为4个空格 set tabstop=4 " 设置按退格键(BackSpace)时一次删除4个空格 set softtabstop=4 " 设置使用>或<进行缩进时的宽度为4个空格 set shiftwidth=4 " 开启自动缩进(新行继承上一行的缩进) set autoindent " 开启C/C++语言智能缩进 set cindent " 搜索优化 set incsearch set ignorecase " 语法高亮 syntax on " 开启256色支持(在终端使用时非常重要) set t_Co=256

Installing vim via msys2 and config vim on windows10

1. Installing vim Open msys2 ucrt64 terminal and enter command as follows: ``` pacman -S vim ``` 2. Configure vim Open msys2 ucrt64 terminal and enter command as follows: ``` cd ~ touch .vimrc ``` Adding the content into .vimrc which you want to. For example: ``` set number ```