Understanding Snippet Triggers
Every snippet in the library is triggered by typing its filename (without the.cpp extension) followed by your editor’s autocomplete trigger.
Naming Convention
Snippets follow a consistent naming pattern:graph_dijkstra_std- Standard Dijkstra algorithm from the graph categorystring_kmp_std- Standard KMP pattern matching from string categoryrange_query_segment_tree_lazy_compress- Compressed lazy segment tree from range query category
VS Code Usage
Triggering Snippets
Select from Autocomplete
VS Code will show matching snippets in the autocomplete menu. Use arrow keys to select and press
Enter or Tab.IntelliSense Configuration
For optimal snippet experience, configure yoursettings.json:
settings.json
"editor.snippetSuggestions": "top" ensures snippets appear at the top of autocomplete suggestions.Keyboard Shortcuts
| Action | Windows/Linux | macOS |
|---|---|---|
| Trigger autocomplete | Ctrl+Space | Cmd+Space |
| Next placeholder | Tab | Tab |
| Previous placeholder | Shift+Tab | Shift+Tab |
| Accept suggestion | Enter or Tab | Enter or Tab |
| Dismiss suggestions | Esc | Esc |
Custom Keybindings
Add to yourkeybindings.json for even faster workflow:
keybindings.json
Vim/Neovim Usage
UltiSnips Configuration
Add to your.vimrc or init.vim:
Triggering Snippets
Snippet Listing
To see all available snippets while editing:Ctrl+l with the above configuration).
Advanced Vim Configuration
For completion menu integration with coc.nvim:Snippet Categories and Usage Patterns
Template Snippets
Always use a template as your starting point:Graph Snippets
Graph algorithms require the base graph structure:Data Structure Snippets
Most data structures are self-contained:Utility Snippets
Add utilities anywhere in your code:Snippet Dependencies
Common Dependencies
Some snippets require others to work:Graph Algorithms
Graph Algorithms
Required:
graph_graph(base class)graph_digraphORgraph_undigraph
graph_dijkstra_stdgraph_bellman_fordgraph_floyd_warshallgraph_kruskal- Any other graph algorithm
Segment Tree with Lazy Propagation
Segment Tree with Lazy Propagation
Self-contained:
range_query_segment_tree_lazy_fullrange_query_segment_tree_lazy_compress
String Algorithms
String Algorithms
Self-contained:
- All string snippets are independent
string_kmp_stdstring_rabin_karp_stdstring_z_algorithm_std
Modular Arithmetic
Modular Arithmetic
Use one of:
numeric_mint_full(comprehensive)numeric_mint_medium(balanced)numeric_mint_compress(minimal)
combinatorics_ncr_compress(binomial coefficients)math_factorial
Multi-Snippet Workflow
Building Complex Solutions
For problems requiring multiple algorithms:Example: Complex Problem
Solving a problem with graphs, modular arithmetic, and combinatorics:Snippet Customization
Modifying Generated Snippets
VS Code
-
Open snippet file:
- Windows:
%APPDATA%\Code\User\snippets\cpp.json - Linux:
~/.config/Code/User/snippets/cpp.json - macOS:
~/Library/Application Support/Code/User/snippets/cpp.json
- Windows:
- Edit the snippet JSON:
Vim/Neovim
-
Open snippet file:
- Modify the snippet:
Creating Custom Snippets
To avoid overwriting, create a separate file:VS Code
Createcpp-custom.json in your snippets directory:
Vim/Neovim
Create~/.vim/UltiSnips/cpp/custom.snippets:
Performance Tips
Load Selectively
Only include snippets you need. Avoid loading entire categories unnecessarily.
Use Compressed Variants
When available, use
_compress variants:numeric_mint_compressstring_hashing_static_compress
Template First
Always start with a template. It includes essential optimizations:
- Fast I/O
- Compiler pragmas
- Common macros
Know Your Variants
Many algorithms have multiple variants:
_std- Standard implementation_full- Feature-complete_compress- Minimal version
Troubleshooting
Snippets Not Appearing
VS Code: Snippets don't show up
VS Code: Snippets don't show up
- Check file extension is
.cpp - Verify
cpp.jsonexists in snippets directory - Restart VS Code
- Check
settings.jsonhas snippets enabled - Try manual trigger:
Ctrl+Space
Vim: Tab doesn't expand snippet
Vim: Tab doesn't expand snippet
- Verify UltiSnips is installed:
:echo exists('g:UltiSnipsExpandTrigger') - Check trigger key:
:echo g:UltiSnipsExpandTrigger - Ensure snippet file exists:
:!ls ~/.vim/UltiSnips/cpp/ - Try listing snippets:
:UltiSnipsEdit
Compilation errors after using snippet
Compilation errors after using snippet
- Check if snippet requires dependencies (e.g.,
graph_graph) - Ensure correct order of snippets
- Verify C++ standard: compile with
-std=c++17 - Check for namespace conflicts
Next Steps
Competitive Programming
Learn best practices and optimization strategies
Templates Overview
Explore all available template options