版本比较
cpython 头文件(AI)
正文
增加 52 行删除 52 行
230 行未改动全部展开2312322332342352352362372382382392402412412422432442452462472482492492502512522532542552562572572582592602612 行未改动2642652662672682682692702712722 行未改动2752762772782792792802812822832842852852862872882892902902912922932942 行未改动29729829930030130130230330430530630730730830931031150 行未改动36236336436536636636736836937037137137237337437537637737837937938038138238338338438538638738838838939039139239339439539639739739839940040140240340440540540640740840941041141241341341441541641741841841942042142242342342442542642742842942943043143243343443543643743743843944044144244344344444544644744844945045145245345345445545645745845945946046146246346446546546646746846946947047147247347447447547647747847947948048148248348448448548648748848949049149149249349449549649749849950050050150250350450550550650750850950951051151251351351451551651751751851952052152252252352452552652652752852953053153153253353453453553653753853653753853954054154254354454554654754854854955055155255255355455555655755855956056156156256356456556656756856856957057157257357457457557657757857857958058158258258358458558658758758858959059113 行未改动230 行未改动全部展开// 命名空间封装所有Python模拟功能namespace py { // 类型别名 using p_str = std::string;
template<typename T> using p_list = std::vector<T>;
template<typename K, typename V> using p_dict = std::map<K, V>;
// 打印功能实现 template<typename... Args> void p_print(Args&&... args) { using expander = int[]; (void)expander{0, (void(std::cout << std::forward<Args>(args)), 0)...}; std::cout << std::endl; }
// 输入功能实现 p_str p_input(const p_str& prompt="") { std::cout << prompt; p_str result; std::getline(std::cin, result); return result; }
int p_input_int(const p_str& prompt="") { while (true) { p_str input = p_input(prompt); try {2 行未改动 p_print("WRONG NUMBER\n"); } } }
double p_input_float(const p_str& prompt="") { while (true) { p_str input = p_input(prompt); try {2 行未改动 p_print("WRONG NUMBER\n"); } } }
// 列表操作函数 template<typename T> void p_append(p_list<T>& lst, const T& value) { lst.push_back(value); }
template<typename T> size_t p_len(const p_list<T>& lst) { return lst.size(); }
template<typename T> p_list<T> p_slice(const p_list<T>& lst, int start, int end) { p_list<T> result; if (start < 0) start = 0;2 行未改动 result.push_back(lst[i]); } return result; }
// 字典操作函数 template<typename K, typename V> void p_setitem(p_dict<K, V>& d, const K& key, const V& value) { d[key] = value; }
template<typename K, typename V> V p_getitem(const p_dict<K, V>& d, const K& key) { return d.at(key); }50 行未改动 template <typename T> class list { private: std::vector<T> data;
public: // 构造函数 list() {} list(std::initializer_list<T> init) : data(init) {}
// 元素访问 T& operator[](size_t index) { if (index >= data.size()) { data.resize(index + 1); } return data[index]; }
const T& operator[](size_t index) const { return data[index]; }
// 添加元素 void append(const T& item) { data.push_back(item); }
// 插入元素 void insert(size_t index, const T& item) { if (index >= data.size()) { data.push_back(item); } else { data.insert(data.begin() + index, item); } }
// 删除元素 void remove(const T& item) { auto it = std::find(data.begin(), data.end(), item); if (it != data.end()) { data.erase(it); } }
// 弹出元素 T pop(size_t index = -1) { if (index >= data.size()) index = data.size() - 1; T item = data[index]; data.erase(data.begin() + index); return item; }
// 获取长度 size_t len() const { return data.size(); }
// 清空列表 void clear() { data.clear(); }
// 迭代器支持 auto begin() { return data.begin(); } auto end() { return data.end(); } auto begin() const { return data.begin(); } auto end() const { return data.end(); }
// 切片操作 list<T> slice(int start, int end) const { list<T> result; if (start < 0) start += data.size(); if (end < 0) end += data.size(); start = std::max(0, start); end = std::min(static_cast<int>(data.size()), end);
for (int i = start; i < end; ++i) { result.append(data[i]); } return result; }
// 列表连接 list<T> operator+(const list<T>& other) const { list<T> result = *this; for (const auto& item : other) { result.append(item); } return result; } };
// 模拟Python字典 template <typename K, typename V> class dict { private: std::map<K, V> data;
public: // 元素访问 V& operator[](const K& key) { return data[key]; }
const V& operator[](const K& key) const { return data.at(key); }
// 获取长度 size_t len() const { return data.size(); }
// 检查键是否存在 bool has_key(const K& key) const { return data.find(key) != data.end(); }
// 删除键值对 void del(const K& key) { data.erase(key); }
// 迭代器支持 auto begin() { return data.begin(); } auto end() { return data.end(); } auto begin() const { return data.begin(); } auto end() const { return data.end(); } };
// 模拟Python print函数 template <typename... Args> void print(Args&&... args) { std::ostringstream oss; int dummy[] = {0, (oss << std::forward<Args>(args), 0)...}; static_cast<void>(dummy); // 避免未使用变量警告 std::cout << oss.str() << std::endl; }
// 字符串拼接增强 std::string operator+(const std::string& a, int b) { return a + std::to_string(b); }
std::string operator+(int a, const std::string& b) { return std::to_string(a) + b; }
std::string operator+(const std::string& a, double b) { return a + std::to_string(b); }
std::string operator+(double a, const std::string& b) { return std::to_string(a) + b; }
// 范围循环辅助类,类似for x in range(n) class range { private: int start_, end_, step_;
public: range(int end) : start_(0), end_(end), step_(1) {} range(int start, int end, int step = 1) : start_(start), end_(end), step_(step) {}
class iterator { private: int current_; int step_;
public: iterator(int current, int step) : current_(current), step_(step) {}
int operator*() const { return current_; } iterator& operator++() { current_ += step_; return *this; iterator& operator++() { current_ += step_; return *this; } bool operator!=(const iterator& other) const { if (step_ > 0) { return current_ < other.current_; } else { return current_ > other.current_; } } };
iterator begin() const { return iterator(start_, step_); } iterator end() const { return iterator(end_, step_); } };
// 字符串处理函数 namespace str { // 转换为小写 std::string lower(const std::string& s) { std::string res = s; std::transform(res.begin(), res.end(), res.begin(), ::tolower); return res; }
// 转换为大写 std::string upper(const std::string& s) { std::string res = s; std::transform(res.begin(), res.end(), res.begin(), ::toupper); return res; }
// 分割字符串 list<std::string> split(const std::string& s, char delimiter) { list<std::string> res; std::stringstream ss(s); std::string part;
while (std::getline(ss, part, delimiter)) { res.append(part); }
return res; } }
// 数学函数 namespace math { constexpr double pi = M_PI; constexpr double e = M_E;
double abs(double x) { return std::abs(x); } double pow(double x, double y) { return std::pow(x, y); } double sqrt(double x) { return std::sqrt(x); } double sin(double x) { return std::sin(x); }13 行未改动