名前空間
変種
操作

std::valarray<T>::operator[]

From cppreference.com
< cpp‎ | numeric‎ | valarray
 
 
 
 
const T&               operator[]( std::size_t pos ) const;
(1)
T&                     operator[]( std::size_t pos );
(2)
std::valarray<T>       operator[]( std::slice slicearr ) const;
(3)
std::slice_array<T>    operator[]( std::slice slicearr );
(4)
std::valarray<T>       operator[]( const std::gslice& gslicearr ) const;
(5)
std::gslice_array<T>   operator[]( const std::gslice& gslicearr );
(6)
std::valarray<T>       operator[]( const std::valarray<bool>& boolarr ) const;
(7)
std::mask_array<T>     operator[]( const std::valarray<bool>& boolarr );
(8)
std::valarray<T>       operator[]( const std::valarray<std::size_t>& indarr ) const;
(9)
std::indirect_array<T> operator[]( const std::valarray<std::size_t>& indarr );
(10)

Retrieve single elements or portions of the array.

The const overloads that return element sequences create a new std::valarray object. The non-const overloads return classes holding references to the array elements.

The selected elements(s) must exist

  • for overloads (1,2), if pos is not less than size(), the behavior is undefined; and
  • for overloads (3-10), if the argument does not specify a valid subset of *this, the behavior is undefined.

目次

[edit] Parameters

pos - 返す要素の位置
slicearr - slice of the elements to return
gslicearr - gslice of the elements to return
boolarr - mask of the elements to return
indarr - indices of the elements to return

[edit] Return value

1,2) A reference to the corresponding element.
3,5,7,9) A std::valarray object containing copies of the selected items.
4,6,8,10) The corresponding data structure containing references to the selected items.

[編集] 例外

実装定義の例外をスローする場合があります。

[edit] Notes

For proper std::valarray values a, b and proper std::size_t values i, j, all of the following expressions always evaluate to true

1) (a[i] = q, a[i]) == q for non-const a
2) &a[i + j] == &a[i] + j
3) &a[i] != &b[j] for every objects a and b that are not aliases of one another
  • This means that there are no aliases in the elements and this property can be used to perform some kinds of optimization.

References become invalid on resize() or when the array is destructed.

For overloads (3,5,7,9), The function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties

  • std::valarray のすべての const メンバ関数が提供されます。
  • std::valarraystd::slice_arraystd::gslice_arraystd::mask_array、および std::indirect_array は、代替型から構築できます。
  • 2つの const std::valarray<T>& 引数を受け取るすべての関数について、代替型を受け取る同一の関数が追加されます(begin() および end() を除く)。
  • 2つの const std::valarray<T>& 引数を受け取るすべての関数について、const std::valarray<T>& と代替型のすべての組み合わせを受け取る同一の関数が追加されます。
  • 戻り値型は、最も深くネストされた引数型に対して、2レベル以上のテンプレートネストを追加しません。

Slice/mask/indirect index accesses do not chain: v[v == n][std::slice(0, 5, 2)] = x; is an error because std::mask_array (the type of v[v == n]) does not have operator[].

[edit] Example

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <valarray>
 
int main() 
{
    std::valarray<int> data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 
    std::cout << "Initial valarray:   ";
    for (int n : data)
        std::cout << std::setw(3) << n;
    std::cout << '\n';
 
    data[data > 5] = -1; // valarray<bool> overload of operator[]
    // the type of data > 5 is std::valarray<bool>
    // the type of data[data > 5] is std::mask_array<int>
 
    std::cout << "After v[v > 5] = -1:";
    for (std::size_t n = 0; n < data.size(); ++n) 
        std::cout << std::setw(3) << data[n]; // regular operator[]
    std::cout << '\n';
}

出力

Initial valarray:     0  1  2  3  4  5  6  7  8  9
After v[v > 5] = -1:  0  1  2  3  4  5 -1 -1 -1 -1

[edit] Defect reports

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 389 C++98 the return type of overload (1) was T corrected to const T&
LWG 430 C++98 the behavior was unclear for overloads
(3-10) if an invalid subset is specified
この場合の動作は
未定義となる
English 日本語 中文(简体) 中文(繁體)