STL array的size方法(18)

来源:互联网 发布:mac版qq群文件 编辑:程序博客网 时间:2024/05/01 20:03
原文地址:http://www.cplusplus.com/reference/array/array/size/
public member function
<array>

std::array::size

constexpr size_type size() noexcept;
Return size
Returns the number of elements in the array container.

返回array里面元素的数目。


The size of an array object is always equal to the second template parameter used to instantiate the array template class (N).

array的size一般都和第二个模版参数一样。


Unlike the language operator sizeof, which returns the size in bytes, this member function returns the size of the array in terms of number of elements.

和sizeof不同,sizeof是返回以bytes单位的大小,该方法是返回元素数目的大小。

例子:

#include <iostream>#include <array>using namespace std;int main(){array<int,5> ai;//{10,20,30,40};auto it=ai.end();cout<<"ai=";for(int i:ai)cout<<i<<" ";cout<<endl;cout<<"size="<<ai.size()<<endl;cout<<"sizeof(ai)="<<sizeof(ai)<<endl;}
运行截图:



Parameters

none

Return Value

The number of elements contained in the array object.

array中元素的数目。

This is a compile-time constant expression (constexpr).

这在编译时就是一个常量。

Member type size_type is an alias of the unsigned integral type size_t.

Example

123456789101112
// array::size#include <iostream>#include <array>int main (){  std::array<int,5> myints;  std::cout << "size of myints: " << myints.size() << std::endl;  std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;  return 0;}
Edit & Run


Possible output:
size of myints: 5sizeof(myints): 20

Complexity

Constant.

Iterator validity

No changes.

Data races

No contained elements are accessed: concurrently accessing or modifying them is safe.
元素不会被访问,同时访问以及修改他们都是安全的。

Exception safety

No-throw guarantee: this member function never throws exceptions.

该方法不会抛出异常。




——————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

Email:coderguang@gmail.com

2014-8-30

于GDUT

——————————————————————————————————————————————————————————————————






0 0
原创粉丝点击