C++ 标准库笔记

                     

贡献者: addis

1. string

   详见 “C++ 字符串笔记”。

2. vector

   更多容器详见 “C++ 标准库常用容器

3. ctime

4. algorithm

5. stdlib

6. std::variant

#include <variant>
using namespace std;

using Operand = variant<int, double>;

Operand performOperation(const Operand& left,
    const Operand& right, const string& op) {
    // Visitor that applies the operation
    return visit([&](auto&& arg1, auto&& arg2) -> Operand {
        if (op == "+") return arg1 + arg2;
        else if (op == "-") return arg1 - arg2;
        else if (op == "*") return arg1 * arg2;
        else if (op == "/") {
            if (arg2 == 0) {
                cout << "Cannot divide by zero!" << endl;
                return 0;
            }
            return arg1 / arg2;
        }
        else {
            cout << "Unsupported operation" << endl;
            return 0;
        }
    }, left, right);
}

7. std::any

                     

© 小时科技 保留一切权利