C++类型和容器在MoonBit中的对应关系整理

针对你的需求,这里整理了C++类型和容器在MoonBit中的对应关系、用法说明和代码示例。

📌 核心类型与容器对应表

C++ 类型/容器

  • std::pair<T, U> (二元组)
  • std::tuple<T...> (多元组)

MoonBit 对应

  • (T, U) (元组)
  • (T1, T2, ...) (元组)

MoonBit 代码示例

moonbit 复制代码
let pair = (42, "hello") // 类型: (Int, String)
let triple = (1, 3.14, true) // 类型: (Int, Double, Bool)
// 通过模式匹配访问
match pair {
  (x, y) => println("Got: \{x}, \{y}")
}

说明

MoonBit中,pair和tuple统一用元组(...)表示,通过模式匹配或下标访问。


C++ 类型/容器

  • T array[N] / std::array<T, N>
  • std::vector<T>

MoonBit 对应

  • FixedArray[T, N] / [T; N]
  • Vec[T] (动态数组)

MoonBit 代码示例

moonbit 复制代码
// 1. 固定大小数组
let fixed_arr: FixedArray[Int, 3] = FixedArray::make(3, 0)
fixed_arr[0] = 1 // 修改元素

// 注意陷阱:make会共享初始值
let wrong = FixedArray::make(2, FixedArray::make(2, 0)) // 内层是同一个数组
let correct = FixedArray::makei(2, fn(_) { FixedArray::make(2, 0) }) // 正确方法

// 2. 动态数组(向量)
let mut vec = Vec::new()
vec.push(1)
vec.push(2)
// 数组字面量默认为Vec
let v = [1, 2, 3] // v: @vec.Vec[Int]

说明

  • 固定数组 :初始化需注意引用陷阱,创建多维数组应用makei
  • 动态数组 :功能类似std::vector,是主要使用的可变长集合。数组字面量[...]默认构建Vec

C++ 类型/容器

  • std::map<K, V>
  • std::unordered_map<K, V>
  • std::set<T> / std::unordered_set<T>

MoonBit 对应

  • BTreeMap[K, V] (有序映射)
  • Map[K, V] / HashMap[K, V] (哈希映射)
  • Set[T] / HashSet[T] (集合)

MoonBit 代码示例

moonbit 复制代码
// 1. 哈希映射(无序)
let mut scores = Map::new() // 或 HashMap::new()
scores.put("Alice", 90)
let alice_score = scores.get("Alice")

// 2. 有序映射(基于B树)
import btreedb
let mut ordered_map = BTreeMap::new()
ordered_map.put(3, "three")

// 3. 集合
let mut unique_nums = Set::new() // 或 HashSet::new()
unique_nums.add(1)

说明

  • Map/Set 基于哈希表,无序,对应C++的unordered版本。
  • BTreeMap 基于B树,保持键的顺序,对应C++的std::map

C++ 类型/容器

  • std::list<T>
  • std::queue<T> / std::deque<T>
  • std::stack<T>
  • std::priority_queue<T>

MoonBit 对应

  • LinkedList[T]
  • VecDeque[T]
  • Vec[T] (模拟栈)
  • BinaryHeap[T]

MoonBit 代码示例

moonbit 复制代码
// 1. 双向链表(使用较少)
import linked_list
let list = LinkedList::new()

// 2. 双端队列(可作为队列使用)
import vec_deque
let mut queue = VecDeque::new()
queue.push_back(1) // 入队
queue.pop_front()  // 出队

// 3. 栈(用Vec模拟)
let mut stack = Vec::new()
stack.push(1) // 入栈
stack.pop()   // 出栈

// 4. 优先队列(二叉堆)
import binary_heap
let mut heap = BinaryHeap::new()
heap.push(3) // 默认最大堆

说明

MoonBit标准库没有专门的StackQueue类型 ,通常用Vec模拟栈,用VecDeque模拟队列。


C++ 类型/容器

  • struct / class
  • enum class
  • std::variant

MoonBit 对应

  • struct (结构体)
  • enum (枚举)
  • enum (可带数据的枚举)

MoonBit 代码示例

moonbit 复制代码
// 1. 结构体
struct Person {
  mut name: String // 可变字段需mut
  age: Int
}
impl Person {
  fn new(name: String, age: Int) -> Person {
    Person { name, age }
  }
}

// 2. & 3. 枚举(强大,可替代variant)
enum Result[T, E] {
  Ok(T)
  Err(E)
}
enum WebEvent {
  Click(x: Int, y: Int) // 可携带数据
  KeyPress(key: Char)
}
// 使用(可省略类型前缀)
let result: Result[Int, String] = Ok(42)
let event = Click(100, 200)

说明

  • struct :MoonBit统一用struct定义数据,字段默认不可变,用mut声明可变。
  • enum :非常强大,本身是独立类型,且可携带数据(变体),是std::variant的理想替代。

C++ 类型/容器

  • Lambda 表达式

MoonBit 对应

  • 闭包 (fn|params| { body })

MoonBit 代码示例

moonbit 复制代码
// 1. 使用fn关键字
let adder = fn(x: Int, y: Int) -> Int { x + y }
// 2. 更简洁的闭包语法(需上下文明确类型)
let numbers = [1, 2, 3]
numbers.each(fn(x) { println(x) })

说明

MoonBit支持函数式编程,匿名函数是"一等公民",可作为参数和返回值。

📝 重要总结与建议

  1. 核心差异

    • 命名与分类 :MoonBit的Vec对应C++的vectorMap对应unordered_mapBTreeMap对应有序的map
    • 默认不可变 :MoonBit变量和结构体字段默认不可变 ,需用mut声明可变,这与C++不同。
    • 没有独立的"类" :MoonBit没有class,使用struct+trait+impl实现面向对象。
    • 功能强大的枚举 :MoonBit的enum统一了C++的enum classstd::variant,并通过match进行模式匹配,更安全。
  2. 学习建议

    • 优先掌握VecMapenumstruct是最常用的核心类型。
    • 注意细节 :使用FixedArray初始化时要理解"共享陷阱",使用mut明确可变性。

如果你对其中某个类型(例如enum的模式匹配,或是trait的具体用法)的深入用法或最佳实践感兴趣,我可以为你提供更详细的解释和示例。

相关推荐
橘子师兄3 分钟前
C++AI大模型接入SDK—环境搭建
开发语言·c++·人工智能
lkbhua莱克瓦243 分钟前
JavaScript核心语法
开发语言·前端·javascript·笔记·html·ecmascript·javaweb
bubiyoushang8884 分钟前
基于MATLAB的近红外光谱与PLS方法测定药片有效成分含量的实现
开发语言·matlab
weixin_433179335 分钟前
Hangman 猜字游戏使用列表List实现
开发语言·python
偷星星的贼119 分钟前
C++中的状态机实现
开发语言·c++·算法
程序员敲代码吗10 分钟前
C++中的组合模式实战
开发语言·c++·算法
C_心欲无痕17 分钟前
Next.js 的服务端路由:对应api文件夹
开发语言·javascript·ecmascript
zh_xuan29 分钟前
kotlin 类委托
开发语言·kotlin
墨雨晨曦881 小时前
2026/01/20 java总结
java·开发语言
look ahead to1 小时前
关于PYQT qt designer的网格布局 单控件占多行的处理
开发语言·qt·pyqt