Pular para o conteúdo
Snippets·Grátis

Snippets de C e C++

14 snippets de C++ (for por intervalo, lambdas, unique_ptr, optional, structured bindings, threads, chrono) e 6 de C (include guard, malloc com verificação, loop com fgets, nó de lista ligada). Os de C aparecem em arquivos .c e .h.

Instalação grátis.

Se o PocketCode estiver instalado no seu Android, isto abre a página no app.

Prévia

5 de 20

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_};
};

Mudanças na 1.0.0

Primeira versão.

Relacionados