名稱空間
變體
操作

std::vector<T,Allocator>::size

來自 cppreference.com
< cpp‎ | 容器‎ | vector
 
 
 
 
size_type size() const;
(C++11 起無異常丟擲)
(C++20 起為 constexpr)

返回容器中的元素數量,即 std::distance(begin(), end())

目錄

[編輯] 返回值

容器中的元素數量。

[編輯] 複雜度

常數時間。

[編輯] 示例

以下程式碼使用 size 來顯示 std::vector<int> 中元素的數量。

#include <cassert>
#include <vector>
 
int main()
{
    std::vector<int> nums;
    assert(nums.size() == 0);
    nums = {1, 2, 3, 4};
    assert(nums.size() == 4);
}

[編輯] 另請參閱

返回當前已分配儲存空間中可容納的元素數量
(public 成員函式) [編輯]
檢查容器是否為空
(public 成員函式) [編輯]
返回元素的最大可能數量
(public 成員函式) [編輯]
更改儲存的元素數量
(public 成員函式) [編輯]
(C++17)(C++20)
返回容器或陣列的大小
(函式模板) [編輯]