std::shared_ptr<T>::operator[]
來自 cppreference.com
< cpp | 記憶體 | shared ptr
element_type& operator[]( std::ptrdiff_t idx ) const; |
(C++17 起) | |
索引儲存指標所指向的陣列。
如果儲存指標為空或 idx 為負,則行為未定義。
如果 T
(shared_ptr
的模板引數)是一個數組型別 U[N]
,則 idx 必須小於 N
,否則行為未定義。
目錄 |
[edit] 引數
idx | - | 陣列索引 |
[edit] 返回值
陣列的第 idx 個元素的引用,即 get()[idx]。
[edit] 異常
不丟擲任何異常。
[edit] 備註
當 T
不是陣列型別時,此函式是否宣告為未指定。如果函式已宣告,則其返回型別未指定,但保證函式的宣告(儘管不一定是定義)是合法的。
[edit] 示例
執行此程式碼
#include <cstddef> #include <iostream> #include <memory> int main() { const std::size_t arr_size = 10; std::shared_ptr<int[]> pis(new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); for (std::size_t i = 0; i < arr_size; ++i) std::cout << pis[i] << ' '; std::cout << '\n'; }
輸出
0 1 2 3 4 5 6 7 8 9
[edit] 另請參閱
返回儲存的指標 (public member function) |