1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

update cpp snippets

This commit is contained in:
jdhao 2021-10-30 22:53:39 +08:00
parent 5bf969e327
commit 80893e7e4a

View File

@ -2,18 +2,30 @@ snippet bare "barebone code template"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <map>
#inlcude <unordered_map>
#include <set>
#inlucde <unordered_set>
#include <stack>
#include <queue>
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::vector; using std::vector;
using std::string; using std::string;
using std::map;
using std::unordered_map;
using std::set;
using std::unordered_set;
using std::stack;
using std::queue;
int main() int main()
{ {
return 0; return 0;
} }
endsnippet endsnippet
@ -25,25 +37,25 @@ endsnippet
snippet plist "print vector" w snippet plist "print vector" w
template <class T> template <class T>
void printList(const T& arr, const string& desc){ void printList(const T& arr, const string& desc){
std::cout << desc << ": ["; std::cout << desc << ": [";
for (auto it = arr.begin(); it != arr.end(); it++){ for (auto it = arr.begin(); it != arr.end(); it++){
std::cout << *it << ((std::next(it) != arr.end()) ? ", " : "]\n"); std::cout << *it << ((std::next(it) != arr.end()) ? ", " : "]\n");
} }
} }
endsnippet endsnippet
snippet pmat "print list of list" w snippet pmat "print list of list" w
template <class T> template <class T>
void printMat(const vector<vector<T>>& mat, const string& desc){ void printMat(const vector<vector<T>>& mat, const string& desc){
cout << desc << ": " << endl; cout << desc << ": " << endl;
for (auto it1 = mat.begin(); it1 != mat.end(); it1++){ for (auto it1 = mat.begin(); it1 != mat.end(); it1++){
auto cur_vec = *it1; auto cur_vec = *it1;
for (auto it2 = cur_vec.begin(); it2 != cur_vec.end(); it2++){ for (auto it2 = cur_vec.begin(); it2 != cur_vec.end(); it2++){
cout << *it2 << ((std::next(it2) != cur_vec.end()) ? ", " : "\n"); cout << *it2 << ((std::next(it2) != cur_vec.end()) ? ", " : "\n");
} }
} }
} }
endsnippet endsnippet
@ -55,16 +67,16 @@ snippet random "Generate a random list" b
// Generate a random sequence of length len, in range(low, high) (inclusive). // Generate a random sequence of length len, in range(low, high) (inclusive).
// need to #include<random> // need to #include<random>
vector<int> genRandom(int low, int high, int len){ vector<int> genRandom(int low, int high, int len){
std::random_device rd; std::random_device rd;
std::mt19937 gen(rd()); std::mt19937 gen(rd());
std::uniform_int_distribution<int> distribution(low, high); std::uniform_int_distribution<int> distribution(low, high);
vector<int> arr(len, 0); vector<int> arr(len, 0);
for (int i = 0; i != len; ++i){ for (int i = 0; i != len; ++i){
arr[i] = distribution(gen); arr[i] = distribution(gen);
} }
return arr; return arr;
} }
endsnippet endsnippet