代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

Java文件读写 Java

NIO文件操作

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileIODemo {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("file.txt");
        
        // Write to file
        String content = "Hello, NIO File API!";
        Files.write(path, content.getBytes(StandardCharsets.UTF8));
        
        // Read from file
        byte[] readBytes = Files.readAllBytes(path);
        System.out.println(new String(readBytes, StandardCharsets.UTF8));
    }
}
C++线程 C++

std::thread使用

#include <iostream>
#include <thread>
#include <vector>
using namespace std;

void thread_function(int id) {
    cout << "Thread " << id << " is running\n";
}

int main() {
    vector<thread> threads;
    
    for (int i = 0; i < 5; ++i) {
        threads.push_back(thread(thread_function, i));
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    cout << "All threads normally terminated\n";
    return 0;
}
TS类 TypeScript

TypeScript类示例

class Person {
  private name: string;
  age: number;
  
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
  
  describe(): string {
    return `${this.name} is ${this.age} years old`;
  }
}

const alice = new Person('Alice', 30);
console.log(alice.describe());
Java枚举 Java

枚举类型示例

public enum DayOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
    
    public String getDisplayName() {
        switch (this) {
            case MONDAY: return "Monday";
            case SATURDAY: return "Weekend";
            default: return "Unknown";
        }
    }
}
C++向量 C++

std::vector操作

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> numbers = {2, 4, 6, 8, 10};
    
    // Add element
    numbers.push_back(12);
    
    // Sort vector
    sort(numbers.begin(), numbers.end());
    
    // Iterate over vector
    for (int n : numbers) {
        cout << n << " ";
    }
    return 0;
}
SQL视图 SQL

创建用户视图

CREATE VIEW user_summary AS
SELECT
  u.id,
  u.username,
  COUNT(o.id) AS order_count,
  SUM(o.total) AS total_spent
FROM users AS u
JOIN orders AS o ON u.id = o.user_id
GROUP BY u.id;

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类