operator==,!=,<,<=,>,>=std::valarray - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# operator==,!=,<,<=,>,>=std::valarray

From cppreference.com
< cpp‎ | numeric‎ | valarray

template< class T > valarray<bool> operator==( const valarray<T>& lhs, const valarray<T>& rhs ); template< class T > valarray<bool> operator!=( const valarray<T>& lhs, const valarray<T>& rhs ); template< class T > valarray<bool> operator<( const valarray<T>& lhs, const valarray<T>& rhs ); template< class T > valarray<bool> operator<=( const valarray<T>& lhs, const valarray<T>& rhs ); template< class T > valarray<bool> operator>( const valarray<T>& lhs, const valarray<T>& rhs ); template< class T > valarray<bool> operator>=( const valarray<T>& lhs, const valarray<T>& rhs ); | (1) |
template< class T > valarray<bool> operator==( const T& lhsv, const valarray<T>& rhs ); template< class T > valarray<bool> operator!=( const T& lhsv, const valarray<T>& rhs ); template< class T > valarray<bool> operator<( const T& lhsv, const valarray<T>& rhs ); template< class T > valarray<bool> operator<=( const T& lhsv, const valarray<T>& rhs ); template< class T > valarray<bool> operator>( const T& lhsv, const valarray<T>& rhs ); template< class T > valarray<bool> operator>=( const T& lhsv, const valarray<T>& rhs ); | (2) |
template< class T > valarray<bool> operator==( const valarray<T>& lhs, const T& rhsv ); template< class T > valarray<bool> operator!=( const valarray<T>& lhs, const T& rhsv ); template< class T > valarray<bool> operator<( const valarray<T>& lhs, const T& rhsv ); template< class T > valarray<bool> operator<=( const valarray<T>& lhs, const T& rhsv ); template< class T > valarray<bool> operator>( const valarray<T>& lhs, const T& rhsv ); template< class T > valarray<bool> operator>=( const valarray<T>& lhs, const T& rhsv ); | (3) |

Compares each value within the numeric array with another value.

1) Returns a numeric array of bool containing elements each of which is obtained by applying the indicated comparison operator to the corresponding values of `lhs` and `rhs`

The behavior is undefined if size() != v.size()

2) Returns a numeric array of bool containing elements each of which is obtained by applying the indicated comparison operator to `lhsv` and the corresponding value of `rhs` .
3) Returns a numeric array of bool containing elements each of which is obtained by applying the indicated comparison operator to the corresponding value of `lhs` and `rhsv`.

### [edit] Parameters

lhs, rhs | - | numeric arrays to compare
lhsv, rhsv | - | values to compare to each element within a numeric array

### [edit] Return value

A numeric array of bool containing comparison results of corresponding elements.

### [edit] Exceptions

(none)

### [edit] Notes

Each of the operators can only be instantiated if the following requirements are met:

  - The indicated operator can be applied to type `T`
  - The result value can be unambiguously converted to `bool`.

The function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties:

  - All const member functions of std::valarray are provided.
  - std::valarray, std::slice_array, std::gslice_array, std::mask_array and std::indirect_array can be constructed from the replacement type.
  - All functions accepting an argument of type const std::valarray& except `begin()` and `end()` (since C++14) should also accept the replacement type.
  - All functions accepting two arguments of type const std::valarray& should accept every combination of const std::valarray& and the replacement type.
  - The return type does not add more than two levels of template nesting over the most deeply-nested argument type.

### [edit] Example

Run this code

    #include <iostream>
    #include <valarray>

    int main()
    {
        // zero all negatives in a valarray
        std::valarray<int> v = {1, -1, 0, -3, 10, -1, -2};
        std::cout << "Before: ";
        for(auto n: v) std::cout << n << ' '; std::cout << '\n';
        v[v < 0] = 0;
        std::cout << "After: ";
        for(auto n: v) std::cout << n << ' '; std::cout << '\n';

        // convert the vector<bool> result of == to a single bool
        std::valarray<int> a = {1,2,3};
        std::valarray<int> b = {2,4,6};

        std::cout << "2*a == b is " << std::boolalpha
                  << (2*a == b).min() << '\n';

    }

Output:

    Before: 1 -1 0 -3 10 -1 -2
    After: 1 0 0 0 10 0 0
    2*a == b is true

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/valarray/operator_cmp&oldid=79268"