Computing the Inner Product of Two Sequences
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, T initValue)
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, T initValue,
BinaryFunc op1, BinaryFunc op2)
• The first form computes and returns the inner product of initValue and all elements in the range
[beg,end) combined with the elements in the range starting with beg2. In particular, it calls the
following for all corresponding elements:
initValue = initValue + elem1 * elem2
• The second form computes and returns the result of calling op for initValue and all elements in
the range [beg,end) combined with the elements in the range starting with beg2. In particular, it
calls the following for all corresponding elements:
initValue = op1(initValue,op2(elem1,elem2))
• Thus, for the values
a1 a2 a3 ...
b1 b2 b3 ...
they compute and return either
initValue + (a1 * b1) + (a2 * b2) + (a3 * b3) + ...
or
initValue op1 (a1 op2 b1) op1 (a2 op2 b2) op1 (a3 op2 b3) op1 ...
respectively.
• If the first range is empty (beg1==end1), both forms return initValue.
• The caller has to ensure that the range starting with beg2 contains enough elements.
• op1 and op2 must not modify their arguments.
• Complexity: linear (numElems calls of operators + and * or numElems calls of op1() and op2(),
respectively).
The following program demonstrates how to use inner_product(). It processes the sum of products
and the product of the sums for two sequences:
คำนวณการคูณภายในของสองลำดับ
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, T initValue)
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, T initValue,
BinaryFunc op1, BinaryFunc op2)
•แบบแรกคำนวณ และส่งกลับค่าผลิตภัณฑ์ภายใน initValue และองค์ประกอบทั้งหมดในช่วง
[วอนสิ้นสุด) รวมกับองค์ประกอบในช่วงเริ่มต้น ด้วย beg2 โดยเฉพาะ เรียกการ
ตามองค์ประกอบที่เกี่ยวข้องทั้งหมด:
initValue = initValue elem1 * elem2
•แบบสองตัว และส่งกลับค่าผลลัพธ์ของการเรียก op สำหรับ initValue และองค์ประกอบทั้งหมดใน
ช่วง [beg,end) รวมกับองค์ประกอบในช่วงเริ่มต้น ด้วย beg2 โดยเฉพาะอย่างยิ่ง มัน
เรียกต่อไปนี้สำหรับองค์ประกอบที่เกี่ยวข้องทั้งหมด:
initValue = op1(initValue,op2(elem1,elem2))
•ดังนี้ สำหรับค่า
a1 a2 a3...
b1 b2 b3...
จะคำนวณ และส่งกลับหรือ
initValue (a1 * b1) (a2 * b2) (a3 * b3)...
หรือ
initValue op1 (a1 op2 b1) op1 (a2 op2 b2) op1 (a3 op2 b3) op1...
ตามลำดับ.
•ช่วงแรกจะ (beg1 == end1), รูปแบบกลับ initValue.
•มีผู้เรียกเพื่อให้แน่ใจว่า ช่วงที่เริ่มต้น ด้วย beg2 ประกอบด้วยองค์ประกอบเพียงพอ
• op1 และ op2 ต้องปรับเปลี่ยนอาร์กิวเมนต์ของพวกเขาไม่
•ความซับซ้อน: เส้น (เรียก numElems ของผู้ประกอบการ และ * โทร numElems ของ op1() และ op2 () หรือ
ตามลำดับ)
โปรแกรมต่อไปนี้สาธิตวิธีการใช้ inner_product() ประมวลผลรวมผลิตภัณฑ์
และผลคูณของผลรวมลำดับที่สอง:
การแปล กรุณารอสักครู่..

การคำนวณด้านผลิตภัณฑ์ของสองลำดับ
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, T initValue)
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, T initValue,
BinaryFunc op1, BinaryFunc OP2)
•รูปแบบครั้งแรกและผลตอบแทนที่คำนวณ ผลิตภัณฑ์ด้านในของ initValue และองค์ประกอบทั้งหมดที่อยู่ในช่วง
[ขอจบ) รวมกับองค์ประกอบในช่วงที่เริ่มต้นด้วย beg2 โดยเฉพาะอย่างยิ่งที่เรียกว่า
ต่อไปนี้สำหรับองค์ประกอบที่เกี่ยวข้องทั้งหมด:
initValue = initValue + elem1 * elem2
•คำนวณรูปแบบที่สองและผลตอบแทนของการเรียกสหกรณ์เพื่อ initValue และองค์ประกอบทั้งหมดที่อยู่ใน
ช่วง [ขอจบ) รวมกับองค์ประกอบใน ช่วงที่เริ่มต้นด้วย beg2 โดยเฉพาะอย่างยิ่งมัน
เรียกร้องต่อไปนี้สำหรับองค์ประกอบทั้งหมดที่เกี่ยวข้อง:
initValue = op1 (initValue, OP2 (elem1, elem2))
•ดังนั้นสำหรับค่า
a1 a2 a3 ...
b1 b2 b3 ...
พวกเขาคำนวณและกลับอย่างใดอย่างหนึ่ง
initValue + (a1 * b1) + (* a2 b2) + (a3 * b3) + ...
หรือ
initValue op1 (a1 OP2 b1) op1 (a2 b2 OP2) op1 (a3 OP2 b3) op1 ...
ตามลำดับ
•หาก ช่วงแรกที่ว่างเปล่า (beg1 == end1) ทั้งสองรูปแบบกลับ initValue
•โทรได้เพื่อให้แน่ใจว่าช่วงที่เริ่มต้นด้วย beg2 มีองค์ประกอบพอ
• op1 และ OP2 ต้องไม่แก้ไขข้อโต้แย้งของพวกเขา
•ซับซ้อน: เชิงเส้น (numElems ใช้โทรศัพท์ของ ผู้ประกอบการ + และ * หรือ numElems สายของ op1 () และ OP2 ()
ตามลำดับ)
โปรแกรมต่อไปนี้แสดงให้เห็นถึงวิธีการใช้ inner_product () ที่จะประมวลผลรวมของสินค้า
และผลิตภัณฑ์ของผลบวกสำหรับสองลำดับ:
การแปล กรุณารอสักครู่..

คอมพิวเตอร์ผลิตภัณฑ์ภายในสองลำดับ
T
inner_product ( inputiterator1 beg1 inputiterator1 end1 , , inputiterator2 beg2
T
T
initvalue ) inner_product ( inputiterator1 beg1 inputiterator1 end1
, , inputiterator2 beg2 T initvalue binaryfunc op1
, ,
- binaryfunc op2 ) รูปแบบแรกและคำนวณการคืนสินค้า ภายใน initvalue และองค์ประกอบทั้งหมดในช่วง
[ ขอร้องจบ ) รวมกับองค์ประกอบในช่วงเริ่มต้น ด้วย beg2 . โดยเฉพาะอย่างยิ่ง มันโทร
ต่อไปนี้สำหรับทุกองค์ประกอบที่สอดคล้องกัน :
initvalue = initvalue elem1 * elem2
- แบบฟอร์มที่สองคำนวณผลตอบแทนผลเรียก OP สำหรับ initvalue และองค์ประกอบทั้งหมดใน
ช่วง [ ขอร้อง จบ ) รวมกับองค์ประกอบในช่วงเริ่มต้น ด้วย beg2 . โดยเฉพาะอย่างยิ่งมัน
โทรตามทุกองค์ประกอบที่สอดคล้องกัน :
initvalue = op1 ( initvalue op2 ( elem1 , ,
- elem2 ) ดังนั้นค่า
A1 A2 A3 B1 B2 B3
. . . . . . . . . . . . . . พวกเขาคำนวณและกลับมาเหมือนกัน
initvalue ( A1 * B1 ) ( A2 * B2 ) ( A3 * B3
) . . . . . . . หรือ
initvalue op1 ( A1 op2 B1 ) op1 ( A2 op2 B2 ) op1 ( A3 op2 B3 ) op1 ตามลำดับ . . . . . . .
.
- ถ้าช่วงก่อนว่าง ( beg1 = = end1 ) ทั้งรูปแบบกลับ
initvalue .- โทรได้เพื่อให้แน่ใจว่าช่วงที่เริ่มต้นด้วย beg2 ประกอบด้วยองค์ประกอบพอ
- op1 op2 ไม่ต้องปรับเปลี่ยนและข้อโต้แย้งของพวกเขา .
- ความซับซ้อน : เชิงเส้น ( numelems เรียกร้องของผู้ประกอบการและ * หรือ numelems และสายของ op1() op2()
, ตามลำดับ ) โปรแกรมต่อไปนี้แสดงให้เห็นถึงวิธีการใช้ inner_product() . ) ผลรวมของผลิตภัณฑ์
และผลิตภัณฑ์ของผลรวมสองลำดับ :
การแปล กรุณารอสักครู่..
