如何使用 Ruby 的 Enumerable .map 方法在 C++ 中执行类似于 map 的操作
How to use Ruby's Enumerable .map method to do something similar to map in C++
map(-30, -89.75, 89.75, 0, 360)
我正在寻找这样的东西,其中:
- -30 是输入值。
- -89.75 到 89.75 是可能的输入值范围
- 0 - 360 是要映射到的最终范围
有人告诉我有一种方法可以使用 http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-map
.. 然而它并不明显!
如果我理解正确,我认为您只想将一个范围统一映射到另一个范围。因此,我们只需要计算输入范围有多远,并返回输出范围的一部分。
def map_range(input, in_low, in_high, out_low, out_high)
# map onto [0,1] using input range
frac = (input - in_low) / (in_high-in_low)
# map onto output range
frac * (out_high-out_low) + out_low
end
另外,我应该注意,地图在 ruby 中的含义有点不同,更合适的描述可能是变换。
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 重载操作程序时出错>>用于类中的字符串 memebr
- 对字符串进行位操作
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 如何添加到由QtConcurrent::map操作的QStringList中?
- 如何使用 Ruby 的 Enumerable .map 方法在 C++ 中执行类似于 map 的操作
- std::map 如何提供常量 size() 操作
- std::map上可能存在线程不安全操作
- 加快std::map和boost:unordereded_map[]操作
- 是否允许std::map在只读操作后重新平衡(如Splay树)
- 是对std::map线程安全的查找和读取操作
- 使用std::map时必须互斥的所有操作/函数是什么?
- 如果在std::map中操作不存在的键值对会发生什么?