首页 \ 问答 \ 在C ++中,你能创建一个对象作为arg来运行吗?(In C++, can you create an object as arg to function?)

在C ++中,你能创建一个对象作为arg来运行吗?(In C++, can you create an object as arg to function?)

在以下简单的两个类中

class B {
  private:
    int i;
  public:
    B( int p_i ) { i = p_i; };
}
class C {
  private:
    B* b;
  public:
    C( B* p_b ) { b = p_b; };
}

创建B作为C()的参数是合法的吗?

例如,是

C c = C( &B(5) );

合法,创造B在飞行中?


In the following simple two classes

class B {
  private:
    int i;
  public:
    B( int p_i ) { i = p_i; };
}
class C {
  private:
    B* b;
  public:
    C( B* p_b ) { b = p_b; };
}

is it legal to create B as an argument to C()?

for example, is

C c = C( &B(5) );

legal, creating B on the fly?


原文:https://stackoverflow.com/questions/42338703
更新时间:2021-11-20 18:11

最满意答案

这个修改如何?

修改点:

  • 通过分隔“标题”和“值”来检索每个数据。
  • 创建一个用于将数据导入Spreadsheet的数组,并使用setValues()将其导入。

修改的脚本:

function myFunction() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();
  var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");

  // Modified
  var out = JSON.parse(data.getContentText());
  var title = [];
  for (var i in out[0]) {
    title.push(i);
  }
  var res = [];
  res.push(title);
  for (var i in out) {
    var values = [];
    for (var j in out[i]) {
      values.push(out[i][j]);
    }
    res.push(values);
  }
  ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}

参考:

如果我误解了你的问题,请告诉我。 我想修改它。

编辑1:

为了从out[0]对象中检索关键字,我使用for (var i in out[0]) { 。 请看下面的例子。

样品1:

var sample = [{key1: "value1", key2: "value2"}];
for (var i in sample[0]) {
  Logger.log("%s, %s", i, sample[0][i])
}
结果
key1, value1
key2, value2

在此脚本中, sample[0]{key1: "value1", key2: "value2"}for (var i in sample[0]) {可以检索关键字key1key2 。 使用检索的键值,可以通过sample[0][i]检索value1value2

另一方面,请参阅下一个示例脚本。

样品2:

var sample = [{key1: "value1", key2: "value2"}];
for (var i = 0; i < sample[0].length; i++) {
  Logger.log("%s, %s", i, sample[0][i])
}

在这个脚本中, sample[0].length变得undefined 。 因为object.length无法检索长度。 所以这个for循环不起作用。

如果您想使用for循环来检索键和值,如示例2所示。请使用下一个示例脚本。

样品3:

var sample = [{key1: "value1", key2: "value2"}];
var keys = Object.keys(sample[0]);
for (var i=0; i<keys.length; i++) {
  Logger.log("%s, %s", keys[i], sample[0][keys[i]])
}
结果
key1, value1
key2, value2

在这个脚本中,可以通过Object.keys()来检索对象中的键。 使用这个,键和值被检索。

参考:

编辑2:

function myFunction() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();
  var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");

  // Modified
  var out = JSON.parse(data.getContentText());
  var title = [];
  for (var i in out[0]) {
    if (i == "assetCode" || i == "transactionFee") {
      title.push(i);
    }
  }
  var res = [];
  res.push(title);
  for (var i in out) {
    var values = [];
    for (var j in out[i]) {
      if (j == "assetCode" || j == "transactionFee") {
        values.push(out[i][j]);
      }
    }
    res.push(values);
  }
  ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}

How about this modification?

Modification points :

  • Retrieve each data by separating "title" and "values".
  • Create an array for importing data to Spreadsheet and import it using setValues().

Modified script :

function myFunction() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();
  var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");

  // Modified
  var out = JSON.parse(data.getContentText());
  var title = [];
  for (var i in out[0]) {
    title.push(i);
  }
  var res = [];
  res.push(title);
  for (var i in out) {
    var values = [];
    for (var j in out[i]) {
      values.push(out[i][j]);
    }
    res.push(values);
  }
  ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}

Reference :

If I misunderstand your question, please tell me. I would like to modify it.

Edit 1 :

In order to retrieve keys from object which is out[0], I used for (var i in out[0]) {. Please see the following samples.

Sample 1 :

var sample = [{key1: "value1", key2: "value2"}];
for (var i in sample[0]) {
  Logger.log("%s, %s", i, sample[0][i])
}
Result
key1, value1
key2, value2

In this script, sample[0] is an object of {key1: "value1", key2: "value2"}. for (var i in sample[0]) { can retrieve keys which are key1 and key2. Using retrieved keys, value1 and value2 can be retrieved by sample[0][i].

On the other hand, please see the next sample script.

Sample 2 :

var sample = [{key1: "value1", key2: "value2"}];
for (var i = 0; i < sample[0].length; i++) {
  Logger.log("%s, %s", i, sample[0][i])
}

In this script, sample[0].length becomes undefined. Because object.length cannot retrieve the length. So this for loop doesn't work.

If you want to retrieve keys and values using for loop like the sample 2. Please use the next sample script.

Sample 3 :

var sample = [{key1: "value1", key2: "value2"}];
var keys = Object.keys(sample[0]);
for (var i=0; i<keys.length; i++) {
  Logger.log("%s, %s", keys[i], sample[0][keys[i]])
}
Result
key1, value1
key2, value2

In this script, keys from object can be retrieved by Object.keys(). Using this, keys and values are retrieved.

Reference :

Edit 2 :

function myFunction() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();
  var data = UrlFetchApp.fetch("https://www.binance.com/assetWithdraw/getAllAsset.html");

  // Modified
  var out = JSON.parse(data.getContentText());
  var title = [];
  for (var i in out[0]) {
    if (i == "assetCode" || i == "transactionFee") {
      title.push(i);
    }
  }
  var res = [];
  res.push(title);
  for (var i in out) {
    var values = [];
    for (var j in out[i]) {
      if (j == "assetCode" || j == "transactionFee") {
        values.push(out[i][j]);
      }
    }
    res.push(values);
  }
  ss.getRange(ss.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。