STL array的size方法(18)

原文地址: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

1
2
3
4
5
6
7
8
9
10
11
12
// 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;
}


Possible output:
size of myints: 5
sizeof(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

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






C++ STL中的array是一个固定大小的数组容器,它提供了一组功能完备的操作函数,用于管理和操作数组。 array与C++内置数组相比,具有以下优势: - 通过array可以方便地获取数组的大小(通过size()函数)和最大容量(通过max_size()函数)。- array提供了一系列的成员函数,如at()、front()、back()等,用于访问和操作数组元素。 - 支持迭代器,可以使用迭代器遍历和修改数组的元素。 - array的大小是固定的,不能动态改变。 以下是一个使用array的简单示例: ```cpp #include <iostream> #include <array> int main() { std::array<int, 5> arr = {1, 2, 3, 4, 5}; std::cout << "Array size: " << arr.size() << std::endl; std::cout << "Array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << std::endl; arr[2] = 10; std::cout << "Modified array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << std::endl; return 0; } ``` 上述示例中,我们创建了一个包含5个整数的array容器arr,并初始化为{1, 2, 3, 4, 5}。通过arr.size()可以获取数组的大小,输出结果为5。然后我们使用for循环和迭代器遍历数组的所有元素,并输出到控制台。 接着我们修改数组的第三个元素为10,然后再次遍历数组并输出修改后的结果。 array是一个非常方便和实用的容器,可以用于替代传统的C风格数组,并提供更多的功能和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值