1. 概念对比:JavaScript vs C++
JavaScript (ES6+) 导入方式
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++ 导入方式
#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 基础输入输出
console.log("Hello World");
const input = prompt("Enter your name:");
console.log(`Hello, ${input}!`);
#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 数组/向量操作
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);
#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 字符串操作
const str = "Hello World";
const upper = str.toUpperCase();
const parts = str.split(" ");
const found = str.includes("World");
#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的数据结构)
#include <vector>
#include <array>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
4.2 算法库(相当于JS的Array方法)
#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 工具库
#include <utility>
#include <functional>
#include <memory>
#include <tuple>
#include <optional>
#include <variant>
4.4 输入输出库
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
4.5 时间和线程库
#include <chrono>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
5. ThreeJS vs ThreePP 库导入对比
ThreeJS
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
#include "threepp/threepp.hpp"
#include "threepp/controls/OrbitControls.hpp"
using namespace threepp;
auto scene = Scene::create();
auto camera = PerspectiveCamera::create();
GLRenderer renderer;