javascript - 将 JavaScript Array.slice + 隐式扩展转换为 C++ vector

标签 javascript c++ arrays

我正在将 JavaScript 代码移植到 C++。

伪代码:

if n = 1 then APPEND(orders, order)

JavaScript(订单和订单是数组):

var order = new array();
var orders = new array();
//....
if (n == 1)
{
    orders[orders.length] = order.slice(); // append copy
}

我在 C++ 中使用 vector 而不是数组。 我认为 C++ 代码应该只是:

vector<int> order;
vector<vector<int> > orders;
//.....
orders.push_back(order)

此代码对于上述伪代码和 javascript 代码是否正确?

最佳答案

你有一个条件 n == 1 我不知道你为什么在你的 C++ 实现中跳过了它。
在评论中你说你正在使用 slice() 来制作原始列表的拷贝,所以你也需要在你的 C++ 实现中做同样的事情。

vector< vector<int> > orders;
vector<int> order;
//and to clone and append
std::vector cloned;
std::copy(order.begin(), order.end(), std::back_inserter(cloned));
if(n == 1)
   orders.push_back(cloned);

关于javascript - 将 JavaScript Array.slice + 隐式扩展转换为 C++ vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937770/

相关文章:

c++ - 重复生成宏

python:numpy列表到数组和vstack

c# - Linq - 获取数组最后一个非零数的索引

javascript - 数组过滤和对象转换: what is fastest and what other considerations matter?

javascript - 使用显示器使表格的 <tbody> 可滚动 :block and offsetWidth

javascript - 需要附加功能的 jQuery 菜单

javascript - Ace 编辑器为 twig 模式启用 emmet

javascript - MySQL 和 View 的日期/时间格式化的正确方法,反之亦然

c++ - 在不知道值类型的情况下从前向迭代器获取反向迭代器

c++ - 在 C++ 中使用 pthread 时出现编译错误