我有以下階代碼:轉換這個階代碼以C++
val gates = varNode.getGates()
val inMsgs = gates.map(g => g.getEndGate.getMessage())
val variableMarginal = inMsgs.reduceLeft((msg1, msg2) => msg1 * msg2)
這是在C以下++(假設我們知道的種類和使用的底層C++容器是一個向量)相同的?
std::vector<Gate *> gates = varNode.getGates();
// Assume that the first gate always has a valid message
double marginal = gates[0]->getEndGate()->getMessage();
for (int i = 1; i < gates.size(); ++i)
marginal *= gates[i]->getEndGate()->getMessage();
我對reduceLeft函數感到困惑。無法理解它的作用。
[編輯]類被定義爲柵如下:
sealed abstract class Gate(initialMsg: SingleFactor) {
type END_GATE <: Gate
private var endGate: Option[END_GATE] = None
private var message: SingleFactor = initialMsg
private var oldMessage: SingleFactor = initialMsg
def setEndGate(gate: END_GATE) { endGate = Some(gate) }
def getEndGate(): END_GATE = endGate.get
def setMessage(newMessage: SingleFactor, msgIndex: Long) {
oldMessage = message
message = newMessage
}
def getMsgIndex(): Long = msgIndex
def getMessage(): SingleFactor = message
def getOldMessage(): SingleFactor = oldMessage
}
很難說不知道,什麼'Gate'實際上是...... –
對不起。現在添加Gate抽象類定義。具體的對象只是覆蓋一些東西 – Luca