名稱空間
變體
操作

std::valarray<T>::operator+,-,~,!

來自 cppreference.com
< cpp‎ | 數值‎ | valarray
 
 
 
 
valarray<T> operator+() const;
(1)
valarray<T> operator-() const;
(2)
valarray<T> operator~() const;
(3)
valarray<bool> operator!() const;
(4)

對數值陣列中的每個元素應用一元運算子。

目錄

[編輯] 引數

(無)

[編輯] 返回值

一個數值陣列,其元素的值透過對 *this 中的值應用相應的運算子獲得。

[編輯] 異常

可能丟擲實現定義的異常。

[編輯] 注意

只有滿足以下要求時,每個運算子才能被例項化:

  • 指定的運算子可以應用於型別 T
  • 結果值可以明確地轉換為 T (1-3) 或 bool (4)。

該函式可以使用與 std::valarray 不同的返回型別實現。在這種情況下,替換型別具有以下屬性:

[編輯] 示例

#include <iostream>
#include <string_view>
#include <valarray>
 
template<typename T>
void print(std::string_view const note,
           std::valarray<T> const vala, // by-value, see Notes above
           std::string_view const term = "\n")
{
    std::cout << note << std::boolalpha << std::showpos;
    for (T const element : vala)
        std::cout << '\t' << element;
    std::cout << term;
}
 
int main()
{
    std::valarray<int> x{1, 2, 3, 4};
    print<int>("x: ", x);
    print<int>("+x: ", +x);
    print<int>("+ + x: ", + + x);
    print<int>("-x: ", -x);
    print<int>("- - x: ", - - x, "\n\n");
 
    std::valarray<short> y{0, 1, -1, 0x7fff};
    print<short>("y: ", y);
    print<short>("~y: ", ~y);
    print<short>("~~y: ", ~~y, "\n\n");
 
    std::valarray<bool> z{true, false};
    print<bool>("z: ", z);
    print<bool>("!z: ", !z);
    print<bool>("!!z: ", !!z);
}

可能的輸出

x:      +1      +2      +3      +4
+x:     +1      +2      +3      +4
+ + x:  +1      +2      +3      +4
-x:     -1      -2      -3      -4
- - x:  +1      +2      +3      +4
 
y:      +0      +1      -1      +32767
~y:     -1      -2      +0      -32768
~~y:    +0      +1      -1      +32767
 
z:      true    false
!z:     false   true
!!z:    true    false

[編輯] 參閱

將複合賦值運算子應用於 valarray 的每個元素
(public member function) [編輯]
對兩個 valarray 的每個元素,或一個 valarray 和一個值應用二元運算子
(函式模板) [編輯]