0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VsCodeスニペット登録ショートカット

Last updated at Posted at 2023-02-14

はじめに

VsCodeでのスニペット登録時に、"(ダブルクォーテーション)でコードを囲む作業を自動化。そのメモ。競プロ用。

ソースコード(c++)

#include <iostream>
#include <string>
using namespace std;
// pf(テキスト)を入力してスニペット呼び出し
const string key = "hoge", pf = "pf", des = "des";

int main(void) {
    cout << '\"' << key << "\": {\n";
    cout << "\"prefix\" : \"" << pf << "\",\n";
    cout << "\"body\": [\n";
    string s;
    while (getline(cin, s)) {
        cout << '"' << s << "\",\n";
    }
    cout << "],\n";
    cout << "\"description\": \"" << des << "\"\n},\n";
    return 0;
}

使用例

ソースコード(変更箇所)

const string key = "Eratosthenes", pf = "eratos", des = "Sieve of Eratosthenes";

入力

void ET(const int n, vector<int>& prime) {

    prime.resize(n + 1, 1);
    prime[0] = prime[1] = 0;

    for (int p = 2; p <= n; ++p) {
        if (!prime[p])
            continue;

        for (int q = p * 2; q <= n; q += p) {
            prime[q] = 0;
        }
    }
}

出力

"Eratosthenes": {
"prefix" : "eratos",
"body": [
"void ET(const int n, vector<int>& prime) {",
"",
"    prime.resize(n + 1, 1);",
"    prime[0] = prime[1] = 0;",
"",
"    for (int p = 2; p <= n; ++p) {",
"        if (!prime[p])",
"            continue;",
"",
"        for (int q = p * 2; q <= n; q += p) {",
"            prime[q] = 0;",
"        }",
"    }",
"}",
],
"description": "Sieve of Eratosthenes"
},

注意点

keyで初期化する文字列は他スニペットとの重複不可(既に登録済みのスニペットに使用している文字列は使えない)

おわりに

もっと楽な方法があれば教えて!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?