代码片段仓库

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

代码片段

C++文件操作 C++

读写文本文件内容

#include <fstream>
#include <iostream>

using namespace std;
int main() {
    // Write to file
    ofstream outFile("output.txt");
    if (outFile.is_open()) {
        outFile << "Hello, World!" << endl;
        outFile.close();
    }
    
    // Read from file
    ifstream inFile("output.txt");
    string line;
    if (inFile.is_open()) {
        while (getline(inFile, line)) {
            cout << line << endl;
        }
        inFile.close();
    }
    return 0;
}
SQL窗口函数 SQL

使用ROW_NUMBER分区排序

SELECT
  customer_id,
  order_date,
  RANK() OVER (

    PARTITION BY customer_id
    ORDER BY order_date DESC
  ) AS order_rank
FROM orders;
Python网页请求 Python

使用requests获取网页内容

import requests

def fetch_web_page(url):
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()  # Raise error for bad status
        return response.text
    except requests.exceptions.RequestException as e:
        return f"Error: {e}"
JS深拷贝 JavaScript

结构化克隆深拷贝对象

const deepClone = (obj) => {
  if (typeof obj !== 'object' || obj === null) return obj;
  
  let cloned;
  if (Array.isArray(obj)) {
    cloned = [];
    for (let i = 0; i < obj.length; i++) {
      cloned[i] = deepClone(obj[i]);
    }
  } else {
    cloned = {};
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        cloned[key] = deepClone(obj[key]);
      }
    }
  }
  return cloned;
}
CSS旋转动画 HTML

无限旋转动画效果

<div class="spinner"></div>

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-top: 4px solid #000;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
Java JDBC连接 Java

使用JDBC连接MySQL

public class DbConnector {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydb";
        String user = "username";
        String password = "password";
        
        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            System.out.println("Connection succeeded!");
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

为什么选择CodeSnippets?

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

智能搜索

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

语法高亮

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

多设备同步

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

热门分类

浏览最受欢迎的代码分类