跳转到内容
代码片段·免费

C 与 C++ 代码片段

14 个 C++ 代码片段(范围 for、lambda、unique_ptr、optional、结构化绑定、线程、chrono)和 6 个 C 代码片段(include guard、带检查的 malloc、fgets 循环、链表节点)。C 片段出现在 .c 和 .h 文件中。

免费安装。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

20 个中的 5 个

main with argumentsmainargs

Entry point that checks the argument count and prints usage

#include <iostream>

int main(int argc, char* argv[]) {
    if (argc < ${1:2}) {
        std::cerr << "usage: " << argv[0] << " ${2:<file>}\n";
        return 1;
    }
    $0
    return 0;
}

Range-based forforr

Loop over a container by const reference

for (const auto& ${1:item} : ${2:items}) {
    $0
}

Sort with a lambdasortl

std::sort with a custom comparison

std::sort(${1:v}.begin(), ${1:v}.end(), [](const auto& a, const auto& b) {
    return a.${2:value} < b.${2:value};
});

unique_ptruptr

Owning pointer created with make_unique

auto ${1:ptr} = std::make_unique<${2:Widget}>($3);

Class with a constructorctor

Class with an explicit constructor, a getter and a private member

class ${1:Widget} {
public:
    explicit ${1:Widget}(${2:int value}) : ${3:value_}(${4:value}) {}

    ${5:int} ${6:value}() const { return ${3:value_}; }

private:
    ${5:int} ${3:value_};
};

1.0.0 的变更

首个版本。

相关