1. 概念对比:JavaScript vs C++
JavaScript (ES6+) 导入方式
javascript
复制代码
// 命名导入
import { useState, useEffect } from 'react';
import { debounce } from 'lodash';
// 默认导入
import React from 'react';
import axios from 'axios';
// 全部导入
import * as THREE from 'three';
// 动态导入
const module = await import('./module.js');
C++ 导入方式
cpp
复制代码
// 标准库导入 - 使用尖括号 <>
#include <iostream> // 输入输出流
#include <vector> // 动态数组
#include <string> // 字符串类
#include <algorithm> // 算法库
#include <memory> // 智能指针
// 用户定义头文件 - 使用双引号 ""
#include "MyClass.hpp"
#include "utils/helper.hpp"
// 项目库头文件
#include "threepp/threepp.hpp"
2. C++常用标准库对照表
功能类别
JavaScript
C++标准库
C++使用
输入输出
console.log()
#include <iostream>
std::cout
, std::cin
动态数组
Array
#include <vector>
std::vector<T>
字符串
String
#include <string>
std::string
映射/对象
Object/Map
#include <map>
std::map<K,V>
集合
Set
#include <set>
std::set<T>
数学运算
Math
#include <cmath>
std::sin
, std::cos
算法
Array.prototype.*
#include <algorithm>
std::sort
, std::find
异步/线程
Promise/async
#include <thread>
std::thread
时间
Date
#include <chrono>
std::chrono
正则表达式
RegExp
#include <regex>
std::regex
随机数
Math.random()
#include <random>
std::random_device
3. 实际示例对比
3.1 基础输入输出
javascript
复制代码
// JavaScript
console.log("Hello World");
const input = prompt("Enter your name:");
console.log(`Hello, ${input}!`);
cpp
复制代码
// C++
#include <iostream>
#include <string>
int main() {
std::cout << "Hello World" << std::endl;
std::string input;
std::cout << "Enter your name: ";
std::getline(std::cin, input);
std::cout << "Hello, " << input << "!" << std::endl;
return 0;
}
3.2 数组/向量操作
javascript
复制代码
// JavaScript
const numbers = [1, 2, 3, 4, 5];
numbers.push(6);
const doubled = numbers.map(x => x * 2);
const sum = numbers.reduce((a, b) => a + b, 0);
console.log(doubled); // [2, 4, 6, 8, 10, 12]
cpp
复制代码
// C++
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.push_back(6);
// 映射操作
std::vector<int> doubled;
std::transform(numbers.begin(), numbers.end(),
std::back_inserter(doubled),
[](int x) { return x * 2; });
// 求和
int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
// 输出
for(int n : doubled) {
std::cout << n << " ";
}
return 0;
}
3.3 字符串操作
javascript
复制代码
// JavaScript
const str = "Hello World";
const upper = str.toUpperCase();
const parts = str.split(" ");
const found = str.includes("World");
cpp
复制代码
// C++
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
int main() {
std::string str = "Hello World";
// 转大写
std::string upper = str;
std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
// 分割字符串
std::istringstream iss(str);
std::vector<std::string> parts;
std::string part;
while (std::getline(iss, part, ' ')) {
parts.push_back(part);
}
// 查找
bool found = str.find("World") != std::string::npos;
return 0;
}
4. C++标准库分类
4.1 容器库(相当于JS的数据结构)
cpp
复制代码
#include <vector> // 动态数组,类似JS Array
#include <array> // 固定大小数组
#include <list> // 双向链表
#include <deque> // 双端队列
#include <stack> // 栈
#include <queue> // 队列
#include <set> // 集合,类似JS Set
#include <map> // 映射,类似JS Map/Object
#include <unordered_set> // 哈希集合
#include <unordered_map> // 哈希映射
4.2 算法库(相当于JS的Array方法)
cpp
复制代码
#include <algorithm> // 排序、查找、变换等算法
#include <numeric> // 数值算法(求和、累积等)
// 常用算法示例
std::sort(vec.begin(), vec.end()); // 排序
std::find(vec.begin(), vec.end(), value); // 查找
std::reverse(vec.begin(), vec.end()); // 反转
std::transform(vec.begin(), vec.end(), result.begin(), func); // 变换
4.3 工具库
cpp
复制代码
#include <utility> // std::pair, std::move等工具
#include <functional> // 函数对象、bind等
#include <memory> // 智能指针
#include <tuple> // 元组
#include <optional> // 可选值(C++17)
#include <variant> // 变体类型(C++17)
4.4 输入输出库
cpp
复制代码
#include <iostream> // 基础输入输出
#include <fstream> // 文件操作
#include <sstream> // 字符串流
#include <iomanip> // 输出格式控制
4.5 时间和线程库
cpp
复制代码
#include <chrono> // 时间库
#include <thread> // 线程
#include <mutex> // 互斥锁
#include <condition_variable> // 条件变量
#include <future> // 异步操作
5. ThreeJS vs ThreePP 库导入对比
ThreeJS
javascript
复制代码
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera();
const renderer = new THREE.WebGLRenderer();
ThreePP
cpp
复制代码
#include "threepp/threepp.hpp" // 主库
#include "threepp/controls/OrbitControls.hpp" // 控制器
using namespace threepp;
auto scene = Scene::create();
auto camera = PerspectiveCamera::create();
GLRenderer renderer;