跳转至内容
  • A place to talk about whatever you want

    15 主题
    137 帖子
    HeiseCurtainH

    非常感谢!!!

  • 8 主题
    12 帖子
    SPeakS
    视频评论同步 - 类型类别和值类别探讨

    image.png

  • 开源软件 | 开源社区 | 开源理念 | 开源与商业 | 开源可持续发展 等相关话的交流讨论
    注: 这里的"开源"是泛化的共建共享概念, 范围包含 OSI的范围、自由软件、CC等相关内容

    39 主题
    165 帖子
    SPeakS
    0.概要

    本文主要介绍如下内容:

    1.cmake + ninja + gcc15(或其他支持import std的编译器)如何搭建一个支持import std(标准库模块化)特性的项目/工程模板 2.cmake不同版本CMAKE_EXPERIMENTAL_CXX_IMPORT_STD功能对应的ID怎么确定 3.如何使用xlings工具一键生成这样的项目模板并自动配置好环境 - 想快速体验的朋友可以直接看第3节 1.最小项目模板及构建 1.1 目录结构 ├── build - 构建目录(后期生成) ├── CMakeLists.txt - 工程描述文件 ├── config.xlings ├── README.md └── src └── main.cpp - 源码文件 3 directories, 4 files 1.2 HelloWorld代码 - src/main.cpp

    通过一行import std导入标准库, 然后不需要一个一个include就能使用标准库里的工具, 例如下面的println

    import std; int main() { std::println("Hello, C++23 modules!"); } 1.3 工程描述文件

    在cmake的描述文件project定义前启用import std特性, 并设置相关C++23标准及模块std, 其中cmake需要3.3及以上才支持CMAKE_EXPERIMENTAL_CXX_IMPORT_STD并不同版本对应ID可能不一样(后面会介绍确定方法)

    cmake_minimum_required(VERSION 4.0.2) # https://github.com/Kitware/CMake/blob/master/Help/dev/experimental.rst set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "a9e1cf81-9932-4810-974b-6eccaf14e457") set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_MODULE_STD 1) project(HelloWorld LANGUAGES CXX) add_executable(helloworld src/main.cpp) 1.4 构建&环境要求

    使用cmake生成ninja的构建文件, 然后使用ninja进行。这里需要注意的是当前不支持默认的GUN Make

    ninja cmake 4.0.2 gcc 15 或 支持import std特性的编译器(clang... mkdir build && cd build cmake -G Ninja .. ninja -v

    4fbab89d-39ce-431e-8e5a-990fc85f33ce-image.png

    注: 使用默认gun make会报错提示: Unsupported generator: Unix Makefiles

    2f9f701b-c79f-4fbe-b24d-e0da407bd4db-image.png

    2 CMAKE_EXPERIMENTAL_CXX_IMPORT_STD不同版本对应的ID

    应该不少朋友是卡在了这一步, 由于cmake这个功能还是实验性质的所以要满足以下要求:

    cmake版本大于等于3.30 要使用和自己版本匹配CMAKE_EXPERIMENTAL_CXX_IMPORT_STD的id号

    这个id号在cmake的github文档中: https://github.com/Kitware/CMake/blob/master/Help/dev/experimental.rst
    但由于默认文档是master分支可能id和你的版本是不匹配的, 所以查找时要切换tag到和自己版本一样的分支 -- 这是个坑点

    01cd4e86-ace4-4549-819e-8547ec843c11-image.png

    3 一键生成项目模板并配置好环境

    我给以上模板和环境添加到了xlings工具, 只要一行命令就能生成这个模板并配置好工具链依赖, 对应模板名为cpp23-cmake-helloworld, 完整命令和生成文件(README里包含构建说明)如下

    xlings new --template cpp23-cmake-helloworld

    ea918e38-e351-40e9-a4a5-05e05f76b241-image.png

    4 Other 原文 - d2learn论坛 xlings工具 - Github xlings - 项目模板cpp23-cmake-helloworld仓库 cmake文档1 - import std基础用法 cmake文档2 - CMAKE_CXX_COMPILER_IMPORT_STD版本要求 cmake文档3 - CMAKE_CXX_COMPILER_IMPORT_STD的id
  • d2learn开源组

    3 主题
    3 帖子
    没有新主题
  • 11 主题
    31 帖子
    妈耶厥了

    本系列博客作为我学习的一个笔记,注重于代码实现,本人非数学专业,证明能力非常弱,本文掺杂着大量我自己的理解,如有数学大佬光顾,请尽情指正,谢谢
    本系列默认大家都学会了C/C++基本语法

    集合 书上的定义 集合是指具有某种特定性质的不同对象无序聚集成的一个整体。 集合中的每一个对象称为集合的元素; 通常用大写字母表示集合; 用小写字母表示集合中的元素。

    也就是说把一些东西放到一起叫做集合(个人理解)

    代码实现 #include <string> #include <vector> #include <stdint.h> #include <iostream> // 抽象基类,定义集合元素的基本接口 class elmBase { public: // 将元素转换为字节数组的纯虚函数(序列化基础) virtual std::vector<uint8_t> getUint8Array() const = 0; // 默认比较运算符,通过字节数组逐字节比较 virtual int operator==(const elmBase &other) { std::vector<uint8_t> thisDate = getUint8Array(); std::vector<uint8_t> otherDate = other.getUint8Array(); if (thisDate.size() != otherDate.size()) return 0; for (int i = 0; i < thisDate.size(); i++) { if (thisDate[i] != otherDate[i]) return 0; } return 1; } // 转换为字符串表示的纯虚函数 virtual std::string toString() = 0; // 虚析构函数确保正确释放派生类对象 virtual ~elmBase() { } }; // 特殊元素类型,始终返回比较不成立 class alwaysFalse : public elmBase { public: std::vector<uint8_t> getUint8Array() const override { return std::vector<uint8_t>(); // 返回空数组 } std::string toString() override { return {}; // 返回空字符串 } // 重载运算符始终返回false,用于占位符场景 virtual int operator==(const elmBase &other) override { return 0; } ~alwaysFalse() { } }; // 哈希表节点结构,使用链表法解决冲突 using elmNode = struct elmNode { elmBase *elm; // 元素指针 elmNode *next; // 下一个节点指针 }; // 节点工厂函数,创建新节点并初始化元素 elmNode *elmNodeFactory(elmBase *elm) { elmNode *newNode = new elmNode(); newNode->elm = elm; newNode->next = nullptr; return newNode; } // 自定义集合类,基于哈希表实现 class set { private: uint32_t prime; // 哈希表大小(通常选择质数) elmNode **setMap; // 哈希桶数组 // 初始化哈希表结构 void inline _init_(uint32_t prime) { this->prime = prime; this->setMap = new elmNode *[prime]; // 初始化每个桶为alwaysFalse哨兵节点 for (uint32_t i = 0; i < prime; ++i) { this->setMap[i] = elmNodeFactory(new alwaysFalse()); } } protected: // 查找元素对应的哈希桶 elmNode **__find__(elmBase *elm) { uint32_t hash = rotatingHash(elm->getUint8Array(), this->prime); return &(this->setMap[hash]); // 返回桶的指针 } /** * 通用链表操作函数 * @param link 链表头指针的指针 * @param elm 目标元素 * @param cmp 自定义比较函数,返回true时停止遍历 * @param finally 遍历结束后的处理函数(插入等操作) * @return 找到的节点或操作结果节点 */ elmNode *__link__(elmNode **link, elmBase *elm, bool (*cmp)(elmNode *, elmBase *), elmNode *(*finally)(elmNode **, elmNode *, elmBase *) = nullptr) { elmNode *linkPriv = *link; elmNode *linkCurrent = linkPriv; // 遍历链表查找元素 while (linkCurrent != nullptr) { if (cmp(linkCurrent, elm)) { return linkCurrent; } linkPriv = linkCurrent; linkCurrent = linkCurrent->next; } // 执行最终处理函数(如插入新节点) if (finally != nullptr) { return finally(link, linkPriv, elm); } return nullptr; } public: // 构造函数初始化哈希表 set(uint32_t prime) { _init_(prime); } ~set() { for (uint32_t i = 0; i < this->prime; ++i) { elmNode *link = this->setMap[i]; while (link != nullptr) { elmNode *tmp = link; link = link->next; if (tmp != nullptr) { if (tmp->elm != nullptr) { std::cout << "delete " << tmp->elm->toString() << std::endl; delete tmp->elm; } delete tmp; } } } } // 旋转哈希算法实现 static uint32_t rotatingHash(std::vector<uint8_t> key, uint32_t prime) { uint32_t hash = key.size(); for (uint32_t i = 0; i < key.size(); ++i) hash = (hash << 4) ^ (hash >> 28) ^ (uint32_t)(key[i]); return (hash % prime); } // 添加元素到集合 bool add(elmBase *elm, bool force = false) { elmNode **node = __find__(elm); auto finally = [](elmNode **node, elmNode *linkPriv, elmBase *elm) -> elmNode * { // 插入新节点到链表末尾 if (linkPriv->next == nullptr) { linkPriv->next = elmNodeFactory(elm); } return nullptr; }; // 使用lambda作为比较函数 elmNode *targetNode = this->__link__(node, elm, [](elmNode *node, elmBase *tag) -> bool { return *(node->elm) == *tag; }, finally); return targetNode == nullptr; // 返回是否插入成功 } // 检查元素是否存在 bool get(elmBase *elm) { elmNode **node = __find__(elm); elmNode *targetNode = this->__link__(node, elm, [](elmNode *node, elmBase *tag) -> bool { return *(node->elm) == *tag; }); return targetNode != nullptr; } // 移除集合中的元素 bool remove(elmBase *elm) { elmNode **node = __find__(elm); elmNode *ret = this->__link__(node, elm, [](elmNode *current, elmBase *tag) -> bool { // 查找前驱节点执行删除 if (current->next != nullptr && *(current->next->elm) == *tag) { elmNode *tmp = current->next; current->next = current->next->next; delete tmp->elm; // 释放元素内存 delete tmp; // 释放节点内存 return true; } return false; }); return ret != nullptr; } // 调试用打印函数,显示哈希表结构 void print() { std::cout << "[HashTable Structure]" << std::endl; for (int i = 0; i < this->prime; i++) { std::cout << "Bucket " << i << ": "; elmNode *node = this->setMap[i]; while (node != nullptr) { std::cout << node->elm->toString() << " -> "; node = node->next; } std::cout << "NULL" << std::endl; } } }; // 数值类型元素实现 class number : public elmBase { int data; public: number(int data) : data(data) {} std::vector<uint8_t> getUint8Array() const override { // 将整型转换为字节数组 return std::vector<uint8_t>( reinterpret_cast<const uint8_t *>(&data), reinterpret_cast<const uint8_t *>(&data) + sizeof(data)); } std::string toString() override { return std::to_string(data); } ~number() { } }; // 字符串类型元素实现 class string : public elmBase { std::string data; public: string(std::string data) : data(data) {} std::vector<uint8_t> getUint8Array() const override { // 将字符串内容转换为字节数组 return std::vector<uint8_t>(data.begin(), data.end()); } std::string toString() override { return data; } ~string() { } }; 集合与元素的关系 若A表示一个集合,a是集合A中的元素,记作aA,读作a属于A; 若a不是集合A中的元素,则记作aA,读作a不属于A。

    未完待续

  • 一个技术知识分享、学习、交流的社区

    14 主题
    43 帖子
    sunrisepeakS

    中文版

    Introduction and Background

    A community forum oriented towards [knowledge/technology, projects, open courses, creative ideas, etc. + open-source philosophy].

    Why build such a forum?

    Many platforms are moving towards being comprehensive and entertainment-focused, leading to a massive increase in content volume but a decrease in the density of specific thematic content. There are relatively few forums related to open source in Chinese platforms, with content scattered across various platforms, lacking a common platform for exchange and sharing. Provide a platform for discussing topics related to open-source philosophy | open-source licenses | open-source projects | open-source communities | open-source and business | sustainable development of open-source, etc. Main Sections and Topics

    General Discussion Section

    Posts without a specific topic section are generally posted here.

    04473257-cc22-456b-b3e9-cfeed513a14a-image.png

    Open Courses Section

    Post content and discussions related to various open courses. Developers of courses/tutorial projects can create corresponding discussion sections.

    6ae992b8-d831-4a8d-8bbd-e00b4cb4b067-image.png

    Open Source Section

    Discussions on topics related to open-source software | open-source communities | open-source philosophy | open-source and business | sustainable development of open-source. Developers of open-source projects can create discussion sections for their projects.

    cf3aa71e-2fda-44cf-b016-6cea0876bb51-image.png

    d2learn Open Source

    Explorations of open-source content by the d2learn community/open-source group.

    04970ff0-56fb-4f7b-8f2f-a2ce42747315-image.png

    Forum Feedback & Community Building

    Feedback on forum-related issues and suggestions. Discussions on topics related to the sustainable development of the forum.

    ca1f43d9-162a-4a5d-9e3a-1dc7878f926a-image.png

    Functional Perspective A platform for open-source enthusiasts to exchange and discuss. Developers of open-source projects/open courses and tutorials can create discussion/exchange sections for their projects. A platform for discussing topics related to open-source itself (e.g., sustainable development of open-source projects). Others. Forum Philosophy and Restrictions

    Philosophy and Orientation

    Focus on technology, knowledge, creativity, and open-source fields, avoiding entertainment. Professional content and rational discussions, avoiding deliberately emotional content.

    Forum Code of Conduct

    Encouraged behaviors

    Use inclusive language. Respect different viewpoints and experiences. Gracefully accept constructive criticism.

    Prohibited/Restricted

    Provocative, insulting/derogatory comments, and personal attacks. Registration and Section Creation

    Registration Methods

    GitHub: Directly use GitHub for SSO single sign-on. Email invitation registration: Users who have already registered and completed email verification can invite others to register via the user interface.

    Personal Blogs and Subsection Creation

    Application Template Others https://d2learn.org https://github.com/d2learn Instant communication group (Q): 167535744

    Note: translate by ai-llm - original

  • Got a question? Ask away!

    2 主题
    9 帖子
    sunrisepeakS

    推荐一个开源浏览器插件, 效果不错。后面研究研究看能不能直接集成到论坛上
    image.png

    https://github.com/darkreader/darkreader

公告栏 | Bulletin Board

欢迎加入d2learn社区 - 社区指南
Welcome to the d2learn Community - Community Guide

一个以 [知识、技术、代码、项目、想法、开源] 相关话题为主导的社区
A community focused on topics related to [knowledge, technology, code, projects, ideas, and open source].


在线用户