Skip to content

Instantly share code, notes, and snippets.

@abiiranathan
Created June 8, 2024 19:49
Show Gist options
  • Save abiiranathan/1fc92115efc4e0f832789b24573c28ab to your computer and use it in GitHub Desktop.
Save abiiranathan/1fc92115efc4e0f832789b24573c28ab to your computer and use it in GitHub Desktop.
C/C++ vscode snippets
{
"main": {
"scope": "c,c++",
"prefix": "main",
"body": [
"#include <stdio.h>",
"",
"int main(int argc, char*argv[]) {",
"\t$0",
"\treturn 0;",
"}"
],
"description": "Main function"
},
"Exit success": {
"scope": "c,c++",
"prefix": "exits",
"body": [
"exit(EXIT_SUCCESS);"
],
"description": "Exit success"
},
"Exit failure": {
"scope": "c,c++",
"prefix": "exitf",
"body": [
"exit(EXIT_FAILURE);"
],
"description": "Exit failure"
},
"malloc": {
"scope": "c,c++",
"prefix": "mal",
"body": [
"void *${1:ptr} = (${2:type}*)malloc(sizeof(${2:type}));",
"if(${1:ptr} == NULL){",
"\tfprintf(stderr, \"Memory allocation failed\\n\");",
"\t${0:return NULL;}",
"}"
],
"description": "malloc code snippet"
},
"calloc": {
"scope": "c,c++",
"prefix": "cal",
"body": [
"void *${1:ptr} = (${2:type}*)calloc(${3:size}, sizeof(${2:type}));",
"if(${1:ptr} == NULL){",
"\tfprintf(stderr, \"Memory allocation failed\\n\");",
"\t${0:return NULL;}",
"}"
],
"description": "calloc code snippet"
},
"memset": {
"scope": "c,c++",
"prefix": "memset",
"body": [
"memset(${1:ptr}, ${2:0}, ${3:size});"
],
"description": "memset code snippet"
},
"memcpy": {
"scope": "c,c++",
"prefix": "memcpy",
"body": [
"memcpy(${1:dest}, ${2:src}, ${3:size});"
],
"description": "memcpy code snippet"
},
"memmove": {
"scope": "c,c++",
"prefix": "memmove",
"body": [
"memmove(${1:dest}, ${2:src}, ${3:size});"
],
"description": "memmove code snippet"
},
"free": {
"scope": "c,c++",
"prefix": "free",
"body": [
"free(${1:ptr});"
],
"description": "free code snippet"
},
"realloc": {
"scope": "c,c++",
"prefix": "realloc",
"body": [
"void *${1:ptr} = realloc(${1:ptr}, ${2:size});",
"if(${1:ptr} == NULL){",
"\tfprintf(stderr, \"Memory allocation failed\\n\");",
"\t${0:return NULL;}",
"}"
],
"description": "realloc code snippet"
},
"System include": {
"scope": "c,c++",
"prefix": "inc",
"body": [
"#include <$1>"
],
"description": "Include a system header"
},
"Local include": {
"scope": "c,c++",
"prefix": "incl",
"body": [
"#include \"$1\""
],
"description": "Include a local header"
},
"Print": {
"scope": "c,c++",
"prefix": "pr",
"body": [
"printf(\"$1\\n\", $2);"
],
"description": "Print to stdout"
},
"Print error": {
"scope": "c,c++",
"prefix": "pre",
"body": [
"fprintf(stderr, \"$1\\n\", $2);"
],
"description": "Print to stderr"
},
"Scanf": {
"scope": "c,c++",
"prefix": "sc",
"body": [
"scanf(\"$1\", $2);"
],
"description": "Scanf"
},
"Return": {
"scope": "c,c++",
"prefix": "ret",
"body": [
"return $1;"
],
"description": "Return"
},
"Declare function": {
"scope": "c,c++",
"prefix": "func",
"body": [
"$1 $2($3) {",
"\t$0",
"}"
],
"description": "Declare a function"
},
"For loop": {
"scope": "c,c++",
"prefix": "for",
"body": [
"for (int ${1:i} = 0; ${1:i} < ${2:N}; ${1:i}++) {",
"\t$0",
"}"
],
"description": "For loop"
},
"Do-while loop": {
"scope": "c,c++",
"prefix": "dowhile",
"body": [
"do {",
"\t$0",
"} while (${1:condition});"
],
"description": "Do-while loop"
},
"While loop": {
"scope": "c,c++",
"prefix": "while",
"body": [
"while (${1:condition}) {",
"\t$0",
"}"
],
"description": "While loop"
},
"If statement": {
"scope": "c,c++",
"prefix": "if",
"body": [
"if (${1:condition}) {",
"\t$0",
"}"
],
"description": "If statement"
},
"If-else statement": {
"scope": "c,c++",
"prefix": "ifelse",
"body": [
"if (${1:condition}) {",
"\t$2",
"} else {",
"\t$0",
"}"
],
"description": "If-else statement"
},
"Else if statement": {
"scope": "c,c++",
"prefix": "elseif",
"body": [
"else if (${1:condition}) {",
"\t$0",
"}"
],
"description": "Else if statement"
},
"Switch statement": {
"scope": "c,c++",
"prefix": "switch",
"body": [
"switch (${1:expression}) {",
"\tcase ${2:case1}:{",
"\t\t$3",
"\t\t}break;",
"\tcase ${4:case2}:{",
"\t\t$5",
"\t\t}break;",
"\tdefault:",
"\t\t$0",
"}"
],
"description": "Switch statement"
},
"Struct declaration": {
"scope": "c,c++",
"prefix": "struct",
"body": [
"struct ${1:StructName} {",
"\t$2",
"};"
],
"description": "Struct declaration"
},
"Typedef struct": {
"scope": "c,c++",
"prefix": "typedef",
"body": [
"typedef struct ${1:StructName}{",
"\t$2",
"} ${1:StructName};"
],
"description": "Typedef struct"
},
"For each loop": {
"scope": "c++",
"prefix": "foreach",
"body": [
"for (auto &${1:element} : ${2:container}) {",
"\t$0",
"}"
],
"description": "For each loop in C++"
},
"Class declaration": {
"scope": "c++",
"prefix": "class",
"body": [
"class ${1:ClassName} {",
"public:",
"\t${1:ClassName}();",
"\t~${1:ClassName}();",
"\t$0",
"private:",
"\t$2",
"};"
],
"description": "Class declaration in C++"
},
"Constructor": {
"scope": "c++",
"prefix": "ctor",
"body": [
"${1:ClassName}::${1:ClassName}() {",
"\t$0",
"}"
],
"description": "Constructor in C++"
},
"Destructor": {
"scope": "c++",
"prefix": "dtor",
"body": [
"${1:ClassName}::~${1:ClassName}() {",
"\t$0",
"}"
],
"description": "Destructor in C++"
},
"Namespace": {
"scope": "c++",
"prefix": "ns",
"body": [
"namespace ${1:NamespaceName} {",
"\t$0",
"}"
],
"description": "Namespace in C++"
},
"Lambda": {
"scope": "c++",
"prefix": "lambda",
"body": [
"auto ${1:lambda_name} = [](${2:params}) -> ${3:return_type} {",
"\t$0",
"};"
],
"description": "Lambda in C++"
},
"Namespace usage": {
"scope": "c++",
"prefix": "usingns",
"body": [
"using namespace ${1:NamespaceName};"
],
"description": "Using namespace"
},
"Vector declaration": {
"scope": "c++",
"prefix": "vector",
"body": [
"#include <vector>",
"std::vector<${1:type}> ${2:vector_name};"
],
"description": "Vector declaration"
},
"Map declaration": {
"scope": "c++",
"prefix": "map",
"body": [
"#include <map>",
"std::map<${1:key_type}, ${2:value_type}> ${3:map_name};"
],
"description": "Map declaration"
},
"Set declaration": {
"scope": "c++",
"prefix": "set",
"body": [
"#include <set>",
"std::set<${1:type}> ${2:set_name};"
],
"description": "Set declaration"
},
"Pair declaration": {
"scope": "c++",
"prefix": "pair",
"body": [
"#include <utility>",
"std::pair<${1:type1}, ${2:type2}> ${3:pair_name};"
],
"description": "Pair declaration"
},
"Lambda with capture": {
"scope": "c++",
"prefix": "lambda_capture",
"body": [
"auto ${1:lambda_name} = [${2:captures}](${3:params}) -> ${4:return_type} {",
"\t$0",
"};"
],
"description": "Lambda with capture in C++"
},
"Try-catch block": {
"scope": "c++",
"prefix": "trycatch",
"body": [
"try {",
"\t$0",
"} catch (${1:ExceptionType} &e) {",
"\t${2:handler}",
"}"
],
"description": "Try-catch block in C++"
},
"Unique pointer": {
"scope": "c++",
"prefix": "unique_ptr",
"body": [
"#include <memory>",
"std::unique_ptr<${1:type}> ${2:ptr_name} = std::make_unique<${1:type}>(${3:args});"
],
"description": "Unique pointer"
},
"Shared pointer": {
"scope": "c++",
"prefix": "shared_ptr",
"body": [
"#include <memory>",
"std::shared_ptr<${1:type}> ${2:ptr_name} = std::make_shared<${1:type}>(${3:args});"
],
"description": "Shared pointer"
},
"Weak pointer": {
"scope": "c++",
"prefix": "weak_ptr",
"body": [
"#include <memory>",
"std::weak_ptr<${1:type}> ${2:ptr_name} = ${3:shared_ptr};"
],
"description": "Weak pointer"
},
"Override function": {
"scope": "c++",
"prefix": "override",
"body": [
"virtual ${1:return_type} ${2:function_name}(${3:params}) override {",
"\t$0",
"}"
],
"description": "Override function in C++"
},
"Virtual function": {
"scope": "c++",
"prefix": "virtual",
"body": [
"virtual ${1:return_type} ${2:function_name}(${3:params}) {",
"\t$0",
"}"
],
"description": "Virtual function in C++"
},
"Pure virtual function": {
"scope": "c++",
"prefix": "pure_virtual",
"body": [
"virtual ${1:return_type} ${2:function_name}(${3:params}) = 0;"
],
"description": "Pure virtual function in C++"
},
// ostream operator
"Ostream operator": {
"scope": "c++",
"prefix": "ostream",
"body": [
"friend std::ostream &operator<<(std::ostream &os, const ${1:ClassName} &obj) {",
"\tos << $0",
"\treturn os;",
"}"
],
"description": "Ostream operator"
},
// istream operator
"Istream operator": {
"scope": "c++",
"prefix": "istream",
"body": [
"friend std::istream &operator>>(std::istream &is, ${1:ClassName} &obj) {",
"\tis >> $0",
"\treturn is;",
"}"
],
"description": "Istream operator"
},
// copy constructor
"Copy constructor": {
"scope": "c++",
"prefix": "copy",
"body": [
"${1:ClassName}(const ${1:ClassName} &obj) {",
"\t$0",
"}"
],
"description": "Copy constructor"
},
// move constructor
"Move constructor": {
"scope": "c++",
"prefix": "move",
"body": [
"${1:ClassName}(${1:ClassName} &&obj) noexcept {",
"\t$0",
"}"
],
"description": "Move constructor"
},
// copy assignment operator
"Copy assignment operator": {
"scope": "c++",
"prefix": "copy_assign",
"body": [
"${1:ClassName} &operator=(const ${1:ClassName} &obj) {",
"\tif (this != &obj) {",
"\t\t$0",
"\t}",
"\treturn *this;",
"}"
],
"description": "Copy assignment operator"
},
// move assignment operator
"Move assignment operator": {
"scope": "c++",
"prefix": "move_assign",
"body": [
"${1:ClassName} &operator=(${1:ClassName} &&obj) noexcept {",
"\tif (this != &obj) {",
"\t\t$0",
"\t}",
"\treturn *this;",
"}"
],
"description": "Move assignment operator"
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment