如何使用 Ruby 的 Enumerable .map 方法在 C++ 中执行类似于 map 的操作

How to use Ruby's Enumerable .map method to do something similar to map in C++

本文关键字:map 操作 执行 C++ 类似于 方法 何使用 Ruby Enumerable      更新时间:2023-10-16
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 中的含义有点不同,更合适的描述可能是变换