std::slice_array<T>::operator=
< cpp | numeric | valarray | slice array
void operator=( const T& value ) const; |
(1) | |
void operator=( const std::valarray<T>& val_arr ) const; |
(2) | |
const slice_array& operator=( const slice_array& sl_arr) const; |
(3) | (C++11 起) |
将值赋值给所有被引用元素。
1) 将
value
赋值给所有元素。2) 将
val_arr
的元素赋值给 *this 所引用的元素。3) 将自
sl_arr
选择的元素赋值给 *this 所引用的元素。参数
value | - | 赋值给所有被引用元素的值 |
val_arr | - | 用以赋值的 std::valarray |
sl_arr | - | 用以赋值的 std::slice_array |
返回值
1-2) (无)
3) *this
异常
(无)
示例
运行此代码
#include <iostream> #include <valarray> void print(char const* remark, std::valarray<int> const& v = {}) { std::cout << remark; if (v.size() != 0) { std::cout << ":"; for (int e : v) std::cout << ' ' << e; } std::cout << '\n'; } int main() { std::valarray<int> v1{1,2,3,4,5,6,7,8}; std::slice_array<int> s1 = v1[std::slice(1, 4, 2)]; print("v1", v1); print("s1", s1); print("\n(1) operator=( const T& )"); print("s1 = 42"); s1 = 42; // (1) print("s1", s1); print("v1", v1); print("\n(2) operator=( const std::valarray<T>& )"); std::valarray<int> v2{10,11,12,13,14,15}; print("v2", v2); print("s1 = v2"); s1 = v2; // (2) print("s1", s1); print("v1", v1); print("\n(3) operator=( const slice_array& )"); v1 = {1,2,3,4,5,6,7,8}; std::slice_array<int> s2 = v1[std::slice(0, 4, 1)]; std::slice_array<int> s3 = v2[std::slice(1, 4, 1)]; print("v1", v1); print("v2", v2); print("s2", s2); print("s3", s3); print("s2 = s3"); s2 = s3; // (3) 注: LHS 与 RHS 必须拥有相同大小 print("s2", s2); print("v1", v1); }
输出:
v1: 1 2 3 4 5 6 7 8 s1: 2 4 6 8 (1) operator=( const T& ) s1 = 42 s1: 42 42 42 42 v1: 1 42 3 42 5 42 7 42 (2) operator=( const std::valarray<T>& ) v2: 10 11 12 13 14 15 s1 = v2 s1: 10 11 12 13 v1: 1 10 3 11 5 12 7 13 (3) operator=( const slice_array& ) v1: 1 2 3 4 5 6 7 8 v2: 10 11 12 13 14 15 s2: 1 2 3 4 s3: 11 12 13 14 s2 = s3 s2: 11 12 13 14 v1: 11 12 13 14 5 6 7 8