C++ STL 中的取反函式


取反函式用於取反給定的值,以便改變這些值上的符號。它將負值變為正值,反之亦然。

函式原型

function transform(a_begin, a_end, a1_begin, negate()):
   a_begin = lower bound of the array.
   a_end = upper bound of the array.
   a1_end = Lower bound of the second modified array.
   negate() = to negate the values of the array.

示例程式碼

#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
   int a[] = { 4,6,7, -10, -20, -30 };
   transform(a, a + 6, a, negate<int>());
   for (int i = 0; i < 6; i++)
      cout << a[i] << ' ';
   return 0;
}

輸出

-4 -6 -7 10 20 30

更新於: 30-7-2019

862 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.