public member function
<vector>

std::vector::size

size_type size() const;
size_type size() const noexcept;
Return size
Returns the number of elements in the vector.

This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.

引數



返回值

容器中的元素數量。

成員型別size_type是一種無符號整型型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// vector::size
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<10; i++) myints.push_back(i);
  std::cout << "1. size: " << myints.size() << '\n';

  myints.insert (myints.end(),10,100);
  std::cout << "2. size: " << myints.size() << '\n';

  myints.pop_back();
  std::cout << "3. size: " << myints.size() << '\n';

  return 0;
}

輸出
0. size: 0
1. size: 10
2. size: 20
3. size: 19


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

訪問容器。
不訪問任何包含的元素:併發訪問或修改它們是安全的。

異常安全

無異常保證:此成員函式從不丟擲異常。

另見