auto rec = [&](auto self, int x) -> int
时间: 2024-04-25 22:24:50 浏览: 6
这是一个使用lambda表达式定义的递归函数。函数名为`rec`,参数包括一个自身引用`self`和一个整数`x`。返回类型为`int`。
在lambda函数体内部,可以通过调用`self`来递归调用自身。`self`是一个函数对象,它接受相同的参数类型和返回类型,以及递归调用时所需的参数。
这种技术称为"带有递归的lambda",它在C++14中被引入。它可以用于编写递归函数,而不需要显式定义函数。
相关问题
解释下Peoplesoft的这段代码 Component array of array of any &tmpArray; Local Record &rec1, &rec2; &rec1 = CreateRecord(Record.HIK_IPSANRS_TBL); &rec2 = CreateRecord(Record.HIK_IPSAN_TBL); Local Rowset &rsLvl1, &rsDtl; Local number &i, &j; Local Row &row; &rsLvl1 = GetLevel0()(1).GetRowset(Scroll.WPS_STR_INF_TBL); For &i = 1 To &rsLvl1.ActiveRowCount &rsDtl = &rsLvl1(&i).GetRowset(Scroll.WPS_STR_DTL_TBL); For &j = 1 To &rsDtl.ActiveRowCount &row = &rsDtl(&j); If Not &row.IsDeleted Then If All(&row.WPS_STR_DTL_TBL.WPS_GP_AMT_ID.Value) Then &rec1 = CreateRecord(Record.WPS_IPSANRS_TBL); &rec1.WPS_GP_AMT_ID.Value = &row.WPS_STR_DTL_TBL.WPS_GP_AMT_ID.Value; &rec1.EFFDT.Value = &row.WPS_STR_DTL_TBL.EFFDT.Value; If &rec1.SelectByKey() Then &rec1.WPS_GP_AMT_ID.Value = &row.WPS_STR_DTL_TBL.WPS_GP_AMT_ID.Value; &rec1.EFFDT.Value = &row.WPS_STR_DTL_TBL.EFFDT.Value; &rec1.STATUS.Value = &rsLvl1(&i).WPS_STR_INF_TBL.STATUS.Value; &rec1.Update(); Else &rec1.WPS_GP_AMT_ID.Value = &row.WPS_STR_DTL_TBL.WPS_GP_AMT_ID.Value; &rec1.EFFDT.Value = &row.WPS_STR_DTL_TBL.EFFDT.Value; &rec1.STATUS.Value = "A"; &rec1.Insert(); End-If; &rec2 = CreateRecord(Record.WPS_IPSAN_TBL); &rec2.WPS_GP_AMT_ID.Value = &row.WPS_STR_DTL_TBL.WPS_GP_AMT_ID.Value; &rec2.EFFDT.Value = &row.WPS_STR_DTL_TBL.EFFDT.Value; &rec2.WPS_STORID.Value = &row.WPS_STR_DTL_TBL.WPS_STORID.Value; &rec2.WPS_GP_WAGECODE.Value = &row.WPS_STR_DTL_TBL.WPS_GP_WAGECODE.Value; If Not &rec2.Insert() Then &rec2.Update(); End-If; End-If; End-If; End-For; End-For;
这段代码是一个PeopleCode脚本,用于在PeopleSoft系统中进行数据操作。
首先,定义了一个Component类型的数组变量&tmpArray。然后,创建了两个Record类型的本地变量&rec1和&rec2,分别对应PeopleSoft中的两个表格。接着,创建了两个Rowset类型的本地变量&rsLvl1和&rsDtl,并定义了两个number类型的本地变量&i和&j。
在循环中,通过GetLevel0()方法获取Level0的第一行,并通过GetRowset()方法获取Scroll.WPS_STR_INF_TBL的Rowset。然后,循环遍历&rsLvl1中的每一行,再通过GetRowset()方法获取Scroll.WPS_STR_DTL_TBL的Rowset。接着,在&rsDtl中循环遍历每一行,如果当前行未被删除且满足一定条件,则执行下面的操作。
首先,判断&row中的WPS_STR_DTL_TBL.WPS_GP_AMT_ID.Value是否全部存在,如果是,则创建一个新的&rec1记录,并将一些属性值赋给它。如果该记录已存在于数据库中,则更新它,否则插入新记录。
然后,创建一个新的&rec2记录,并将一些属性值赋给它。如果该记录不存在于数据库中,则插入新记录,否则更新它。
最后,循环结束后,该脚本会执行一些数据操作,例如更新或插入记录到数据库中。
帮我把下面一段C++代码改写成python代码:#include "Trade.h" #include "WPrice.h" #include <algorithm> double normalCDF(double x) // Phi(-∞, x) aka N(x) { return std::erfc(-x / std::sqrt(2)) / 2; //erfc()是互补误差函数,该返回值表示标准正态分布下var小于x的概率,即N(x) } CTrade::CTrade(double tick) : wp_bid(0.01), wp_ask(0.01), m_tick(tick), m_TimeRound(50) { newday(NULL); } CTrade::~CTrade() { } void CTrade::OnBook(const BTRec& btRec) { wp.setGamma(0.1); wp_bid = wp.getWP(&btRec.Bids); wp_ask = wp.getWP(&btRec.Asks); if (wp_mid > 0){ //wp_mid初始化为-1,仅遇到第一条BTRec记录时条件为false double wp_now = (wp_bid + wp_ask) / 2; //updated wp_mid int volume = btRec.volume; //volume between two orderbook records double ratio = normalCDF((wp_now - wp_mid) / (2 * m_tick)); //m_tick = tick = 0.2 double buyvolume = ratio*volume, sellvolume = (1 - ratio)*volume; m_TimeRound.update(buyvolume, sellvolume, btRec.rec_time.timestamp); //volume moving average if (mv_volume < 0) { mv_volume = volume; mv_buyvolume = buyvolume; } else{ mv_volume += 0.002*(volume - mv_volume); mv_buyvolume += 0.002*(buyvolume - mv_buyvolume); } // round trip volatility if (time_ini < 0 || btRec.rec_time.timestamp - time_ini >= time_scale){ if (time_ini>0){ double dp = wp_now - wp_ini; volatility += 0.05*(dp*dp - volatility); } time_scale = m_TimeRound.getTime() * 1000; //in milliseconds time_ini = btRec.rec_time.timestamp; wp_ini = wp_now; } } wp_mid = (wp_bid + wp_ask) / 2; } void CTrade::newday(const char* p) { wp_mid = -1; m_TimeRound.newday(p); volatility = 16 * m_tick*m_tick; time_slapse = -1, time_scale = -1, wp_ini = -1, time_ini = -1; mv_buyvolume = mv_volume = -1; }
```python
import math
class CTrade:
def __init__(self, tick):
self.wp_bid = 0.01
self.wp_ask = 0.01
self.m_tick = tick
self.m_TimeRound = TimeRound()
self.newday(None)
def OnBook(self, btRec):
self.wp.setGamma(0.1)
self.wp_bid = self.wp.getWP(btRec.Bids)
self.wp_ask = self.wp.getWP(btRec.Asks)
if self.wp_mid > 0:
wp_now = (self.wp_bid + self.wp_ask) / 2
volume = btRec.volume
ratio = normalCDF((wp_now - self.wp_mid) / (2 * self.m_tick))
buyvolume = ratio * volume
sellvolume = (1 - ratio) * volume
self.m_TimeRound.update(buyvolume, sellvolume, btRec.rec_time.timestamp)
if self.mv_volume < 0:
self.mv_volume = volume
self.mv_buyvolume = buyvolume
else:
self.mv_volume += 0.002 * (volume - self.mv_volume)
self.mv_buyvolume += 0.002 * (buyvolume - self.mv_buyvolume)
if self.time_ini < 0 or btRec.rec_time.timestamp - self.time_ini >= self.time_scale:
if self.time_ini > 0:
dp = wp_now - self.wp_ini
self.volatility += 0.05 * (dp * dp - self.volatility)
self.time_scale = self.m_TimeRound.getTime() * 1000
self.time_ini = btRec.rec_time.timestamp
self.wp_ini = wp_now
self.wp_mid = (self.wp_bid + self.wp_ask) / 2
def newday(self, p):
self.wp_mid = -1
self.m_TimeRound.newday(p)
self.volatility = 16 * self.m_tick * self.m_tick
self.time_slapse = -1
self.time_scale = -1
self.wp_ini = -1
self.time_ini = -1
self.mv_buyvolume = -1
self.mv_volume = -1
def normalCDF(x):
return math.erfc(-x / math.sqrt(2)) / 2
class TimeRound:
def __init__(self):
self.buy_volume = 0
self.sell_volume = 0
self.time = 0
def update(self, buyvolume, sellvolume, timestamp):
if timestamp > self.time:
self.buy_volume = 0
self.sell_volume = 0
self.time = timestamp
self.buy_volume += buyvolume
self.sell_volume += sellvolume
def getTime(self):
return self.buy_volume + self.sell_volume
```