收集、管理和分享有用的代码片段,提高开发效率
读写文本文件内容
#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;
}
使用ROW_NUMBER分区排序
SELECT
customer_id,
order_date,
RANK() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS order_rank
FROM orders;
使用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}"
结构化克隆深拷贝对象
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;
}
无限旋转动画效果
<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); }
}
使用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();
}
}
}
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类