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的具体用法)的深入用法或最佳实践感兴趣,我可以为你提供更详细的解释和示例。

相关推荐
CoderCodingNo1 小时前
【GESP】C++五级真题(数论考点) luogu-B3871 [GESP202309 五级] 因数分解
开发语言·c++
froginwe111 小时前
NumPy 字符串函数
开发语言
ComputerInBook1 小时前
C++编程语言:标准库:第43章——C语言标准库(Bjarne Stroustrup)
c语言·c++·c语言标准库
wildlily84271 小时前
C++ Primer 第5版章节题 第九章
开发语言·c++
特立独行的猫a1 小时前
c++弱引用指针std::weak_ptr作用详解
开发语言·c++·智能指针·弱指针·weak_ptr
小菱形_1 小时前
【C#】IEnumerable
开发语言·c#
爱敲点代码的小哥1 小时前
Directoy文件夹操作对象 、StreamReader和StreamWriter 和BufferedStream
开发语言·c#
这是程序猿1 小时前
基于java的ssm框架经典电影推荐网站
java·开发语言·spring boot·spring·经典电影推荐网站
Nan_Shu_6142 小时前
学习:Java (1)
java·开发语言·学习
李慕婉学姐2 小时前
【开题答辩过程】以《基于PHP的饮食健康管理系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
开发语言·php