Namespaces
Variants

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

From cppreference.net
valarray < T > operator + ( ) const ;
(1)
valarray < T > operator - ( ) const ;
(2)
valarray < T > operator~ ( ) const ;
(3)
valarray < bool > operator ! ( ) const ;
(4)
**참고사항:** - 모든 C++ 코드와 HTML 태그는 원본 그대로 유지되었습니다. - 번역이 필요한 일반 텍스트가 없으므로 원본 내용이 그대로 출력됩니다. - C++ 관련 용어(valarray, operator, const 등)와 HTML 구조는 변경되지 않았습니다.

숫자 배열의 각 요소에 단항 연산자를 적용합니다.

목차

매개변수

(없음)

반환값

* 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와 값에 이항 연산자를 적용
(function template)