以对为对象访问地图

时间:2017-03-14 11:51:00

标签: c++ stl

正如标题所说。 我是c ++的新手。 所以我想用一对int作为键,使用boolean作为值

// Map range of numbers to another range of numbers
var mapRange = function(from, to, s) {
  return to[0] + (s - from[0]) * (to[1] - to[0]) / (from[1] - from[0]);
};
// Your event listener and logic 
window.addEventListener('deviceorientation', function(eventData) {
  // The compass direction - will return a value between 0 and 360
  var R = Math.round(mapRange([0, 360], [0, 255], eventData.alpha));

  // Side to side value - will return a value between -180 and 180
  var G = Math.round(mapRange([-180, 180], [0, 255], eventData.beta));

  // Front to back value - will return a value between -90 and 90
  var B = Math.round(mapRange([-90, 90], [0, 255], eventData.gamma));
  
  console.log(R, G, B); // Sample output, deal with these however you like
});

// Test 1
var eventDataSample1 = {
  alpha: 125,
  beta: -65,
  gamma: -2
}
var R = Math.round(mapRange([0, 360], [0, 255], eventDataSample1.alpha));
var G = Math.round(mapRange([-180, 180], [0, 255], eventDataSample1.beta));
var B = Math.round(mapRange([-90, 90], [0, 255], eventDataSample1.gamma));
console.log(R, G, B);

// Test 2
var eventDataSample2 = {
  alpha: 12,
  beta: 66,
  gamma: 17
}
var R = Math.round(mapRange([0, 360], [0, 255], eventDataSample2.alpha));
var G = Math.round(mapRange([-180, 180], [0, 255], eventDataSample2.beta));
var B = Math.round(mapRange([-90, 90], [0, 255], eventDataSample2.gamma));
console.log(R, G, B);

如何分配值并访问它?

2 个答案:

答案 0 :(得分:1)

您需要传递一个std::pair<int,int>对象(您的评论建议传递两个int个对象,但这不是尚未。)

您可以像这样创建std::pair<int,int>std::pair<int,int> {5,7}或者从std::make_pair(5, 7)的两个参数中找出类型。

答案 1 :(得分:0)

第一个参数

map <pair<int,int>, bool> mp;

是配对密钥。因此,您可以分配一个值并将其作为-

访问
mp[{1,2}] = true;